Why JSP built-in objects do not need to be declared

Source: Internet
Author: User

In this paper, through the analysis of a JSP running process, in-depth JSP running inside, and from a new perspective on some of the technical aspects of JSP.

helloworld.jsp

Let's take the Tomcat 4.1.17 server as an example to see how the simplest helloworld.jsp works.

Code Listing 1:HELLOWORLD.JSP

helloworld.jsp
<%
String message = "Hello world!";
%>
<%=message%>

This file is very simple, just defines a string variable and outputs it. Put this file into Tomcat's webapps\root\ directory, launch Tomcat, Access http://localhost:8080/HelloWorld.jsp in the browser, and the output in the browser is "helloworld!"

Let's take a look at what Tomcat has done. To go to Tomcat's \work\standalone\localhost\_ directory, you can find the following Helloworld_jsp.java, which is the source file that Tomcat generated when parsing helloworld.jsp:

Code Listing 2:helloworld_jsp.java

Package org.apache.jsp;

Import javax.servlet.*;
Import javax.servlet.http.*;
Import javax.servlet.jsp.*;
Import org.apache.jasper.runtime.*;

public class Helloworld_jsp extends Httpjspbase {
......
public void _jspservice (HttpServletRequest request,
HttpServletResponse response) throws Java.io.IOException, Servletexception
{
Jspfactory _jspxfactory = null;
Javax.servlet.jsp.PageContext PageContext = null;
HttpSession session = NULL;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;

try {
_jspxfactory = Jspfactory.getdefaultfactory ();
Response.setcontenttype ("text/html;charset=iso-8859-1");
PageContext = _jspxfactory.getpagecontext (this, request, Response,null, True, 8192, true);
application = Pagecontext.getservletcontext ();
Config = Pagecontext.getservletconfig ();
Session = Pagecontext.getsession ();
out = Pagecontext.getout ();
_jspx_out = out;

String message = "Hello world!";
Out.print (message);
} catch (Throwable t) {
out = _jspx_out;
if (out! = null && out.getbuffersize ()! = 0)
Out.clearbuffer ();
if (PageContext! = null) pagecontext.handlepageexception (t);
} finally {
if (_jspxfactory! = null) _jspxfactory.releasepagecontext (PageContext);
}
}
}

As can be seen from the above, helloworld.jsp is first parsed into a Java class at runtime Helloworld_ Jsp.java, this class inherits from the Org.apache.jasper.runtime.HttpJspBase base class, Httpjspbase implements the HttpServlet interface. It can be seen that JSP is compiled into a servlet first before running, which is the key to understanding JSP technology.

We also know that several objects are built into the JSP page, such as PageContext, Application, config, page, session, out, and so on, and you may wonder why these built-in objects can be used directly in the code snippets in the JSP. Observe the _jspservice () method, in fact, these built-in objects are defined here. These built-in objects are initialized before parsing the code snippet in the JSP file.

First, call Jspfactory's Getdefaultfactory () method to get a reference to a Jspfactory object for the container implementation (the Tomcat 4.1.17 in this article). Jspfactory is an abstract class defined in the javax.servlet.jsp package that defines two static methods Set/getdefaultfactory (). The set method is placed when the JSP container (TOMCAT) instantiates the page servlet (that is, the helloworld_jsp Class), so you can call the Jspfactory.getdefaultfactory () method directly to get the implementation class for this JSP factory. Tomcat is called the Org.apache.jasper.runtime.JspFactoryImpl class.

Then, call this Jspfactoryimpl's Getpagecontext () method, populate a PageContext return, and assign Pageconext to the built-in variable. Other built-in objects are obtained through the PageContext. See the above code for the specific process, and don't repeat it here. The page servlet environment is set up and the page begins parsing. The helloworld.jsp page simply defines a string variable and then outputs it directly. The parsed code is as follows:

Code Listing 3:jsp page-parsed code snippet

String message = "Hello world!";
Out.print (message);

Other:

JSP Declaration Token <%! %> a member variable or member function of a class in a servlet

The JSP expression tag <%= message %> is also inserted into the _jspservice () method, which means that the built-in object can also be used in <%= message%>. It should be) translated into out.print (message);

Why JSP built-in objects do not need to be declared

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.