Another form of JSP------servlet

Source: Internet
Author: User

In the previous Servlet login instance, an HTML was used for access, a servel processing and output, and the servlet output was displayed in the form of the following concatenation string. In the stitching of HTML tags, css or js the same one of the embedding, the disadvantage is that the display and processing is not separated. And it's cumbersome to modify the HTML output.


JSP: You can first understand the HTML tags in the middle of embedded and Java code. Let's take HelloWorld as an example:

JSP script : Embedded Java code representation.    : <%! Defines member properties and methods, which are used in such a way that few%>

<% defines local variables, which are used in the Jsp_service method, using a wide range of%>

<%= must be followed by a string variable or an expression that can be converted to a string%>
Note : <%--... ...--%>,<%//...%>,<% */* ... */%>

<span style= "FONT-SIZE:14PX;" ><%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%>

The execution process of JSP

1. Client: http://localhost:8080/test_jsp/HelloWorld.jsp access.

2.Tomcat Server: conf file in Web. xml file, execute Jspservlet this class

<span style= "FONT-SIZE:14PX;" >  <servlet> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper . servlet. Jspservlet</servlet-class> .....    <servlet-mapping>        <servlet-name>jsp</servlet-name>        <url-pattern>*.jsp</ url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>jsp</ servlet-name>        <url-pattern>*.jspx</url-pattern>    </servlet-mapping></span>
3.JspServlet: inheritance and HttpServlet Coverage service () method: for first-access JSPs, a servlet class is produced (Helloworld_ Jsp.java and Helloworld_jsp.class), is not the first time access to the corresponding servlet class that has been generated

4. helloworld_jsp: Inheritance and Httpjspbase (Httpjspbase class in service () call _jspservice () method, _jspservice () Method is not implemented in the Httpjspbase class, there is a subclass implementation)

5.helloworld_jsp the _jspservice () method to execute the Java code output HTML tag.

Jspservlet.java and Httpjspbase.java code Tomcat source There are we do not focus on understanding, know that the JSP finally translated into the servlet code to execute.

The corresponding timing diagram:



The generated Helloworld_jsp.java

<span style= "FONT-SIZE:14PX;" >package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;p ublic Final class Helloworld_jsp extends Org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent {int i = 10;public void Method1 () {} private static java.util.List _JSPX_DEP  endants;  Public Object getdependants () {return _jspx_dependants; } public void _jspservice (HttpServletRequest request, httpservletresponse response) throws Java.io.IOException, Se    rvletexception {jspfactory _jspxfactory = null;    PageContext 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.getdefaultfactory (); Response.setcontenttype ("text/html;      charset=gb18030 "); 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 ("JSP directives:

The instruction code at the beginning of the JSP indicates the way in which the JSP and Tomcat communicate, indicating the settings that return the style displayed by the client

<span style= "FONT-SIZE:14PX;" ><%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%></span>
All:

<span style= "FONT-SIZE:14PX;" ><% @page language= "script language" |extends= "ClassName" |import= "Importlist" |buffer= "none|kb size" |   --none: No buffering, default 8ksession= "True|false" |   --whether the session can be used, default trueautoflush= "True|false"  --whether the buffer is automatically cleared, default trueisthreadsafe= "True|false" |  --Default False (never set to True) info= "InfoText" |    --Any character errorpage= "Errorpageurl" |iserrorpage= "True|false" |contenttype= "Contenttyepinfo" |pageencoding= "gb2312"% ></span>

JSP built-in objects (9)
Out, request, response, PageContext, session, Application, config, exception, Page

These objects do not need to be declared to introduce the corresponding package can be used directly. For example Out.println ("HelloWorld"), but embedded Java code would need to add a corresponding reference such as:

<span style= "FONT-SIZE:14PX;" ><%@ page import= "java.util.*"%><%@ page import= "java.text.*"%></span>

when the JSP is first accessed, it is converted into a servlet code, which is then compiled into a class file for execution, and subsequent access is not compiled (it will be newly compiled after modification) . The servlet splicing HTML string output, the JSP HTML code embedded in the Java code, or did not do, processing and display separation. In order to solve this problem, a Jsp+servlet-based MVC architecture has emerged.






Copyright notice: The shortcomings of this article is unavoidable, please criticize correct, leave valuable comments.

Another form of JSP------servlet

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.