Why do I say JSP is a servlet? How does a JSP convert to a servlet? __js

Source: Internet
Author: User

JSP is the extension of the servlet, before the JSP, there has been the servlet technology. The servlet uses output streams to dynamically generate HTML pages, including each HTML tag and each content that appears in an HTML page.

The development efficiency of the servlet is extremely low due to the large number of HTML tags, a large number of static text passes, etc. All of the presentation logic, including layout, color, and image, must be coupled in the Java code, which is really annoying. The emergence of JSP to compensate for this shortcoming, JSP by inserting Java code in the standard HTML page, its static part does not need Java program control, only those who need to read from the database and dynamically generate information according to the program, only use Java scripting.

On the surface, JSP pages no longer need Java classes and seem to be completely detached from Java object-oriented features. In fact, JSP is a special form of the servlet, each JSP page is a servlet instance--jsp page is compiled by the system into servlet,servlet and then responsible for responding to user requests. JSP is also a kind of simplification of servlet, when using JSP, actually use servlet, because each JSP page in Web application will generate corresponding servlet by the servlet container. For Tomcat, the servlet generated by the JSP page is placed under the Web application that corresponds to the work path.

Look at a simple JSP page below:

<!--indicates that this is a JSP page-->

<%@ page contenttype= "text/html; charset=gb2312 "Language=" Java%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

<HTML>

<HEAD>

<TITLE> First JSP page </TITLE>

</HEAD>

<BODY>

<!--The following is a Java script scriptlet-->

<%for (int i = 0; i < i++)

{

Out.println (i);

%>

<br>

<%}%>

</BODY>

</HTML>


After you start Tomcat, you can find the following files in Tomcat's catalinalocalhostjsptestorgapachejsp directory (if the Web application name is jsptest, the JSP page above is named test1.jsp): Test1 _jsp.java and Test1_jsp.class. All two files are Tomcat generated, and Tomcat generates the Java and class files corresponding to the servlet based on the JSP page.

The following is the source code for the Test1_jsp.java file, which is a special Java class and is a servlet class:

JSP page after Tomcat-compiled default package (different servlet container provider generated servlet file is different)

Package org.apache.jsp;

Import javax.servlet.*;

Import javax.servlet.http.*;

Import javax.servlet.jsp.*;

Inherits the Httpjspbase class, which is actually a httpservlet subclass (Jasper is Tomcat's JSP engine)

Public final class Test1_jsp extends Org.apache.jasper.runtime.HttpJspBase

Implements Org.apache.jasper.runtime.JspSourceDependent

{

private static Java.util.Vector _jspx_dependants;

Public Java.util.List getdependants () {

return _jspx_dependants;

}

Methods to respond to the user

public void _jspservice (HttpServletRequest request,

HttpServletResponse response)

Throws Java.io.IOException, Servletexception

{//built-in objects (Variavles) are created here.

Get page output stream

Jspfactory _jspxfactory = null;

PageContext pagecontext = null;

HttpSession session = NULL;

ServletContext application = null;

ServletConfig config = null;

Get page output stream

JspWriter out = null; Not printwriter. JspWriter is buffered defautly.

Object page = this;

JspWriter _jspx_out = null;

PageContext _jspx_page_context = null;

Start generating responses

Try

{

_jspxfactory = Jspfactory.getdefaultfactory ();

Set the page format for output

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 ();

Page output stream


out = Pagecontext.getout ();

_jspx_out = out;

Output stream, starting output page document

Out.write ("RN");

The following output HTML tag

Out.write ("<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0

transitional//en ">rn");

Out.write ("

Out.write ("

Out.write ("<title>first jsp</title>rn");

Out.write ("

Out.write ("<body>rn");

Loops in the page, where the output loops

for (int i = 0; i < i++)

{

Out.println (i);

Out.write ("RN");

Out.write ("<br>rn");

}

Out.write ("RN");

Out.write ("</body>rn");

Out.write ("

Out.write ("RN");

}

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.handle

Pageexception (t);

}

}

Finally

{

if (_jspxfactory!= null) _jspxfactory.releasepagecontext (_jspx_

Page_context);

}

}

}

Based on the execution of Figure 2.1, again comparing the test1.jsp and Test1_jsp.java files, you can conclude that each character in the JSP page is generated by the output stream of the Test1_jsp.java file.

According to the above JSP page working principle diagram, may obtain the following four conclusions:

The-jsp file must be run within the JSP server.

The-jsp file must generate a servlet to execute.

-The first visitor for each JSP page is slow because the JSP must be waiting to be compiled into a servlet.

Visitors to the-jsp page do not need to install any clients or even run Java environments, because JSP pages are delivered to the client by standard HTML pages.


The JSP and Servlet will have the following transformations:

-Static content of JSP pages, JSP scripts are converted to the Xxxservice () method of the servlet, similar to the service () method when creating the servlet itself.

-The JSP declaration section, converted to the member portion of the servlet. All JSP declaration parts can use modifiers such as private,protected,public,static, but not elsewhere.

-The output expression of the JSP (<%=. %> part), the output expression is converted to the output statement in the Xxxservice () method of the servlet.

   -Nine built-in objects are either formal parameters of the Xxxservice () method or local variables of the method, so nine built-in objects can only be used in JSP scripts and output expressions. You cannot use the

in a JSP declaration.

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.