Jsp implicit object

Source: Internet
Author: User

1. Introduction
To facilitate Programmer access to common objects, implicit objects are defined in JSP. These objects can be directly used without declaration. When converting from JSP to servlet, they will convert to the corresponding servlet type.

Out: indicates the output, which is equivalent to PrintWriter In the servlet. The corresponding type is javax. servlet. jsp. JspWriter.

Request: indicates the request information. The type of the request is javax. servlet. http. HttpServletRequest.

Response: indicates the response information. The corresponding type is javax. servlet. http. HttpServletResponse.

Session: Same as HttpSession in servlet. The corresponding type is javax. servlet. http. HttpSession.

Application: indicates the context of the entire application, which is equivalent to getServletConfig (). getServletContext. The corresponding type is javax.. servlet. jsp. PageContext.

Exception: Page exception, which can be used to obtain the exception information. The corresponding type is java. lang. Throwable;

Page: indicates the JSP page, which has the same meaning as this in the java class.

PageContext: the context of the JSP. The corresponding type is javax. servlet. jsp. PageContext.

Config: indicates the servletconfig of the JSP. The corresponding type is javax. servlet. ServletConfig.

PageContext, request, session, and application are four scope objects.

2. request object

Let's talk about using it to pass values.

Request. setAttribute ("name", "guo ");
Request. getRequestDispatcher ("Address"). forward (request, response );

In another page, you can receive: request. getAttribute ("name ");


3. session Object

The session Object corresponds to the HttpSession object. In JSP, the page command has an attribute related to the session, which is also called the session. If its value is set to false,
The session cannot be used on this JSP page.

Note that this is a built-in object, so you can directly use it without generating it.

SetAttribute (java. lang. String name, java. lang. Object value)

GetAttribute (java. lang. String name) returns an object

4. application

The application object is a global variable that is valid for web applications. It implements the ServletContext interface. Note that it is global, that is, all clients access the same object.

SetAttribute (java. lang. String name, java. lang. Object value)

GetAttribute (java. lang. String name) returns an object

5. Summary of four scope objects

If you put the variable in pageContext, it indicates that its scope is page, and its valid range is only in the current jsp page, starting from putting the variable into pageContext and ending the jsp page, you can use this variable.

If you put the variable in the request, it indicates that its scope is request, and its valid range is the current request cycle. The request cycle refers to the whole process from initiating an http request to processing the server and returning a response. In this process

The forward method redirects to multiple jsp pages. You can use this variable in these pages.


If the variable is put into the session, it indicates that its scope is session, and its valid range is the current session. The current session refers to the process from when the user opens the browser to when the user closes the browser. This process may contain multiple
Request Response. That is to say, as long as the user is not in the browser, the server can know that these requests are initiated by a person. The whole process is called a session and put into the session variable, it can be used in all requests of the current session.

If you put the variable in the application, it indicates that its scope is application, and its effective scope is the entire application. The entire application refers to the process from application startup to application termination, it can be simply understood as starting from the server to shutting down the server.


6. exception

The exception object is an instance of Throwable, which indicates the exception information in JSP. Note that the implicit object exception is not available in all JSP pages. If you want to use
This object must set the isErrorPage attribute value of the JSP page command to true.

His use is as follows:

Error. jsp:

Set him to an error page: <% @ page isErrorPage = "true" %>

Jsp code: <%
Date date = new Date ();
SimpleDateFormat sdf = new SimpleDateFormat ("MM minute ss seconds on mm dd, yyyy ");
String s = sdf. format (date );
Out. println ("You have an error, please check ");
Out. println ("error time:" + s + "<br> ");
Out. println ("Error Type:" + exception );
%>

In the web. xml file:

<Error-page>
<Exception-type> java. lang. Throwable </exception-type>
<Location>/error. jsp </location>
</Error-page>

Through the above settings, the error information of the entire website can be concentrated in error. jsp for processing. Try to get an error page: index. jsp

<%
Int a = 3;
Out. println (a/0 );
%>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.