Javaweb Learning Summary (14)--jsp principle

Source: Internet
Author: User

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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <% 3 String path = Request.getcontextpat H (); 4 String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; 5%> 6  7 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 8 

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\ in the apache\jsp directory you can see the code for the Index_jsp.class source file Index_jsp.java,index_jsp.java as follows:

 1 package org.apache.jsp; 2 3 Import javax.servlet.*; 4 Import javax.servlet.http.*; 5 Import javax.servlet.jsp.*; 6 Import java.util.*; 7 8 Public final class Index_jsp extends Org.apache.jasper.runtime.HttpJspBase 9 implements Org.apache.jasper.runtime . Jspsourcedependent {Ten private static final jspfactory _jspxfactory = Jspfactory.getdefaultfactory (); Static java.util.List _jspx_dependants;14 private javax.el.ExpressionFactory _el_expressionfactory;16 private org.a Pache. Annotationprocessor _jsp_annotationprocessor;17 public Object getdependants () {return _jspx_dependants;20}2 1 public void _jspinit () {_el_expressionfactory = _jspxfactory.getjspapplicationcontext (Getservletconfig (). Get ServletContext ()). Getexpressionfactory (); _jsp_annotationprocessor = (org.apache.AnnotationProcessor)   Getservletconfig (). Getservletcontext (). getattribute (Org.apache.AnnotationProcessor.class.getName ()); 25}26 27 public void _jspdestroY () {}29-public void _jspservice (HttpServletRequest request, HttpServletResponse response) throws Java . Io. IOException, servletexception {pagecontext PageContext = null;34 HttpSession session = null;35 Servletco ntext application = null;36 servletconfig config = null;37 jspwriter out = null;38 Object page = this;39 J Spwriter _jspx_out = null;40 PageContext _jspx_page_context = null;41 try {response.setcontenttype (                   "Text/html;charset=utf-8"); PageContext = _jspxfactory.getpagecontext (this, request, response,46 NULL, True, 8192, true), _jspx_page_context = pagecontext;48 application = Pagecontext.getservletcontext (); Config = Pagecontext.getservletconfig (); session = Pagecontext.getsession (); n-out = pagecontext.ge TOut (); _jspx_out = out;53 out.write (' \ R '), Out.write (' \ n '); Tpath ();Sepath = Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; r\n "), Out.write (" \ r \ n "), Out.write (" <! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\r\n "), Out.write (" 

We can see that index_ JSP this class is inherited org.apache.jasper.runtime.HttpJspBase this class, by looking at the Tomcat server source code, can know in 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:

 1/* 2 * Licensed to the Apache software Foundation (ASF) under one or more 3 * Contributor license agreements. See the NOTICE file distributed with 4 * This work for additional information regarding copyright ownership. 5 * The ASF licenses this file to you under the Apache License, Version 2.0 6 * (the "License");  The except in compliance with 7 * the License. Obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * one * unless Requi Red by applicable law or agreed to in writing, Software12 * Distributed under the License are distributed on a "as is" BA sis,13 * without warranties or CONDITIONS of any KIND, either express or implied.14 * See the License for the specific L Anguage Governing Permissions AND15 * Limitations under the license.16 */17 package org.apache.jasper.runtime;19 Mport java.io.ioexception;21 Import javax.servlet.servletconfig;23 import javax.servlet.servletexception;24 ImporT javax.servlet.http.httpservlet;25 import javax.servlet.http.httpservletrequest;26 Import JAVAX.SERVLET.HTTP.HTTPSERVLETRESPONSE;27 Import javax.servlet.jsp.httpjsppage;28 Import JAVAX.SERVLET.JSP.JSPFACTORY;29 Import org.apache.jasper.compiler.localizer;31/**33 * This is the Super Class A ll jsp-generated servlets.34 *35 * @author Anil K. Vijendran36 */37 Public abstract class Httpjspbase extends Ht Tpservlet implements Httpjsppage () (protected httpjspbase () {}46 Lic final void init (ServletConfig config) throws servletexception {super.init (config); Jspi NIT (); _jspinit (),}54, Getservletinfo () (), Localizer.getmessage ("JSP. Engine.info ");}58 public final void Destroy () {Jspdestroy (); _jspdestroy (); 62}63 64/* *65 * Entry point to service.66 */67 public final void service (HTTPSERVLEtrequest request, HttpServletResponse response) throws Servletexception, IOException     E (Request, response);}72, public void Jspinit () {}75-public void _jspinit () {77}78 79 public void Jspdestroy () {}81 protected void _jspdestroy () {}84-public abstract void _jsps Ervice (HttpServletRequest request, httpservletresponse response) throws Servletexception, I OEXCEPTION;88}

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

1 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 2 

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

 1 out.write (' \ R '); 2 out.write (' \ n '); 3 4 String path = Request.getcontextpath (); 5 String basepath = Request . Getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; 6 7 out.write ("\ r \ n"); 8 Out.write ("\ r \ n"); 9 Out.write ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\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

1 PageContext pagecontext;2 HttpSession session;3 servletcontext application;4 servletconfig config;5 JspWriter out;6 Obj ECT page = this;7 httpservletrequest request, 8 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.g Etservletcontext (); 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:

1 <% 2         session.setattribute ("name", "Session Object");//Use Session object to set properties of Session Object 3         out.print ( Session.getattribute ("name") + "<br/>");//Gets the properties of Session Object 4         pagecontext.setattribute ("name", " PageContext object ");//Use the PageContext object to set the properties of the PageContext object 5         out.print (Pagecontext.getattribute (" name ") +" <br/ > ");//Gets the properties of the PageContext object 6         application.setattribute (" name "," Application Object ");//Use Application object, Set the properties of the Application Object 7         out.print (Application.getattribute ("name") + "<br/>");//Get the properties of the Application Object 8         Out.print ("Hello Jsp" + "<br/>");//Use Out Object 9         out.print ("The name of the class that was translated when the server called 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/> ");// Use the Config object one by one         Out.print (Response.getcontenttype () + "<br/>"),//using the Response object of the         Out.print ( Request.getcontextpath () + "<br/>");//Use 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.

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