Getting started with Servlet-JSP

JSP

Function: The main function 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, the development cost and maintenance cost are very high.

The general Servlet return page is as follows

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write("<!DOCTYPE html>\r\n");
        writer.write("<html lang=\en\">\r\n");
        .....
        writer.write("</html>\r\n");
    }

Can see the trouble


Visiting jsp pages is the same as html pages.
Jsp is essentially a servlet program
when we first visit jsp pages. The tomcat server will help us translate the jsp page into a java source file. And compile it into a .class file

page指令
<%@ page attribute=“value” %>

buffer specifies the size of the buffer used by the
out object autoFlush controls the buffer area of ​​the out object
contentType specifies the MIME type and character encoding of the current JSP page
errorPage specifies the error handling page that needs to be turned when an exception occurs in the JSP page
isErrorPage specifies whether the current page can be used as another The error handling page of the JSP page
extends Specifies the class from which the servlet inherits
import Import the Java class to be used
info Defines the description information
of the JSP page isThreadSafe Specifies whether the access to the JSP page is thread-safe
language Defines the scripting language used by the JSP page, the default is Java
session Specify whether the JSP page uses session
isELIgnored Specify whether to execute EL expression
isScriptingEnabled Determine whether the script element can be used

include directive

<%@ include file="File relative URL address" %>
JSP can include other files through the include directive. The included files can be JSP files, HTML files or text files. The included files seem to be part of the JSP file and will be compiled and executed at the same time.

target
<%@ taglib uri="uri" prefix="prefixOfTag" %>
JSP API allows users to customize tags. A custom tag library is a collection of custom tags.
The Taglib directive introduces a definition of a custom tag set, including library paths and custom tags.

Jsp script (not commonly used)
role: you can define attributes and methods for java classes translated by jsp, even static code blocks, internal classes, etc.

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/16
  Time: 12:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%!
        private Integer id;
        private String name;
        private static Map<String,Object> map;
        static {
    
    
            map = new HashMap<String,Object>();
            map.put("a",1);
        }
        public int abc(){
    
    
            return 12;
        }
        public static class A{
    
    
            private Integer id = 12;
            private String abc = "abc";
        }
    %>>
</body>
</html>

Insert picture description here

jsp expression script (commonly used)
<%%=expression>
features

  • All expression scripts will be translated into the _jspService method
  • The expression script will be translated into out.print() and output to the page
  • Since the content of the expression script forensic is in the _jspService() method, all objects in the jspServicef() method can be used directly.
    Function: Generate data on the jsp page
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/16
  Time: 12:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%!
        private static Map<String,Object> map;
    %>

    <%=12%><br>
    <%=12.12%><br>
    <%="我是字符串"%><br>
    <%=map%>
    <%=request.getParameter("passoword")%>
</body>
</html>

Insert picture description here

Jsp code script
Function: You can write the functions we need for self-study in the jsp page.
The characteristics of the code script are:

  • The code and the translation are all in the _jspService method
  • Since the code script is translated into the _jspService() method, the existing objects in the _jspService() method can be used directly
  • You can also quickly combine multiple code scripts to complete a completed java statement
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/16
  Time: 12:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body >

    <table border="1" cellspacing="0">
    <%
        for(int i = 0;i < 10;i ++){
    
    
           %>
            <tr>
                <td><%=i%>个数字</td>
            </tr>
            <%
        }
    %>
    </table>
</body>
</html>

Insert picture description here
JSP implicit objects

  • Instance of request HttpServletRequest class
  • Instance of response HttpServletResponse class
  • Instance of out PrintWriter class, used to output the result to the web page
  • Instance of session HttpSession class
  • An instance of the application ServletContext class, related to the application context
  • Instance of config ServletConfig class
  • PageContext An instance of the PageContext class, which provides access to all objects and namespaces of the JSP page
  • page is similar to the this keyword in Java classes
  • Object of exception exception class, representing the corresponding exception object in the JSP page where the error occurred

jsp four domain objects

  • pageContext (PageContextImpl) is valid in the current jsp page range
  • request (HttpServletRequest class) valid within one request
  • session (HttpSession class) is valid within a session (open the browser to access the server until the browser is closed)
  • application (ServletContext class) is valid throughout the web project (as long as the web project does not stop, the data is there)

Domain objects are objects that can access data like Maps. The functions of the four domain objects are the same, but the difference is their data access range.

Use order
pageContext->request->session->application

Static request and dynamic request
Static request:
dynamic request: <jsp:include page="relative URL address" flush="true" />

Principle of dynamic request: JspRuntimeLibrary.include(request,response,"/include/footer.jsp",out,false)

It is to import the file when the JSP file is converted to Servlet, and the jsp:include action here is different, the time to insert the file is when the page is requested.

Listener

  1. Listener is one of the three major components of JavaWeb. The three major components of JavaWEB are: Servlet program, Filter, Listener
  2. Listener is the standard interface of JavaEEl
  3. The role of the listener is to change something on the ship, and then return to the client (program) to do some corresponding processing by destroying the function.

ServletContextListener listener
can strengthen the creation and destruction of
ServletContext objects. ServletContext objects are created when the web project is started, and destroyed when the web project is stopped.
After listening to the creation and destruction, they will call the ServletContextListener listener method feedback.

The steps are as follows

  1. Write a class to implement ServletContextListener
  2. Implement its two callback methods
  3. Go to web.xml to configure the listener

Guess you like

Origin blog.csdn.net/m0_46656833/article/details/112696129