Java Server Pages (JSP) is another major technology used by the J2EE Web layer to dynamically generate Web pages. JSP technology is an extension of Servlet technology. Compared with Servlet, JSP provides a natural method for generating web pages, which can be seen in the following example.
In this example, the header information of httpservletrequest is returned as described in the previous servlet section.. The Code is as follows:
<% @ Page import = "Java. util. enumeration" %>
<HTML>
<Head>
<Title> requestheaderjsp </title>
</Head>
<Body bgcolor = "white">
<Center>
<Font color = "#009999" type = "codeph" text = "/codeph">
<Strong> list of all headers in jsp request </strong>
</Font>
</Center>
<HR>
<H3> request line is <B> method: </B> <% = request. getmethod () %>
<B> URI: </B> <% = request. getrequesturi () %>
<B> Protocol: </B> <% = request. getprotocol () %>
<Br>
<Center> <Table border = "1" align = "center">
<Tr bgcolor = "#99cee6"> <TH> name </Th> <TH> value </Th> <tr>
<%
Enumeration headernames = request. getheadernames ();
While (headernames. hasmoreelements ()){
String headername = (string) headernames. nextelement ();
%>
<Tr> <TD> <% = headername %> </TD>
<TD> <% = request. getheader (headername) %> </TD>
<% }%>
</Table>
</Body>
</Html>
The servlet code translated by Tomcat during the first request is as follows:
Package org. Apache. jsp. jsp;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import javax. servlet. jsp .*;
Import java. util. enumeration;
Public final class servletrequestheader_jsp extends org. Apache. Jasper. runtime. httpjspbase
Implements org. Apache. Jasper. runtime. jspsourcedependent {
Private Static java. util. LIST _ jspx_dependants;
Public object getdependants (){
Return _ jspx_dependants;
}
Public void _ jspservice (httpservletrequest request, httpservletresponse response)
Throws java. Io. ioexception, servletexception {
Jspfactory _ jspxfactory = NULL;
Pagecontext = NULL;
Httpsession session = NULL;
Servletcontext application = NULL;
Servletconfig Config = NULL;
Jspwriter out = NULL;
Object page = this;
Jspwriter _ jspx_out = NULL;
Pagecontext _ jspx_page_context = NULL;
Try {
_ Jspxfactory = jspfactory. getdefafactory Factory ();
Response. setcontenttype ("text/html ");
Pagecontext = _ jspxfactory. getpagecontext (this, request, response,
Null, true, 8192, true );
_ Jspx_page_context = pagecontext;
Application = pagecontext. getservletcontext ();
Config = pagecontext. getservletconfig ();
Session = pagecontext. getsession ();
Out = pagecontext. getout ();
_ Jspx_out = out;
Out. Write ("/R/N ");
Out. Write ("<HTML>/R/N ");
Out. Write ("Out. Write ("<title> requestheaderjsp </title>/R/N ");
Out. Write ("Out. Write ("<body bgcolor =/" White/">/R/N ");
Out. Write ("<center>/R/N ");
Out. Write ("<font color =/" #009999/">/R/N ");
Out. Write ("<strong> list of all headers in jsp request </strong>/R/N ");
Out. Write ("</font>/R/N ");
Out. Write ("</center>/R/N ");
Out. Write ("<HR>/R/N ");
Out. Write ("Out. Write ("<B> method: </B> ");
Out. Print (request. getmethod ());
Out. Write ("/R/N ");
Out. Write ("<B> URI: </B> ");
Out. Print (request. getrequesturi ());
Out. Write ("/R/N ");
Out. Write ("<B> Protocol: </B> ");
Out. Print (request. getprotocol ());
Out. Write ("/R/N ");
Out. Write ("<br>/R/N ");
Out. Write ("<center> Out. Write ("<Table border =/" 1/"align =/" center/">/R/N ");
Out. write ("<tr bgcolor =/" #99cee6/"> <TH> name </Th> <TH> value </Th> <tr>/R/N ");
Enumeration headernames = request. getheadernames ();
While (headernames. hasmoreelements ()){
String headername = (string) headernames. nextelement ();
Out. Write ("/R/N ");
Out. Write ("<tr> <TD> ");
Out. Print (headername );
Out. Write ("</TD>/R/N ");
Out. Write ("<TD> ");
Out. Print (request. getheader (headername ));
Out. Write ("</TD>/R/N ");
}
Out. Write ("/R/N ");
Out. Write ("</table>/R/N ");
Out. Write ("</body>/R/N ");
Out. Write ("} Catch (throwable t ){
If (! (T instanceof skippageexception )){
Out = _ jspx_out;
If (OUT! = NULL & out. getbuffersize ()! = 0)
Out. clearbuffer ();
If (_ jspx_page_context! = NULL) _ jspx_page_context.handlepageexception (t );
}
} Finally {
If (_ jspxfactory! = NULL) _ jspxfactory. releasepagecontext (_ jspx_page_context );
}
}
}
Httpjspbase is the abstract parent class of all servlet programs generated by JSP in Tomcat. It is used by the JSP processing component of Tomcat when the web program is running. jsp programmers do not need to use this class.
Implicit objects in JSP
In the previous code, we can see the request and several methods used. Request is an implicit pre-defined object and does not need to be manually created in JSP code. JSP also contains the following implicit objects.
1. application: The javax. servlet. servletcontext object obtained from the servlet configuration.
2. Session: represents the javax. servlet. http. httpsession object.
3. Request: represents the javax. servlet. httpservletrequest object.
4. Response: represents the javax. servlet. httpservletresponse object.
5. config: indicates the javax. servlet. servletconfig object.
6. Page: indicates the javax. servlet. jsp. httpjsppage object. The servlet generated by JSP.
7. pagecontext: corresponding to javax. servlet. jsp. pagecontext. The API used to provide shared page information.
8. Out: indicates the javax. servlet. jsp. jspwriter object, which is used to output the response result.
9. Exception: indicates the java. Lang. throwable object, which can be obtained only by error processing JSP.