2011年2月22日 星期二

[JSP] JavaServer Pages

* Is a technology, not a language.


* To put dynamic data into a Web document with Java statements embedded in special HTML tags.


The embedded Java statements will be executed by the JSP enabled Web server, not by the Web browser.


There are two key processes involved in serving a JSP page:
  • Compilation: A JSP page must be compiled into a Java Servlet class, before it can be executed. The server can compile a JSP page in real-time when the page is requested for the first time, if the page is not pre-compiled.
  • Execution: When a JSP page is requested, its compiled class will be executed on the server. The server will send back the output of the execution, not the content of the JSP page.

* Example for hello.jspWhat happened here was that Tomcat, the JSP Web server, has translated hello.jsp into hello_jsp.java, and compiled it to hello_jsp.class.

The Java file, hello_jsp.java, shows that:
  • hello_jsp is a sub class of org.apache.jasper.runtime.HttpJspBase, which is a sub class of javax.servlet.http.HttpServlet.
  • The important method in hello_jsp is _jspService() with two objects listed as parameters: one represents the Servlet request, and the other represents the Servlet response.
  • The static content of hello.jsp is translated into out.write() statements.
  • The embedded Java statement in hello.jsp is copied directly.

JSP page is served by the JSP Web server by executing the JSP Servlet Java class translated from the JSP page.


Java statements embedded in JSP pages will translated into _jspService() method of a special Servlet class.

pre-defined objects ready for the embedded Java statements:
  • out: The output stream to collect dynamic data to be mixed into the final Web document.
  • this: The instance of the special Servlet class.
  • request: An HttpServletRequest object representing the request received from the Web browser.
  • response: An HttpServletResponse object representing the response to be delivered back to the Web browser.
  • session: An HttpSession object representing the concept of linking multiple trips of requests and response into a single process unit.
  • application: A ServletContext object representing the concept of grouping Servlets into a single application.

A new session will be established, if this JSP page is requested for the first time. Subsequent requests will share the same session. 

session: A object provided by the JSP server to hold information and methods common to all JSP pages running under one session. The session object must be an instance of a class that implements the javax.servlet.http.HttpSession interface defined by the J2EE specification. 


* Reference
JavaServer Pages (JSP)
Execution Context

沒有留言:

張貼留言