Connection and difference between request. setattribute (), session. setattribute (), and request. getparameter ()

Source: Internet
Author: User

1. session. setattribute () and session. getattribute () are used in pairs. The scope is used throughout the session and all pages use the data.

2. The request. setattribute () and request. getattribute () pairs are used between the request and the requested page. Request. setattribute () is used only when the next forward of this action needs to be used; request. getattribute () indicates that the set attribute is obtained from the request range. You must set the attribute first to get the attribute through getattribute. The object type is set and obtained. In fact, the object name and value in the Form Control are stored in a hash table. Therefore, the name of the object is given here and the corresponding value is found in the hash table. The parameters of setattribute () are string and object.

3. Request. getparameter () indicates the receiving parameter, which is the parameter submitted on the page. Including: Form submission parameters, URL rewriting (that is, XXX? Id = ID in 1), so there is no way to set the parameter (no setparameter (), and the receiving parameter returns a string type instead of an object.

Example:

Session. setattribute ("kindresult", result); // The result is a stringbuffer object.

Response. sendredirect ("../manage_kind.jsp ");


In manage_kind.jsp:

<%
Stringbuffer kindresult = new stringbuffer ();
Kindresult = (stringbuffer) Session. getattribute ("kindresult ");
%>

<% = Kindresult %>

######################################## ##########################

I used request. setattribute () in servlet to store information.
Syntax: request. setattribute ("user", "1234 ");
Then response. sendredirect ("/Hello. jsp ");
However, in my hello. jsp request. getattribute ("user ");
The returned value is null. Why didn't I get the string "1234 "?

Note that sendredirect cannot pass the request object. When request. setattribute is used, redirect cannot be used but forward. That is, the request is forwarded rather than redirected.

You can use getservletcontext (). getrequestdispatcher ("/hello. JSP "). forward (request, response) to hello. JSP page, which does not realize hello for the client. the JSP page responds to it.
The request object is the same as the response object. Of course, your parameters can be passed.

You use response. sendredirect ("/Hello. jsp"); after you go to hello. jsp, the request object is created and your attribute value is lost. However, you can use session instead of request.
Session. setattribute ("user"," 1234 ");
Session. getattribute ("user ");

######################################## ######################################## #####

4. Difference between request. getparameter () and request. getattribute ()

(1) Request. getparameter () is achieved through the implementation of the container to obtain data transmitted through methods such as post and get, request. setattribute () and getattribute () are transferred only within the Web Container, but only in the request processing phase.

(2) the data transmitted by the request. getparameter () method is transmitted from the Web Client to the Web server, representing the HTTP request data. The request. getparameter () method returns string-type data.

The data transmitted by the request. setattribute () and getattribute () methods only exists in the Web container.

Another point is that the httpservletrequest class has the setattribute () method instead of the setparameter () method.

Let's take an example. If two web pages are in a link, they are starting from 1. JSP link 2. in JSP, the link is 2. JSP can obtain request parameters through the getparameter () method.

Suppose that 1. jsp contains

<Form name = "form1" method = "Post" Action = "2.jsp">
Enter the User name: <input type = "text" name = "username">
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>

The request parameter username is obtained through the request. getparameter ("username") method in 2. jsp:

<% String username = request. getparameter ("username"); %>

However, if the two web pages are in a forwarding relationship, you can use the getattribute () method to share the data within the request range with the source web. Let's take an example.

1. jsp and 2.jsp

1. jsp wants to pass the current username to 2.jsp. How can this data be transmitted? Call the following setattribute () method in 1.jsp:

<%
String username = request. getparameter ("username ");
Request. setattribute ("username", username );
%>

<JSP: Forward page = "2.jsp"/>

In 2. jsp, use the getattribute () method to obtain the username:
<% String username = (string) request. getattribute ("username"); %>

5. Request. getattribute () and request. setattribute ()

Request. getattribute ("nameofobj") to obtain the value of the control in the form of JSP page 1. In fact, the object name and value in the Form Control are stored in a hash table. Therefore, the name of the object is given here and the corresponding value is found in the hash table.

The request is used to pass values between different pages. when setattribute (Position, nameofobj) is set. JSP to B. after a jsp request is passed, the request will lose the scope of its function. setattribute (). When session. setattribute () is used, this value is always stored in a process.

P.s: JavaScript and JSP cannot pass values to each other because JavaScript runs on the client and JSP runs on the server. If you want to transfer parameters between them, you can set a hidden control in JSP and use its value in combination with the preceding usage to pass the required value.

Connection and difference between request. setattribute (), session. setattribute (), and request. getparameter ()

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.