A JSP.
Instructions 1.
- Function: Configure a JSP page or import a resource file
- format:
<%@ Directive name Attribute name 1= attribute value 1 Attribute name 2= Attribute value 2 %>
- Classification of instruction names
page
: Configure the JSP page- Classification of attribute names
contentType
: Function equivalent toresponse.setContentType()
- Sets the MIME type and character set of the response body, for example
text/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 Notepad
pageEncoding
Property to set the encoding of the current JSP page.
- Sets the MIME type and character set of the response body, for example
import
: guide packageerrorPage
: Indicates the error page to be redirected to if an exception occurs on the current pageisErrorPage
: indicates whether the current page is an error pagetrue
: Yes, you can use the built-in object Exceptionfalse
: No, the built-in object Exception cannot be used
- Classification of attribute names
include
: You can include other pages in the current page- Such as:
<%@ include file="index.jsp" %>
- Such as:
taglib
: Imports the label library- Such as:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/japl/core" %>
- Among them
prefic
: User-defined prefix
- Among them
- Such as:
2. Comment
- HTML comments
- <! — –> : Can only comment HTML snippets
- JSP annotations (recommended)
- <%– –%> : All can be commented
3. Built-in objects
- 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
- Since there were only servlets initially, it was difficult to output the data the user needed to the page
- 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
- 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
- M:You should use Modal.withJavaBeanimplementation
- Perform specific service operations, such as querying databases and encapsulating objects
- V:ViewwithJSPimplementation
- The page presented to the user
- C:Controller (Controller)withServletimplementation
- Get the user’s request
- Invocation model
- Give the data to the view for presentation
- The advantages and disadvantages
- advantages
- Low coupling and easy maintenance
- High reusability (Controllers and models can be reused)
- disadvantages
- It complicates the project structure and increases the demands on developers
- advantages