First acquaintance with JSP

content

Introduction to JSP

Creation of jsp

How to access jsp

The essence of jsp

page directive in the header of jsp

Common properties:


Introduction to JSP

The full name of jsp is java server pages. java server page.

The main function of jsp is to replace the servlet program to return the data of the html page.

Because the servlet program returns html page data is a very cumbersome thing, which is not conducive to development and maintenance.

Creation of jsp

How to access jsp

Like html, jsp pages are stored in the web directory. The access is the same as the html page.

Such as: files in the web directory

a.html page: http://ip:port/project path/a.html

b.jsp page: http://ip:port/project path/b.jsp

The essence of jsp

The essence of jsp is a servlet program

When we access the server for the first time, tomcat will translate the jsp page into a java source file and compile it into a .class bytecode program

 The bytecode file is the corresponding java source file, which can be found by opening the source file.

 The class b_jsp inherits the HttpJspBase class. We found that the HttpJspBase class directly inherits the HttpServlet class through the idea. Therefore, the java class translated by jsp indirectly inherits the HttpServlet class, so the essence of jsp is a Servlet program

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, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
      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('\r');
      out.write('\n');

    String path = request.getContextPath();
    String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";

      out.write("\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("    <base href=\"");
      out.print(basepath );
      out.write("\"/>\r\n");
      out.write("    <meta charset=\"utf-8\"/>\r\n");
      out.write("    <title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("b.jsp的页面\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } 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);
    }
  }

Observing the translated Servlet source code, it can be found that the lower layer also sends the html page back to the client through the output stream.

page directive in the header of jsp

The page directive of jsp can modify some important properties or behaviors in the jsp page.

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

Common properties:

The language attribute             indicates what language file is translated by jsp, and it can only support Java for the time being.

The contentType attribute       indicates what is the data type returned by jsp, and the parameter value of response.setContentType() in the source code

The pageEncoding property      represents the character set of the current jsp page file itself.

The import attribute                   is used to import packages and classes as in the java source code. Such as:

<%@ page import="java.util.Map" %>

The autoFlush attribute        sets whether to automatically flush the buffer when the out output stream buffer is full, the default is true.

The buffer attribute sets the size of the out buffer, the default is 8kb

When we set not to automatically refresh the buffer, and the set buffer is relatively small, jsp overflow will occur. If automatic refresh is set, it will not overflow. (buffer setting 8kb is best for synthesis)

The errorPage property         sets the error page path to automatically jump to when an error occurs when the jsp page is running

errorPage indicates the path to automatically jump to after an error. This path usually starts with a slash. He indicates that the request address is http://ip:port/project path/, which is mapped to the web directory in the code

 The b.jsp page is as follows:

<%@ page language="java"
         contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"
          autoFlush="true"
           buffer="8kb"
            errorPage="/error500.jsp"    %>
<%--errorPage表示错误后自动跳转去的路径,这个路径一般是以斜杆开头,
            他表示请求地址为http://ip:port/工程路径/,
            映射到代码中的web目录
            --%>
<%@ page import="java.util.Map"  %>
<%
    String path = request.getContextPath();
    String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <base href="<%=basepath %>"/>
    <meta charset="utf-8"/>
    <title>Insert title here</title>
</head>
<body>
<%--制造一个错误--%>
<% int i =10/0; %>

b.jsp的页面
</body>
</html>

The isErrorPage property        sets whether the current jsp page is an error information page. The default is false. If it is true, the exception information can be obtained.

The session attribute                sets whether to create an HttpSession object when accessing the current jsp page. The default is true.

The extends property                sets who the java class translated by jsp inherits by default

Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/122796130