A JSP.

Instructions 1.

  1. Function: Configure a JSP page or import a resource file
  2. format:<%@ Directive name Attribute name 1= attribute value 1 Attribute name 2= Attribute value 2 %>
  3. Classification of instruction names
    1. page: Configure the JSP page
      • Classification of attribute names
        1. contentType: Function equivalent toresponse.setContentType()
          • Sets the MIME type and character set of the response body, for exampletext/html; utf-8
          • Sets the encoding of the current JSP page (only for advanced ides, such as IDEA; This parameter is invalid for low-level tools, such as NotepadpageEncodingProperty to set the encoding of the current JSP page.
        2. import: guide package
        3. errorPage: Indicates the error page to be redirected to if an exception occurs on the current page
        4. isErrorPage: indicates whether the current page is an error page
          • true: Yes, you can use the built-in object Exception
          • false: No, the built-in object Exception cannot be used
    2. include: You can include other pages in the current page
      • Such as:<%@ include file="index.jsp" %>
    3. taglib: Imports the label library
      • Such as:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/japl/core" %>
        • Among themprefic: User-defined prefix

2. Comment

  1. HTML comments
    1. <! — –> : Can only comment HTML snippets
  2. JSP annotations (recommended)
    1. <%– –%> : All can be commented

3. Built-in objects

  1. Concept: Objects that can be used directly in A JSP page without being created
Object name Belongs to the class role
pageContext PageContext The attribute is shared on the current page, and its methods can get the other eight built-in objects
session HttpSession Attributes that share a session
application ServletContext All users share the attribute
request HttpServletRequest Share attributes for forwarding a request
response HttpServletResponse As a response object
page Object Represents a reference to the current object, equivalent to this
out JspWriter As an output object, output content to the page
config ServletConfig As a configuration object for the Servlet
exception Throwable The exception object

Two: MVC development mode

1. JSP evolution history

  1. Since there were only servlets initially, it was difficult to output the data the user needed to the page
  2. Later, JSP made Servlet development easier, but if you write too much Java code in JSP, it gets mixed up with HTML, resulting in messy code that is difficult to maintain
  3. Later, Java Web development drew lessons from MVC development mode, with clear division of labor, which made the program design more reasonable and easy to maintain

2. MVC

  1. M:You should use Modal.withJavaBeanimplementation
    1. Perform specific service operations, such as querying databases and encapsulating objects
  2. V:ViewwithJSPimplementation
    1. The page presented to the user
  3. C:Controller (Controller)withServletimplementation
    1. Get the user’s request
    2. Invocation model
    3. Give the data to the view for presentation
  4. The advantages and disadvantages
    • advantages
      1. Low coupling and easy maintenance
      2. High reusability (Controllers and models can be reused)
    • disadvantages
      1. It complicates the project structure and increases the demands on developers