Basic knowledge and understanding of jsp

Summary:

JSP stands for JavaServer Pages, it and servlet technologies, are technologies for developing dynamic web resources defined by SUN.

JSP biggest feature of this technology is that, as written in jsp write html, but:

Compared terms html, html data to only provide static user, the technology allows nested Jsp java code in the page, provide dynamic data to the user.

For comparison servlet, servlet difficult layout data, while at the same time may be produced in addition to jsp dynamic data using java code, the data is also very easy layout. Whether or JSP Servlet, although dynamic web resources can be used for development. But because the two technical characteristics of each, in the long-term software practice, people gradually as a controller servlet web application components to use, and the JSP technology as a data template to use.

Data of the program to beautify and then usually output: If both jsp java code to generate dynamic data, it did lead to beautify the page difficult to maintain.

Let servlet produces both data and nested inside the html code beautification data, the program will also lead to poor readability, it is difficult to maintain.

So the best way is based on the characteristics of these two technologies, each responsible for each of them, servlet only responsible for responding to requests generated data, and the data forwarding technology to bring jsp, jsp to display data to do.

Jsp operating principle:

aims:

  • Web server is how to invoke and execute a jsp page?
  • html page publishing label Jsp how they are sent to the client?
  • How Jsp page server java code is executed?
  • Web server when you call the jsp, jsp will provide some of what java objects?

Thoughts: JSP servlet Why can like, like, can also be called dynamic web development technology resources?

In fact, that is a Jsp Servlet , so we must first introduce the relevant technology Servlet, and when we first visit Jsp, the engine will Jsp Jsp this will translate into a Servlet, the java files are stored in the Tomcat work directory here we create a new MyJsp.jsp page, then visit the following, we look at the source code translated:

Here's what MyJsp.jsp page:

[html] view plain copy
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
      
    <title>My JSP 'MyJsp.jsp' starting page</title>  
      
  </head>  
    
  <body>  
    This is my JSP page. <br>  
  </body>  
</html>

Here is the source code after translation:

[java] view plain copy
package org.apache.jsp;  
  
import javax.servlet.*;  
import javax.servlet.http.*;  
import javax.servlet.jsp.*;  
import java.util.*;  
  
public final class MyJsp_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\n");  
      out.write("\r\n");  
      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");  
      out.write("<html>\r\n");  
      out.write("  <head>\r\n");  
      out.write("    \r\n");  
      out.write("    <title>My JSP 'MyJsp.jsp' starting page</title>\r\n");  
      out.write("    \r\n");  
      out.write("  </head>\r\n");  
      out.write("  \r\n");  
      out.write("  <body>\r\n");  
      out.write("    This is my JSP page. <br>\r\n");  
      out.write("  </body>\r\n");  
      out.write("</html>\r\n");  
    } 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);  
    }  
  }  
}  

We see that this class inherits org.apache.jasper.runtime.HttpJspBase, in order to see the source of this class, we need to download the tomcat source code, and then find this class, the source code is as follows:

[java] view plain copy
/* 
 * 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 You under the Apache License, Version 2.0 
 * (the "License"); you may not use this file except in compliance with 
 * the License.  You may obtain a copy of the License at 
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0 
 *  
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "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 is 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;  
}  

Well, see, inherited HttpServlet class, so that in fact is a Servlet Jsp

JSP script

JSP JSP script is defined java code the way, there are three forms:

  • <% Java codes%>, java script code that appears in the defined service method jsp files generated java file. So service method can be defined, the script can be defined.
  • <%! java code%>, java script defined in the Code, members position. (seldom use it)
  • <% = Java codes%>, java script code is defined, is output to the page. That is what can define the output statement here on what can be defined. In the method of service class java.

JSP built-in objects

Do not need to get in and create a JSP page, the object can be used directly.

jsp a total of nine built-in objects. (The first four objects as domain)

  • PageContext pageContext. Sharing data in the current page. You can also get eight other built-in objects.
  • HttpServletRequest request. In the range of a plurality of resources requesting access (jump)
  • HttpSession session. Sharing data between multiple requests within a session.
  • ServletContext application. Among all users to share data.
  • HttpServletResponse response. Response object.
  • Object page. Current page (Servlet) of the object, the equivalent of this.
  • ServletConfig config. Servlet configuration object.
  • Throwable exception. Exception object.
  • JspWriter out. Output character stream objects, the output data to the page.

The difference between out and response.getWriter (): Before tomcat respond to the client, will go first to response.getWriter () data buffer, and then look for the data out of the buffer. So the conclusion is: response.getWriter () the contents of the output is always, regardless of the position before and writing out. We recommend consistent use out to output data.

jsp code modifications without restarting the server, you can directly access the refresh.

JSP directives

JSP page is used to configure, import the resource file. Format is as follows:

There are three instructions:

  1. page: used to configure the JSP page (a must, without which it becomes a normal static page)
  2. taglib: Import tab Resources (commonly used to import jstl tag library)
  3. include: page contains the import page file. Commonly used to extract a plurality of common content jsp pages, thereby simplifying jsp pages, the page is included only need to write to tags and content configuration jsp

such as: 

page instruction common properties

  • contentType, set the response MIME type and character set of the body, and set the character set of the current page (the latter only advanced development tools to take effect, if it is low-level tools you need to configure pageEncoding property to set the character set of the current page)
  • import, to lead the pack, typically one per line import script.
  • errorPage, an exception occurs after the current page will automatically jump to a specific error page.
  • isErrorPage, identify whether the current page is an error page. If the value of this attribute is set to true, then the exception may be used as built-in objects exception. The default is false, not to use exception.

taglib directive , the need to use imported tag library.

  • prefix attribute to define a prefix, you want to define what it defined as what the future write "prefix:" we can show label tag library, tantamount to taking the individual tag library name. But there are some common names, such as the prefix c represents jstl tag library.
  • Address uri attribute defines the tag library is located.

means include command, jsp commonly used to duplicate part extraction into a new page, then the page that contains the file attribute can be used in place.

JSP comments

  • The first is the use of HTML comments, comment only for HTML snippet.
  • The second is specific to JSP comments <% - Comment content -%>, you can annotate all.

EL expression

Published 70 original articles · won praise 1 · views 2232

Guess you like

Origin blog.csdn.net/caozp913/article/details/103944676