JSP Execution Process
1. The client sends a request
2. The Web Container converts JSP into Servlet Source Code
3. The Web Container compiles the generated source code.
4. The Web container loads and executes the compiled code.
5. Return the execution result to the client.
JSP Exception Handling Mechanism
Handle abnormal operations
On the error handling page, enter <% @ page iserrorpage = "true" %>
Write Exception Handling as follows:
Exception: <% = Exception %>. Check the JSP page. <Br/>
<! -- Output error messages for exception handling -->
<% Exception. printstacktrace (New printwriter (out); %>
Error Page to jump
<% @ Page errorpage = "error. jsp" %>
Add in Web. xml
<Error-page>
<Exception-type>
Java. Lang. arithmeticexception
</Exception-type>
<Location>
/Error. jsp
</Location>
</Error-page>
All errors in the project can be redirected to the error. jsp page.
The 404 error will jump to 404.jsp
<Error-page>
<Error-code> 404 </error-code>
<Location>/404.JSP</Location>
</Error-page>
404. jsp
<Body>
The resource you accessed does not exist <br>
</Body>
JSP implicit object(Built-in JSP objects, also called implicit objects)
JSP implicit objects are instances of a group of classes loaded by web containers. It is an object that can be used directly on the JSP page. There are four main categories:
-Input and Output objects: control the input and output (request, response, and out) of the page ).
-Scope communication object: Retrieves servlet-related information (Session, application, and pagecontext) related to JSP pages ).
-Servlet object: Provides page and config information ).
-Error object: handling errors on the page (exception ).
Request object:
The request implicit object indicates the client request, which contains all the request information. Common Methods: 1. String getparameter (string name): obtains the data submitted on the request Page Based on the page form component name. 2. String getparametervalues (string name): obtains the user request data (eg: check box) When an expression component corresponds to multiple values in a page request)
Response object:
The response implicit object processes the response generated by JSP and sends the response result to the client. Common Methods: 1. Void setcontenttype (string name ): set the type and character encoding of the content generated as a response. 2. Void sendredirect (strign name): sends a response to the browser indicating that it should request another URL (data will be lost and the code below sendredirect will not be executed after the jump)
Out object:
The out object indicates the output stream. The output stream is sent to the client as a response to the request. Common methods include print (), println (), and write ().
Scope communication object --
Pagecontext object
The pagecontext object allows you to access all the implicit objects defined in the current page scope. Common Methods for pagecontext objects:
1. getrequest (), getservletconfig (), getsession (), etc.
2. Void setattribute (string name, object value)
3. Void getattribute (string name)
Session Object
The Session Object indicates the user's session status. This mechanism can be used to easily identify each user and save and track the user's session status. Common Methods of session:
1. Void setattribute (string name, object value)
2. Void getattribute (string name)
Application Object
The Application Object acts on the entire application. All client windows can share the object, which exists from the server until the server is closed. The most common method for application objects:
1. Void setattribute (string name, object Value): stores the value of an object in the application (the type of the stored value is object) by name/value ).
2. Void getattribute (string name): obtains the value of the object stored in the application based on the name (the type of the obtained value is object ).