JSP basic composition and execution process

Source: Internet
Author: User

JSP basic composition and execution process

A JSP (Java Server Page) Page is a file with the. jsp extension.

JSP is a page running on the server. Unlike html files, JSP can be run directly in a browser. JSP pages must be deployed in web containers and compiled into servlets for execution. JSP is a form of Servlet.

In fact, JSP is the product of Servlet development. I will introduce the relationship between JSP and Servlet in my blog later.

 

The JSP basic page consists of five parts: instruction tag, HTML markup language, annotation, Java code, and JSP action tag.

1. JSP command labels do not generate any content that is output to the webpage. They are mainly used to define information about the entire JSP page, such as the language used, imported class packages, and specified error handling pages.

There are three JSP commands:

Page ---- defines the attributes of the entire JSP page. These attributes are converted to the corresponding java code when the JSP is parsed into a Servlet by the server.

Main attributes include language extends import pageEncoding contentType session, etc.

Include -- File Inclusion command, which can contain the content of another file on the JSP page (it can be a java code, html code or JSP page)

Taglib --- reference the tag library command

2. HTML Markup Language

3 Java code

Syntax format <% write java code %>

JSP expressions <% = expression %> can directly output the Java expression results to the JSP page. The final result of the expression is converted to the string type.

4. Comments include html comments. JSP comments. java code comments.

5. JSP action labels are mainly used to implement special functions, such as forwarding user requests, including operations on JavaBean by other files.

Action tags that contain external files. You can include the content of another file to the current JSP page, which can be static text or dynamic code.

Difference between action labels and include commands: include Commands include static resources. If jsp dynamic content is included with include commands, static resources are also included in the current page.

That is, static inclusion, which is included first and then compiled into a class file (static inclusion cannot pass parameters ). While The action tag is a dynamic inclusion. For a dynamic page, it is first compiled and then included. That is, the current page and the contained resources are two independent entities. The contained JSP page processes the request object in the JSP page that contains it, uses the processing result as the processing content on the current JSP page, and sends it together with the content on the current page to the client.

Request forwarding tag to forward the current page request to other Web resources.

You can set parameter tags as sub-tags of other tags and PASS Parameters for other tags. For example

 

       
  
 

 

 

When the JSP page is requested for the first time, the JSP Engine converts the original JSP file to the Servlet Source Code, calls the Java compiler, compiles the file into a Servlet, and runs the file in the Servlet engine, the container returns the generated page to the client. When a request is sent again, the JSP engine runs the Servlet based on whether the JSP file is updated. If the file is not updated, the conversion and compilation process is re-executed. Compared with Servlet, the execution performance of JSP is only the first execution. When the call is repeated, the Servlet generated for the first execution is directly executed.

The following is the simplest code of index. jsp.

 

<%@ page language=java import=java.util.* pageEncoding=UTF-8%><%String path = request.getContextPath();String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%>
> This is my JSP page.

 

 

After the release is executed in Tomcat, two files index_jsp.class and index_jsp.java are generated.

 

/** Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/8.0.17 * Generated at: 12:09:56 UTC * Note: the last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */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, org. apache. jasper. runtime. jspSourceImports {private static final javax. servlet. jsp. jspFactory _ jspxFactory = javax. servlet. jsp. jspFactory. getdefafactory Factory (); private static java. util. map
 
  
_ Jspx_dependants; private static final java. util. Set
  
   
_ Jspx_imports_packages; private static final java. util. Set
   
    
_ Jspx_imports_classes; static {_ jspx_imports_packages = new java. util. hashSet <> (); _ jspx_imports_packages.add (javax. servlet); _ jspx_imports_packages.add (java. util); _ jspx_imports_packages.add (javax. servlet. http); _ jspx_imports_packages.add (javax. servlet. jsp); _ jspx_imports_classes = new java. util. hashSet <> ();} private javax. el. expressionFactory _ el_expressionfactory; private org. apache. tomcat. instanceManager _ jsp_instancemanager; public java. util. map
    
     
GetDependants () {return _ jspx_dependants;} public java. util. Set
     
      
GetPackageImports () {return _ jspx_imports_packages;} public java. util. Set
      
        GetClassImports () {return _ jspx_imports_classes;} public void _ jspInit () {// initialize _ el_expressionfactory = _ jspxFactory. getJspApplicationContext (getServletConfig (). getServletContext ()). getExpressionFactory (); _ jsp_instancemanager = org. apache. jasper. runtime. instanceManagerFactory. getInstanceManager (getServletConfig ();} public void _ jspDestroy () {// destroy} public void _ jspService (final javax. servlet. Http. httpServletRequest request, final javax. servlet. http. httpServletResponse response) throws java. io. IOException, javax. servlet. servletException {final java. lang. string _ jspx_method = request. getMethod (); if (! GET. equals (_ jspx_method )&&! POST. equals (_ jspx_method )&&! HEAD. equals (_ jspx_method )&&! Javax. servlet. dispatcherType. ERROR. equals (request. getDispatcherType () {response. sendError (HttpServletResponse. SC _METHOD_NOT_ALLOWED, JSPs only permit GET POST or HEAD); return;} final javax. servlet. jsp. pageContext pageContext; javax. servlet. http. httpSession session = null; final javax. servlet. servletContext application; final javax. servlet. servletConfig config; javax. servlet. jsp. jspWriter out = null; final java. lang. object page = this; javax. servlet. jsp. jspWriter _ jspx_out = null; javax. servlet. jsp. 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 (''); out. write (''); String path = request. getContextPath (); String basePath = request. getScheme () +: // + request. getServerName () +: + request. getServerPort () + path +/; out. write (); out. write (); out. write (); out. write (); out. write (); out. write (
       ); Out. write (); out. write (
       ); Out. write (
       ); Out. write (
       ); Out. write (
       ); Out. write (
       ); Out. write (
       ); Out. write (
       ); Out. write (); out. write (); out. write (); out. write (This is my JSP page .); out. write (); out. write ();} catch (java. lang. throwable t) {if (! (T instanceof javax. servlet. jsp. SkipPageException) {out = _ jspx_out; if (out! = Null & out. getBufferSize ()! = 0) try {if (response. isCommitted () {out. flush ();} else {out. clearBuffer () ;}} catch (java. io. IOException e) {} if (_ jspx_page_context! = Null) _ jspx_page_context.handlePageException (t); else throw new ServletException (t) ;}} finally {_ jspxFactory. releasePageContext (_ jspx_page_context );}}}
      
     
    
   
  
 

The execution process of JSP file requests processed by Web containers mainly includes the following four parts:

 

1. The client sends a Request

2JSP container translates JSP into Servlet code

3. Compile the translated Servlet code and load it To the memory for execution.

4. Request the result to the client.

 

 

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.