jsp super ppt

46
JAVA SERVER PAGES

Upload: veronica-forbes

Post on 16-Dec-2015

229 views

Category:

Documents


2 download

DESCRIPTION

jsp

TRANSCRIPT

Java Server Pages

Java Server Pages2ServletsThe purpose of a servlet is to create a Web page in response to a client requestServlets are written in Java, with a little HTML mixed inThe HTML is enclosed in out.println( ) statementsJSP (Java Server Pages) is an alternate way of creating servletsJSP is written as ordinary HTML, with a little Java mixed inThe Java is enclosed in special tags, such as The HTML is known as the template textJSP files must have the extension .jspJSP is translated into a Java servlet, which is then compiledServlets are run in the usual wayThe browser or other client sees only the resultant HTML, as usualTomcat knows how to handle servlets and JSP pages2Java Server Pages (JSP)JSP is dynamic web pageJSP is written as ordinary HTML, with a little Java mixedThe Java is enclosed in special tags, such as JSP files must have the extension .jspJSP is translated in to a Javaservlet ,which is then compiled

4How JSP worksWhen Tomcat needs to use a JSP page, it:Translates the JSP into a Java servletCompiles the servletCreates one instance of the JSP servletExecutes the servlet as normal Java codeHence, when you are writing JSP, you are writing higher-level Java codeEach call to the JSP servlet is executed in a new ThreadSince there is only one JSP object, you have to use synchronization if you use any instance variables of the servletYou have two basic choices when using JSP:Let the JSP do all the work of a servletWrite a servlet that does the work and passes the results to JSP to create the resultant HTML pageThis works because a servlet can call another servletBottom line: JSP is just a convenient way of writing Java code!4JSP life cycleSome how similar to Servlet life cycle. When any request mapped to a JSP, it is handled by a special Servlet event. Servlet checks the java Server Pages Servlet is older than the java Server Page .

If so it translates to java Server Page into a Servletclass& compiles the class. Next the Servlet sends response to the java server page.Advantages of JSPBuild process is performed automatically . Translation phase treat each data type in java Server Pages differently. Template data is transformed into code . This code emits that data stream that returns data to the client.

It is faster than Servlet.The processing of JSPWhen the browser asks the Web server for a JSP, the Web server passes control to a JSP container .

A container works with the Web server to provide the runtime environment and other services a JSP needs .

It knows how to understand the special elements that are part of JSPs . Because this is the first time this JSP has been invoked , the JSP container converts it into an executable unit called a Servlet .ContdThe entire page, including the parts that are in HTML, is translated into source code . After code translation, the JSP container compiles the Servlet , loads it automatically, and then executes it.

Typically, the JSP container checks to see whether a Servlet for a JSP file already exists . If not it does the translation process If version does not match do the translation processORGANIZATION OF THE PLATFORM

JSP Environment

Jsp flow

Look at the flow

JSP TagsThe expression is evaluated and the result is inserted into the HTML pageThe code is inserted into the servlet's service methodIf code contains declarations, they become local variables of the service methodThis construction is called a scriptletThe declarations are inserted into the servlet class, not into a method

Structure of a jsp file

16Example JSP

Hello! The time is now

Notes:The tag is used, because we are computing a value and inserting it into the HTMLThe fully qualified name (java.util.Date) is used, instead of the short name (Date), because we havent yet talked about how to do import declarations16Example JSP

First JSP page.