JSP implicit object request (2)

Source: Internet
Author: User
Tags http post

Three. Request implied objectThe information submitted by the client via HTTP request is encapsulated by the servlet container in the Request object, and the JSP page program can manipulate the data such as client request parameters (parameter), attributes (attribute) and client input stream through the request object;Parameter parameters are generally submitted by the client through the HTTP post or get method of the parameters, the general visitor from the client or have a previous program forwarding;
The attribute property is mainly a parameter generated by the data generated by the server program, usually not from the client;
(1) The most common parameter that the client passes to the server is the form data or the parameter appended to the URL, where the parameter in the URL refers to the url "? The arguments that follow are called query string parameters, such as "Name=tomcat" in Http://localhost/exam.jsp?name=tomcat.

(2) Read single-value parameters with request
The so-called single-valued parameter refers to a variable that has at most one value. These parameters are read with the GetParameter () method of the Request object. GetParameter () is used to read the parameter value of the specified variable name, defined by: Public java.lang.String getparameter (java.lang.String name)

The parameter of the method is the variable name of the argument, and the value of the variable is returned as a string. If the request object does not have a specified variable, NULL is returned.

(3) Request scope variable
When exchanging data between the two Jsp/servlet programs on the server side, the request scoped variable can be implemented. The request scope variable is also called the request attribute (attributes), which is a property pair similar to "Name=value", consisting of a property name and a property value, which is typically a Java object, not a Java basic data type data. When a servlet program a wants to pass a data object to the Servlet program B, program a writes the data object to the request scope by calling Request.setattribute () and jumps to program B through request forwarding. The request object of program A is forwarded to program B, and the data object is read from the request scope in program B through Request.getattribute ().
Using the Request.setattribute () method to write a property value object to the Request object, or to define a property value object as the request scoped variable, is actually binding the property value object to the request implication object. The life cycle of the property value object itself is directly related to the life cycle of the request object, and the property value object bound to it is valid within the scope of the current request implication object, and can be read by the Reuqest.getattribute () method. When the request object's lifetime ends, the request attribute variable that is bound to it becomes a garbage object and is recycled.
The variable name of the request scope variable can be named in the Java package, for example: Com.abc.mis.login.name, COM.ABC.MIS.LOGIN.PW, and so on, with the variable name as unique as possible, and not with the JAVA/J2EE package name/ Same class name

In JSP, in addition to the request scope variable, there are page, session and application scope variables, their basic meaning is to attribute value object and a life-cycle jsp implicit object binding, so that the property value object has a certain life cycle, Or, make the property value object valid within a certain scope. Defining a scope variable is generally called the setattribute () method in the jsp implicit object, and the read scoped variable is generally called the GetAttribute () method.

1, SetAttribute ()/getattribute () method
Request.setattribute () is used to write a Property object to the request scope by the specified name, with the following syntax:

public void SetAttribute (java.lang.String name, Java.lang.Object o)


The first parameter is the scope variable name, the name is unique, and the second parameter is the property value object.

Request.getattribute () reads the Property object of the specified name from the request scope, and its syntax is:

Public Java.lang.Object getattribute (java.lang.String name)


The parameter is the variable name of the property value object, which is the object type, which is typically forced type conversion, restoring the original data type of the property value object.

2.getRequestDispatcher ()
Two servlet programs to use the request scope variable to pass data, to use a forward jump operation to jump from the first servlet program A to the second servlet program B, Jump, The request and response implied objects in program A are automatically forwarded to program B.
The function of the request forwarder (RequestDispatcher) is to obtain a forwarder for the target resource, forward the request and response object of the current servlet program to the target resource through a forwarder, and jump to the target resource to run the program, so that The request object can be used by the target resource to read the Request property passed to it by the previous resource.

Equest. The function of Getrequestdispatcher () is to return the RequestDispatcher object of the target resource,
The parameter is the URI of the current web App target resource, and you can use a relative or absolute path.
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 implied object of the current servlet program to the target resource and jump to the target resource running code. The formal parameter is the request and response implied object of the current servlet program.
The forward () method is called before the response information is submitted. If the response output buffer has been flushed before calling forward (), forwarding throws an exception. The information in the current response object output buffer will be emptied before the jump action is performed.

3, RemoveAttribute ()
The function of this method is to remove the property of the specified name from the request scope, and its syntax is:

public void RemoveAttribute (String name)
The formal parameter is the property name. For example: Request.removeattribute ("name");

4, Setcharacterencoding ()
Defines the character encoding standard for the parameter parameter in the Request object. For example, if the parameter parameter is in Chinese, this method is called before reading the parameter, and the encoding standard of the set parameter is GB2312, which resolves the Chinese garbled problem of submitting the parameter by post.

JSP implicit object request (2)

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.