Lists JSP built-in objects and describes how to pass values on pages in the same application ?, Jsp built-in

Source: Internet
Author: User
Tags map class

Lists JSP built-in objects and describes how to pass values on pages in the same application ?, Jsp built-in

Lists JSP built-in objects and describes how to pass values on pages in the same application?

Jsp has nine built-in objects: request, reponse, page, pagecontext, session, application, execption, config, out, And param.

1. request object

The request object is a javax. servlet. httpServletRequest object. This object represents the client request information and is mainly used to accept data transmitted to the server over HTTP. (Including header information, system information, request method, and request parameters ). The scope of the request object is one request.

2. response object

Response refers to the response to the client, mainly to pass the objects processed by the JSP Container Back to the client. The response object also has a scope, which is only valid on the JSP page.

3. session Object

A session object is a user request-Related Object automatically created by the server. The server generates a session object for each user to save the user's information and track the user's operation status. The session object uses the Map class to save data. Therefore, the format of the stored data is "Key/value ". The value of the session object can make complex object types, not limited to string types.

4. application Object

The application object can save the information on the server until the server is closed. Otherwise, the information saved in the application object will be valid throughout the application. Compared with the session object, the application object has a longer life cycle, similar to the system's "global variable ".

5. out object

The out object is used to output information in a Web browser and manage the output buffer on the application server. When you use an out object to output data, you can operate on the data buffer, and immediately clear the residual data in the buffer, giving up the buffer space for other outputs. After the data is output, close the output stream in time.

6. pageContext object

The pageContext object is used to obtain parameters of any range. It can be used to obtain out, request, reponse, session, application, and other objects of the JSP page. The creation and initialization of the pageContext object are completed by the container. You can directly use the pageContext object on the JSP page.

7. config object

The main function of the config object is to obtain the configuration information of the server. You can use the getServletConfig () method of the pageConext object to obtain a config object. When a Servlet is initialized, the container passes some information to the Servlet through the config object. Developers can provide initialization parameters for Servlet programs and JSP pages in the application environment in the web. xml file.

8. page Object

The page object represents the JSP itself and is valid only on the JSP page. The page implicit object essentially contains the variables referenced by the current Servlet interface, similar to the this pointer in Java programming.

9. exception object

The exception object is used to display exception information. It can only be used on pages containing isErrorPage = "true". Using this object on a common JSP page will not compile JSP files. The annotation object is the same as all Java objects and has the inheritance structure provided by the system. The exception object defines almost all exceptions. In Java programs, you can use the try/catch keyword to handle exceptions. If exceptions are not caught on the JSP page, an exception object is generated, transfer the exception object to the error page set in the page command, and then process the exception object on the error page.

Value Transfer Mode between pages in the same application:

(1) Add

For example, <a href = "thexuan. jsp? Action = transparams & detail = directe "> directly passing parameters </a>

When using response. sendRedirect for page redirection, you can also use the following code:

Response. sendRedirect ("thexuan. jsp? Action = transparams & detail = directe "), which can be obtained by request. getParameter (name ).

(2) jsp: param

It allows you to pass parameters to the include page on the home page, as follows:

<Jsp: include page = "Relative URL">

<Jsp: param name = "param name" value = "paramvalue"/>

</Jsp: include>

You can also pass parameters when using the jsp: forward action for page Jump, as follows:

<Jsp: forward page = "Relative URL">

<Jsp: param name = "paramname" value = "paramvalue"/>

</Jsp: forward> in this way, parameters can be obtained through request. getParameter (name), which is the same as common form parameters.

(3) set the session and request

The displayed parameters are placed in the session and request to pass the parameters.

Session. setAttribute (name, value );

Request. setAttribute (name, value)

Take the following parameters:

Value = (value className) session. getAttribute (name );

Value = (value className) request. getAttribute (name );

You must have noticed that the type conversion is performed when the parameter is obtained, because the attributes of the objects placed in the session and request are treated as java. lang. if the Object type is not converted, The classcastexception exception is reported when the value is directly paid.

Passing parameters between multiple JSP pages

1. How to pass parameters between multiple JSP pages? Use the JSP built-in scope object session. Using its two methods setAttribute (), getAttribute ()

2. The following example implements the function of passing parameters of the first JSP page to the third page.

3. The Code is as follows: 1.jsp

<Html>

<Form method = get action = 2.jsp>

What's your name <input type = text name = username>

<Input type = submit value = submit>

</Form>

</Html>

4. 2.jsp

<Html>

<Form method = post action = "3.jsp? Pass = 11 ">

<%

String name = request. getParameter ("username ");

Session. setAttribute ("username", name );

%>

Your name is: <% = request. getParameter ("username") %>

<Br> what's your holobby <input type = text name = holobby>

<Input type = submit value = submit>

</Form>

</Html>

5. 3.jsp

<Html>

Your name is: <% = session. getAttribute ("username") %>

<Br>

Your holobby is: <% = request. getParameter ("holobby") %>

<Br>

Your password is: <% = request. getParameter ("pass") %>

<Br>

</Form>

</Html>

Related Article

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.