Javaweb---summary (14) JSP principle

Source: Internet
Author: User
Tags tomcat server

first, What is jsp?

The JSP full name is Java Server Pages, which, like Servle technology, is a technology defined by sun to develop dynamic web Resources.
The biggest feature of JSP technology is that writing JSP is like writing html, but it can only provide static data to the user compared to html, while JSP technology allows to nest Java code in the page and provide dynamic data to the User.

second, the JSP principle 2.1, the Web server is how to invoke and execute a JSP page?

The browser sends a request to the server, regardless of what resources are accessed, is actually accessing the servlet, so when accessing a JSP page, is actually accessing a servlet, when the server executes the jsp, the JSP is first translated into a servlet, so when we visit the JSP , instead of accessing the jsp, it is accessing the servlet after the JSP has been translated, such as the following code:

index.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

<%

String Path = Request.getcontextpath ();

String BasePath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

%>

<! DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en" >

<base href= "<%=basePath%>" >

<title>first jsp</title>

<body>

<%

Out.print ("Hello Jsp");

%>

</body>

When we access index.jsp through a browser, the server first translates index.jsp into a index_jsp.class, work\catalina\localhost\ project name on the Tomcat server \org\apache in the \jsp directory you can see the code for the Index_jsp.class source file Index_jsp.java,index_jsp.java as Follows:

Package org.apache.jsp;

Import javax.servlet.*;

Import javax.servlet.http.*;

Import javax.servlet.jsp.*;

Import java.util.*;

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

Implements Org.apache.jasper.runtime.JspSourceDependent {

private static final Jspfactory _jspxfactory = jspfactory.getdefaultfactory ();

private static Java.util.List _jspx_dependants;

Private Javax.el.ExpressionFactory _el_expressionfactory;

Private Org.apache.AnnotationProcessor _jsp_annotationprocessor;

Public Object getdependants () {

Return _jspx_dependants;

}

public void _jspinit () {

_el_expressionfactory = _jspxfactory.getjspapplicationcontext (getservletconfig (). getservletcontext ()). Getexpressionfactory ();

_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getservletconfig (). getservletcontext (). getattribute ( Org.apache.AnnotationProcessor.class.getName ());

}

public void _jspdestroy () {

}

public void _jspservice (httpservletrequest request, httpservletresponse Response)

Throws java.io.IOException, Servletexception {

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 {

Response.setcontenttype ("text/html;charset=utf-8");

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

Out.write (' \ n ');

String Path = Request.getcontextpath ();

String BasePath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

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

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

Out.write ("<! DOCTYPE HTML public \ "-//w3c//dtd HTML 4.01 transitional//en\" >\r\n ");

Out.write ("

Out.write ("

Out.write ("<base href=\");

Out.print (basepath);

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

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

Out.write ("<title>first jsp</title>\r\n");

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

Out.write ("

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

Out.write ("<body>\r\n");

Out.write ("");

Out.print ("Hello Jsp");

Out.write ("\ 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)

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

If (_jspx_page_context! = Null) _jspx_page_context.handlepageexception (t);

}

} finally {

_jspxfactory.releasepagecontext (_jspx_page_context);

}

}

}

As we can see, index_jsp this class is inherited Org.apache.jasper.runtime.HttpJspBase this class, by looking at the source code of the Tomcat server, you can know the apache-tomcat-6.0.20-src\java\org\apache\ Jasper\runtime directory httpjspbase The source code file for this class, as shown in:

We can look at the source code for this class httpjsbase, as Follows:

/*

* Licensed to the Apache software Foundation (ASF) under one or more

* Contributor License Agreements. See the NOTICE file distributed with

* This work for additional information regarding copyright Ownership.

* The ASF licenses this file to under the Apache License, Version 2.0

* (the "License"); Except in compliance with

* The License. Obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable or agreed to writing, software

* Distributed under the License is distributed on a "as is" BASIS,

* Without warranties or CONDITIONS of any KIND, either express or Implied.

* See the License for the specific language governing permissions and

* Limitations under the License.

*/

Package org.apache.jasper.runtime;

Import java.io.IOException;

Import javax.servlet.ServletConfig;

Import javax.servlet.ServletException;

Import javax.servlet.http.HttpServlet;

Import javax.servlet.http.HttpServletRequest;

Import javax.servlet.http.HttpServletResponse;

Import javax.servlet.jsp.HttpJspPage;

Import javax.servlet.jsp.JspFactory;

Import org.apache.jasper.compiler.Localizer;

/**

* This was the Super class of all jsp-generated Servlets.

*

* @author Anil K. Vijendran

*/

Public abstract class Httpjspbase

Extends HttpServlet

Implements Httpjsppage

{

Protected Httpjspbase () {

}

Public final void Init (servletconfig Config)

Throws Servletexception

{

Super.init (config);

Jspinit ();

_jspinit ();

}

Public String Getservletinfo () {

return Localizer.getmessage ("jsp.engine.info");

}

Public final void Destroy () {

Jspdestroy ();

_jspdestroy ();

}

/**

* Entry Point into service.

*/

Public final void service (httpservletrequest request, httpservletresponse Response)

Throws servletexception, IOException

{

_jspservice (request, response);

}

public void Jspinit () {

}

public void _jspinit () {

}

public void Jspdestroy () {

}

protected void _jspdestroy () {

}

public abstract void _jspservice (httpservletrequest request,

HttpServletResponse Response)

Throws servletexception, ioexception;

}

Httpjspbase Class is inherited httpservlet, so the Httpjspbase class is a servlet, and index_jsp is inherited httpjspbase class, so the Index_jsp class is also a servlet, So when the browser accesses the index.jsp page on the server, it is actually accessing the index_jsp servlet,index_jsp this servlet uses _jspservice this method to process the Request.

2.2. How does the HTML layout tag in the JSP page be sent to the client?

The data that the browser receives

<! DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en" >

<base href= "http://localhost:8080/JavaWeb_Jsp_Study_20140603/" >

<title>first jsp</title>

<body>

Hello JSP

</body>

are exported to the browser using the following code in the _jspservice method:

Out.write (' \ r ');

Out.write (' \ n ');

String Path = Request.getcontextpath ();

String BasePath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

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

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

Out.write ("<! DOCTYPE HTML public \ "-//w3c//dtd HTML 4.01 transitional//en\" >\r\n ");

Out.write ("

Out.write ("

Out.write ("<base href=\");

Out.print (basepath);

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

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

Out.write ("<title>first jsp</title>\r\n");

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

Out.write ("

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

Out.write ("<body>\r\n");

Out.write ("");

Out.print ("Hello Jsp");

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

Out.write ("</body>\r\n");

Out.write ("

The Java code and HTML code written in the JSP will be translated into the _jspservice method, and the Java code written in the JSP will be translated intact into Java code, such as <%out.print ("Hello Jsp");%> Translated directly into Out.print ("Hello Jsp"), while the HTML code is translated to use out.write (" In the form of output to the Browser. The HTML layout tags written in the JSP page are out.write (" Output to the browser, and the browser gets the HTML code to parse the HTML Code.

2.3. How does the Java code server in the JSP page execute?

The Java code written in the JSP is translated into the _jspservice method, and when the _jspservice method is executed, the Java code written in the JSP is executed, so the Java code server in the JSP page is called by the _ The Jspservice method executes when the request is Processed.

2.4. when the Web server calls the jsp, what Java objects will be provided to the jsp?

Looking at the _jspservice method, you can see that when the Web server calls the jsp, it gives the JSP the following 8 Java objects

PageContext pagecontext;

HttpSession session;

ServletContext application;

ServletConfig config;

JspWriter out;

Object page = this;

HttpServletRequest request,

HttpServletResponse response

Where the page object, request, and response have been instantiated, and the other 5 objects that are not instantiated are instantiated in the following way

1 PageContext = _jspxfactory.getpagecontext (this, request, response,null, true, 8192, true);
2 application = Pagecontext.getservletcontext ();
3 config = Pagecontext.getservletconfig ();
4 session = Pagecontext.getsession ();
5 out = Pagecontext.getout ();

These 8 Java objects can be used directly in a JSP page, as Follows:

<%

Session.setattribute ("name", "session Object");//use The Session object to set the properties of the Session object

Out.print (session.getattribute ("name") + "<br/>");//gets The properties of the Session object

Pagecontext.setattribute ("name", "pagecontext Object");//use The PageContext object to set the properties of the PageContext object

Out.print (pagecontext.getattribute ("name") + "<br/>");//get properties of PageContext object

Application.setattribute ("name", "application Object");//use The Application object to set the properties of the Application object

Out.print (application.getattribute ("name") + "<br/>");//get properties of Application Object

Out.print ("Hello Jsp" + "<br/>");//use out Object

Out.print ("the Name of the class translated into the server when invoking the index.jsp page is:" +page.getclass () + "<br/>");//use the Page object

Out.print ("the Name of the servlet processing the request is:" +config.getservletname () + "<br/>");//using the Config object

Out.print (response.getcontenttype () + "<br/>");//use Response Object

Out.print (request.getcontextpath () + "<br/>");//use the Request object

%>

The results of the operation are as Follows:

 

2.5. JSP Best Practices

JSP best practice is how JSP technology should be used in Development.

Both JSP and servlet can be used to develop dynamic web Resources. however, due to the characteristics of these 2 technologies, in the long-term software practice, the servlet is used as the controller component in the Web application, and JSP technology is used as the data display Template. The original reason is that the data of the program is usually beautified and then output: let the JSP use Java code to generate dynamic data, and landscaping will cause the page difficult to Maintain. Having the servlet produce both data and nested HTML code inside the data will also result in poor program readability and difficult maintenance. So the best way is based on the characteristics of these two technologies, so that they are responsible for each, the servlet is responsible for responding to the request to generate data, and the data through the forwarding technology to jsp, the data display JSP to do.

Difficult to Maintain. Having the servlet produce both data and nested HTML code inside the data will also result in poor program readability and difficult maintenance. So the best way is based on the characteristics of these two technologies, so that they are responsible for each, the servlet is responsible for responding to the request to generate data, and the data through the forwarding technology to jsp, the data display JSP to do.

2.6.the execution process of the Tomcat server

  

First time Execution:

    1. The client connects to the server through the computer, because the request is dynamic, so all requests are given to the Web container to handle

    2. Find the *.jsp file that needs to be executed in the container

    3. After the *.jsp file is converted to a *.java file

    4. *.java files are compiled to form *.class files

    5. Final server to execute the resulting *.class file

Second Execution:

    1. Because the *.class file already exists, it is not necessary to convert and compile the process

After modification, execute:

1. The source file has been modified, so it needs to be re-converted and Recompiled.

Javaweb---summary (14) JSP principle

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.