JSP implicit Object request (2), implicit request

Source: Internet
Author: User

JSP implicit Object request (2), implicit request
III. the information submitted by the request implicit object client through the HTTP request will be encapsulated in the request object by the servlet container. The jsp page program can operate the client request parameters (parameter) and attributes through the request object) and the input stream of the client. The parameter is generally a parameter submitted by the client through the http post or GET method. Generally, the client forwards the parameter from the client or a previous program;
Attribute is mainly a parameter generated by the data generated by the server program, generally not from the client;
(1) The most common parameters passed by the client to the server are form data or parameters attached to the URL. The parameters in the URL refer to the URL "?" The following parameters are called query string parameters, such as http: // localhost/exam. jsp? Name = "name = tomcat" in tomcat ".

(2) reading Single-value parameters with requests
A single-value parameter means that a variable can have at most one value. Use the getParameter () method of the request object to read these parameters. GetParameter () is used to read the parameter value of the specified variable name. The method is defined as public java. lang. String getParameter (java. lang. String name)

The parameter of the method is the variable name of the parameter, and the value of the variable is returned in String form. If no variable is specified in the request object, null is returned.

(3) request scope variable
When two JSP/Servlet programs on the server need to exchange data, the request range variable can be used. The request scope variable is also called the request attribute (attributes). It is an attribute pair similar to "name = value" and consists of the attribute name and attribute value. The attribute value is generally a Java object, it is not a Java basic data type. When Servlet program A passes the data object to Servlet program B, program A calls the request. setAttribute () writes the data object to the scope of the request, and jumps to program B through request forwarding. The request object of program A is forwarded to program B and passed request in program B. getAttribute () reads data objects from the scope of the request.
Request. the setAttribute () method writes a property value object to the request object, or defines a property value object as a request range variable. In fact, it binds the property value object to the request implicit object, the life cycle of the property value object is directly related to the life cycle of the request object. Within the valid range of the implicit object in the current request, the property value object bound to it is also valid. You can use reuqest. the getAttribute () method reads these valid property value objects. When the request object expires, the request property variable bound to the object becomes a garbage object and is recycled.
The variable name of the request range variable can be named using the Java package, for example, com. abc. mis. login. name, com. abc. mis. login. pw and so on, the variable name should be as unique as possible, and should not be the same as the Java/J2EE package name/Class Name

In JSP, besides the request range variables, there are also the range variables for page, session, and application, their basic meaning is to bind the property value object to a JSP implicit object with a lifecycle, so that the property value object has a certain lifecycle, or make the property value object valid within a certain scope. To define a range variable, call the setAttribute () method in the implicit JSP object. to read a range variable, call the getAttribute () method.

1. setAttribute ()/getAttribute () method
Request. setAttribute () is used to write an attribute object to the action range of the request according to the specified name. Its syntax is:

Public void setAttribute (java. lang. String name, java. lang. Object o)


The first parameter is the name of the variable in the action range, and the name must be unique. The second parameter is the property value object.

Request. getAttribute () reads the attribute object of the specified name from the scope of the request. Its syntax is:

Public java. lang. Object getAttribute (java. lang. String name)


The parameter is the variable name of the property value Object. The Object returned by the method is of the Object type. Generally, a forced type conversion is required to restore the original data type of the property value Object.

2. getRequestDispatcher ()
When two Servlet programs use the request range variable to transmit data, they must use the forward jump operation to jump from the first Servlet program A to the second Servlet program B, the implicit request and response objects in program A are automatically forwarded to program B.
Request forwarder (RequestDispatcher) is used to obtain the forwarder of the target resource. The forwarder forwards the request and response objects of the current Servlet program to the target resource and jumps to the target resource to run the program, in this way, the target resource can read the request attribute passed to the previous resource through the request object.

Equest. getRequestDispatcher () is used to return the RequestDispatcher object of the target resource,
The parameter is the URI of the target Web Application resource. You can use relative or absolute paths.
The main methods in RequestDispatcher are:
Public void forward (ServletRequest request, ServletResponse response)
Throws ServletException, java. io. IOException
This method can forward the request and response implicit objects of the current Servlet program to the target resource and jump to the target resource running code. Parameters are the request and response implicit objects of the current Servlet program.
The forward () method is called before the response information is submitted. If the response output buffer has been refreshed before forward () is called, forwarding will cause an exception. The information in the output buffer of the current response object will be cleared before the jump action is executed.

3. removeAttribute ()
This method is used to delete the attribute of the specified name from the scope of the request. Its syntax is:

Public void removeAttribute (String name)
The parameter is the attribute name. Example: request. removeAttribute ("name ");

4. setCharacterEncoding ()
Defines the character encoding standard for the parameter in the request object. For example, if the parameter has Chinese characters, call this method before reading the parameter and set the parameter encoding standard to GB2312, which can solve the problem of Chinese garbled characters when parameters are submitted in POST mode.


What is the implicit object of jsp?

1. The out comes from the java. io. Writer class, which is used to send the output to the client.
2. The request comes from the javax. servlet. http. HttpServletRequest interface. It is associated with every HTTP request.
3. response comes from javax. servlet. http. HttpServletResponse. The response object is used to return the obtained data to the client.
4. pageContext provides a namespace for accessing JSP pages. It also provides access to other JSP implicit objects.
5. The session is from javax. servlet. http. HttpSession. It is used to store client request information, so it is stateful interactive.
6. The application comes from javax. servlet. ServletContext.
7. config comes from ServletConfig, which contains the configuration information of the WEB application where the current JSP/Servlet is located.
8. page comes from the instantiation of the currently accessed JSP page. It actually uses the Servlet converted from JSP.
9. exception is used to capture exceptions thrown by JSP. It is available only when the JSP page attribute isErrorPage = true.

Some implicit objects can be used in jsp to access the attributes in the request.

Request. getAttribute (attribute name)
Or use the EL expression $ (attribute name)

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.