JSP compiled into Servlet (3) JSP compiled Servlet

Source: Internet
Author: User

JSP compiled into Servlet (3) JSP compiled Servlet

What is the Servlet class after JSP compilation? What are the mappings between them? When discussing the relationship between JSP and Servlet, Let's first look at what a simple HelloWorld. jsp will look like after it is compiled into HelloWorld. java.

① HelloWorld. jsp

<% @ Page contentType = "text/html; charset = gb2312" language = "java" %>

<%

Out. println ("HelloWorld ");

%>

② HelloWorld_jsp.java

Package org. apache. jsp;

 

Import javax. servlet .*;

Import javax. servlet. http .*;

Import javax. servlet. jsp .*;

Public final class HelloWorld_jsp extends org. apache. jasper. runtime. HttpJspBase

Implements org. apache. jasper. runtime. JspSourceDependent {

Private static final javax. servlet. jsp. JspFactory _ jspxFactory =

Javax. servlet. jsp. JspFactory. getdefafactory Factory ();

Private static java. util. Map _ Jspx_dependants;

Public java. util. Map GetDependants (){

Return _ jspx_dependants;

}

Public void _ jspInit (){

}

Public void _ jspDestroy (){

}

Public void _ jspService (final javax. servlet. http. HttpServletRequest request, final javax. servlet. http. HttpServletResponse response)

Throws java. io. IOException, javax. servlet. ServletException {

Final javax. servlet. jsp. PageContext pageContext;

Javax. servlet. http. HttpSession session = null;

Final javax. servlet. ServletContext application;

Final javax. servlet. ServletConfig config;

Javax. servlet. jsp. JspWriter out = null;

Final java. lang. Object page = this;

Javax. servlet. jsp. JspWriter _ jspx_out = null;

Javax. servlet. jsp. PageContext _ jspx_page_context = null;

Try {

Response. setContentType ("text/html; charset = gb2312 ");

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 ("\ r \ n ");

Out. write ("

\ R \ n ");

Out. write ("

\ R \ n ");

Out. write ("

\ R \ n ");

Out. write ("\ r \ n ");

Out. write ("

\ R \ n ");

Out. println ("HelloWorld ");

Out. write ("\ r \ n ");

Out. write ("\ r \ n ");

Out. write ("\ r \ n ");

} Catch (java. lang. Throwable t ){

If (! (T instanceof javax. servlet. jsp. SkipPageException )){

Out = _ jspx_out;

If (out! = Null & out. getBufferSize ()! = 0)

Try {out. clearBuffer ();} catch (java. io. IOException e ){}

If (_ jspx_page_context! = Null) _ jspx_page_context.handlePageException (t );

Else throw new ServletException (t );

}

} Finally {

_ JspxFactory. releasePageContext (_ jspx_page_context );

}

}

}

After parsing the preceding syntax and compiling the HelloWorld. jsp file into the corresponding HelloWorld_jsp.java file using the visitor mode, we can see that the Servlet class name is spelled by the jsp file name _ jsp. Next, let's look at the detailed content of the HelloWorld_jsp.java file. The default class package name is org. apache. jsp. By default, there are three imported "import javax. servlet. *; import javax. servlet. http. *; import javax. servlet. jsp. *;".

The next step is the real class body. All java classes generated by jsp must inherit org. apache. jasper. runtime. httpJspBase. The structure of this class is as follows. It inherits from HttpServlet to inherit all functions of HttpServlet, in addition, the HttpJspPage interface defines the core processing method of a JSP Servlet _ jspService. In addition, _ jspInit and _ jspDestroy are used for execution during jsp initialization and destruction, in fact, these methods are indirectly called by Servlet service, init, and destroy methods. Therefore, jsp servlet generation mainly implements these three methods.

Besides inheriting HttpJspBase, you also need to implement the org. apache. jasper. runtime. JspSourceDependent interface, which has only one returned Map For the getDependants () method, the Map key value is the resource name and the last modification time respectively. This implementation is mainly to record whether some dependent resources are outdated, dependent resources may be imported by the page command or referenced by TAG files. When the servlet is generated, if the jsp page has the preceding dependency, a static block will be added to the servlet class. The static block will add the resource and the last modification time to the Map.

The jsp servlet processing process depends on a lot of resources. For example, if you want to operate a session, you need to access the HttpSession object. For example, if you want to operate an object at the Context container level, you need a ServletContext object, for example, if I want to obtain servlet configuration information, I need a ServletConfig object. Finally, I need an output object to output the content during processing. These objects are used in the core method _ jspService. It is very easy to obtain these objects as servlet classes, because these objects are servlet attributes and can be directly obtained using related methods. However, because JSP has its own standards, it must be implemented according to its standards.

What are the specific JSP standards? First, to facilitate JSP implementation, a unified factory class JspFactory is provided for obtaining different resources. Second, servlet context cannot be used directly according to the standard, therefore, we need to define a PageContext class to encapsulate the servlet context. Finally, we also need to define an output class JspWriter to encapsulate the servlet output according to the standard requirements. Therefore, we can see that the PageContext object is obtained through JspFactory, and other ServletContext objects, ServletConfig objects, HttpSession objects, and JspWriter are obtained through the PageContext object. These objects are added with the syntax tree objects parsed in the previous chapter. Then, the visitor pattern can be used to traverse the syntax tree to generate the core processing method _ jspService.

The above is just the simplest process of converting a jsp page into a servlet. It aims to illustrate the conversion principle from jsp to servlet. In fact, many jsp command labels need to be processed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.