[Javaweb 04] JSP principle analysis and simple understanding

JSP understanding

Java Server Pages: java server-side pages for dynamic web technology

In the early days, there was no jsp, and the front end needed to use out. Print ("< H1 > 123123 < / H1 >") in the servlet; Line by line (jsp encapsulates this step)

jsp pages can embed java code to provide users with dynamic data

1. JSP principle

1. index.jsp file analysis

  1. File: like html, there is no difference

  2. Inside the server: there is a work directory in tomcat. You can find that jsp pages are converted to index_jsp.java file

    The Tomcat directory can be found in the working folder of the IDEA, and there is also the Tomcat path of the work directory of the IDEA: user / AppDate/JetBrains/tomcat /

    Therefore, the JSP file will finally be converted into a jave class, which inherits the HttpJspBase class, which inherits the HttpServlet class

    So JSP is essentially a Servlet class

2. xxx_jsp.java analysis:

The. java file contains: method, built-in object, assignment of built-in object, and out.write() code of the front-end page

  1. JSP methods: initialization, destruction, jspservice

    public void _jspInit(){}
    public void _jspDestroy(){}
    public void _jspService(req, resp){
        Request judgment
        Built in object
        Code of the output page
    }
    
  2. JSP_ Source code of jspService method in java file:

    1. Judgment request,

    2. Built in some objects

      final javax.servlet.jsp.PageContext pageContext;//Ask up and down the page
      javax.servlet.http.HttpSession session = null;//Session
      final javax.servlet.ServletContext application;//ApplicationContext
      final javax.servlet.ServletConfig config;//to configure
      javax.servlet.jsp.JspWriter out = null;//Output out object, (jspWriter)
      final java.lang.Object page = this;//Page: represents the current page
      req
      resp
      
    3. Code before output page:

      Pagecontext, application, config, session and out objects can be used directly!

      response.setContentType("text/html");//Set the display mode of the front-end page
      pageContext = _jspxFactory.getPageContext(this, request, response,//These objects can be used directly!!!!!!
                                                null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;
      
      //Interface output code:
      out.write("<h1>.....</h1>");
      ...
      
  3. jsp is a decorator, which is used to convert. jsp files into. java files, and then return them to tomcat in the server, which runs on the JVM.

30. JS file to XXX_jsp.java ` file,

  1. The java code is output intact
  2. The html code is converted to out. Write(“

    ... ") output

2. JSP basic syntax

1. Write java program in jsp:

1. The scope is_ In the jspService() method

  • jsp expression:

    <%--Output program results to client --%>
    <%= Variable or expression%>
    <%=new java.util.Date()%>
    Or:
    ${=i}//EL, express language expression
    
  • jsp script fragment

    <%
    	code snippet;
    %>
    

    Output call out.println("");

  • Nesting: the jsp interface is directly understood as a java file, and the following code outputs hello four times

    All the code is in_ jspService() method, output

    <%
    for(i=1;i<5;i++)
    {  >%
        <h1> hello   <%=i%> </h1>
    <%
    }
    >%
    

2. The scope is xxx_jsp.java class:

At this point, in the generated. java file, the code is in the class, not in the_ jspService() method.

<%! The code in is called a JSP declaration.

<%! 
    static{
    System.out.println("static code");
}
private int age = 10;
    %>

3. Summary:

<%= expression/variable %>
${= variable}

<% java fragment %>

<%! Global variable declaration %>

<!-- HTML Comments for are displayed in the front-end page (client) -->
<%-- JSP Comments for are not displayed in the front page (not visible to the client) --%>

2. jsp instruction set

1. How to jump to the error page:

  1. Configure tags in web.xml:

    <error-page>
    	<error-code>404</error-code>
    	<location>${pageContext.request.contextPaht}/img/404.jsp</location>
    </error-page>
    
  2. In the jsp page, it is represented by an instruction:

    <%@ page errorPage="${pageContext.request.contextPath}/img/404.jsp" %>
    

2. Other directives:

Import package

<%@ page import=java.util.Date %>

Other settings: all after the space in the page

Add common headers and tails to the page:

web directories are usually placed as follows: (common places common parts and error places error pages)

Description of Web project directory: (generally, there are three files in main: java, resource and web). Here, the web is outside

  1. include (in the generated java file, directly display the header.jsp and footer.jsp pages out.println(),

    Therefore, if the namespace is the same, the same variable name cannot be used in the code of three pages

    <%@ include file = "common/header.jsp" %>
    Own page body
    <%@ include file = "common/footer.jsp" %>
    
  2. jsp tag (the generated java file still introduces two pages, so it is still equivalent to three pages

    Each has its own namespace

    <jsp:include page= "/common/header.jsp" />
    Own page body
    <jsp:include page= "/common/footer.jsp" />
    

3. jsp 9 built-in objects

Nine built-in objects when converting jsp pages to. java files:

  • pageContext, two parameters passed in: req and resp
  • Session
  • Application is ServletContext
  • config, ServletConfig
  • out
  • page = this
  • Exception (not seen before)

Four objects for storing things:

pageContext, req, Session , Application

//Put something in
pageContext.setAttribute("name1", "Name 1");//Valid in one page
request.setAttribute("name2", "Name 2");//Valid in one request
session.setAttribute("name3", "Name 3");//Valid in one session
application.setAttribute("name4", "Name 4");//It is valid from server on to server off
//Take things out (you can use pageContext to take them out)
String name1 = (String) pageContext.findAttribute("name1");
String name5 = (String) pageContext.findAttribute("name5");

The reason why pageContext can be used: the scopes of the four objects are different

The scope from low to high is:

pageContext ---- request ----session ----application, if not found,

​ $ {name1}: no value will be displayed,

​ <%= Name1% >: display is null, so use EL expression

Forwarding page:

pageContext.forward("/index.jsp");
//The actual operation is:
req.getRequestDispatcy("/index.jsp").forward();

Therefore, the forwarded page can get the values in request, session and application

If there is no page forwarding, the new page can only get the values of session and application!!!

3. JSP tag, JSTL tag, EL expression

1. EL expression

  1. Obtain data, 2. Perform operations, 3. Obtain common objects for web development

Solution to the problem that EL expressions cannot be used in files:

Note: the EL expression cannot be used because the version of the web is too low, which may be 1.0. You need to set the version of the jsp file header to 4.0 (directly copy the one in tomcat) or add <% @ page iselignored = "false"% > to the file

2. JSP tag

<jsp:forward page="index.jsp">
    <jsp:param name="name1" value="value1"/>
</jsp:forward>

3. JSTL label

JSTL tag is to make up for the deficiency of html tag; the function of tag is the same as that of java code. (java code can be implemented with tag)

You must introduce headers to use. You only need to understand the core tags (just go to the rookie tutorial)

eg: if implementation, < C: set saves data

Tags: Java

Posted on Sun, 28 Nov 2021 07:18:31 -0500 by zhTonic