Saturday, August 12, 2006

JSP Core

I collected and summarized the main points about JSP, which are listed below:

Standard JSP Statements
1. JSP comments: <%-- comments --%> , it is not visible to the clients
2. JSP declaration: <%! int k = 5; %>
3. JSP expressions and scriptlets: <%= someValue %>, <% java code %> .

JSP Directives
1. include: <%@include file="relativeURLspec" %> .
2. page: <%@page language/ extends/ import/ session/ buffer/ autoFlush/ isThreadSafe/ info/ errorPage/ contentType/ isErrorPage %> .
3. tag lib: <%@taglib uri="tagLibURI" prefix="tagPrefix" %> .

JSP Actions
1. <jsp:forward page="relativeURL" >
<jsp:param name="pName" value="pValue" />
</jsp:forward >
2. <jsp:include page="relativeURL" flush="true" >
<jsp:param name="pName" value="pValue" />
</jsp:include >

Note the value of flush must be set to true. The difference between action of include and directive of include is that jsp include action is executed at run time, while the directive is executed at compile time.
3. <jsp:plugin type="typebean"/ code="classFile"/ codebase="classDir">
</jsp:plugin >
jsp:plugin is used to download the JavaBeans or Applet from the server and execute them at the client's browser.
4. <jsp:useBean id="name" class="className" scope="page request session application" typeSpec >
</jsp:useBean >
jsp:useBean is the core of the java component technology, and it's used to instantiated one or more JavaBean components.
5. <jsp:setProperty name="BeanName" PropertyExpr/>
jsp:setProperty is used to set the property of a JavaBean.
6. <jsp:getProperty name="BeanName" property="propertyName"/ >.

JSP Internal Objects
1. out: it's used to output data to the clients, and manage the output buffer of the application server. Its base class is javax.servlet.jsp.JspWriter.
2. request: it's used to get the input message from the clients. Its base class is javax.servlet.http.HttpServletRequest.
3. response: it deals with http connection messages, such as cookies and HTTP headers. One important method of Response is sendRedirect, to redirect the clients to other pages:
<%response.sendRedirect("");%> .
The repsonse object can also redirect a page by setting the header information:
String newLocn="/newpath/index.html";
4. exception: it's used to deal with the errors and exceptions during the execution of JSP programms. And it can only be used in a page with a page directive specifying isErrorPage true. The exception object is an instantiated object of the java.lang.Throwable class.
5. config: it's a message sent by the JSP engine to a servlet program when it is initialized. The information includes the parameters for initialization and server information. Use its getServletContext() method can return an application object. The config object is an instantiated object of the javax.servlet.ServletConfig interface.
6. page: it refers to the current JSP program, and it's analogous to "this" in the class file. It's an instantiated object of the java.lang.Object class.
7. application: it's an instantiated object of the javax.servlet.ServletContext interface.
8. session: it's an instantiated object of the javax.servlet.http.HttpSession interface. It's used to store user information.
9. pageContext: it's an intantiated object of the javax.servlet.jsp.PageContext class. It provides accesses to all the JSP objects and namespaces.
10. global.asa file is used to execute some programm when initializing or destroying application and session objects. It has 4 main methods to be defined: applicationInit(), sessionInit(), applicationDestroy(), sessionDestroy().

In fact, JSP is developed on the basis of servlet technology, and it's a high-level wrapping of the servlet technolgoy.
Servlet lifecycle:
1. web server loads a servlet.
2. web server instantiates a servlet.
3. web server calls the init() method of the servlet.
4. when the server receives a client request, it create an object of HttpServletRequest, and then an object of HttpServletResponse.
5. web server activates the service() method of the servlet and pass those 2 created objects as parameters.
6. for more requests, web server creates new request and response objects, but still calls the service() method of the same servlet.
7. when the server shuts down, it calls the destroy() method of the servlet.
8. note the init() and destroy() methods only execute once.
Server side include (SSI):
The file use SSI has an extension .shtml, and it's actually a text file containing the include and html tags. SHTML supports 2 kinds of tags: <servlet> and <include> .
The javax.servlet package:
It contains 4 packages: javax.servlet, javax.servlet.http, javax.servlet.jsp and javax.servlet.jsp.tagext. The first one is the basis of the whole servlet API; the second one is the HTTP extension of servlet; the third one is the JSP extension of the servlet; the last one is used to support Tag Library.
javax.servlet: contains ServletConfig interface, ServletContext interface, ServletRequest interface, ServletResponse interface, RequestDispatcher interface, ServletInputStream interface, and ServletOutputStream interface.
javax.servlet.http: contains HttpServletRequest interface, HttpServletResponse interface, HttpSession interface, HttpServlet class, Cookie class, and HttpUtils class.

No comments: