Difference between getattribute and getparameter

Source: Internet
Author: User
Tags what parameter

1. getattribute is the attribute set by setattribute in JSP.
2. What parameter gets is string; what attribute gets is object.
3. request. the data transmitted by the getparameter () method is transmitted from the Web Client to the Web server, representing the HTTP request data; request. the data transmitted by the setattribute () and getattribute () methods only exists in the Web Container and is shared among the Web Components with forwarding relationships. That is, the request. getattribute () method returns objects in the request range, while the request. getparameter () method obtains the data submitted by HTTP.

What is the difference between getparameter and getattribute in JSP?
-- To be honest, I was confused for a long time and I knew how to use it. But I was not very clear about the difference. I found a lot of information to understand it. Another friend asked me this question yesterday. I thought I was also confused. So I posted this question to my confused friends.
-- Getparameter is of the string type. Or a http://a.jsp? Id = 123 in 123, or the data submitted by a form.
-- Getattribute can be an object.
-- Getparameter () is used to obtain the parameter value passed by post/get;
-- Getattribute () is used to obtain the data value in the object container;
-- Getparameter: used for client redirection, that is, when a link is clicked or a button is submitted for value transfer, it is used to receive data when a form or URL is redirected for value transfer.
-- Getattribute: used for server redirection, that is, the forward function is used in the sevlet, or the mapping. findforward function is used in struts. Getattribute can only receive Values Transmitted by the program using setattribute.
-- Getparameter () is used to obtain the parameter value passed by post/get;
-- Getattribute () is the value of the session;
In addition, you can use setattribute and getattribute to send the receiving object, while getparameter can only transmit strings.
Setattribute indicates that the application server places this object in the memory of the corresponding page. When your Page Server redirects to another page, the Application Server copies the memory to the memory corresponding to another page. In this way, getattribute can get the value you set. Of course, this method can be used to transmit objects. The same is true for sessions, but the lifecycle of objects in the memory is different.
Getparameter is only used by the application server to analyze the text of the Request page you sent, and obtain the value you set when you set it to form or URL redirection.

Getparameter returns a string to read the value in the submitted form;
Getattribute returns an object that requires conversion. You can use setattribute to set it to any object. It is flexible to use and can be used at any time;
 

In many cases, some data is taken to the forwarded page for processing during request forwarding. In this case, you can use the setattribute () method of the request object to set the data to the memory in the request range. The data forwarding method is as follows:
-

Manage attributes in scope
In many cases, some data is taken to the forwarded page for processing during request forwarding. In this case, you can use the setattribute () method of the request object to set the data to the memory in the request range.
The data forwarding method is as follows:
Request. setattribute ("key", object );
The method is described as follows.
The parameter key is of the string type. This key is used to retrieve data on the forwarded page. The parameter object is a key value of the object type, indicating the data to be stored in the request range.
Use the following method to obtain the forwarded data:
Request. getattribute (string name );
The method is described as follows.
The parameter name indicates the key name.
Use the setattribute ("name", OBJ) method of the request object on the page to set the data OBJ to the request range. After the request is forwarded, use the getattribute ("name") method to obtain the data obj.
This example uses the setattribute () method to set data, and then obtains the set data after the request is forwarded. The procedure for program development is as follows.
(1) create a page named index. jsp, which uses the request object to call the setattribute () method to store data. The program code is as follows:
Key code of index. jsp:
<%
String uname = "Tom ";
String usex = "man ";
String uage = "26 ";
String ucity = "Changchun ";
String utel = "13999999 ";
Request. setattribute ("name", unname); // store the unname value in the request object
Request. setattribute ("sex", usex); // store the usex value in the request object
Request. setattribute ("Age", uage); // store the uage value in the request object
Request. setattribute ("city", ucity); // store the "city" value in the request object
Request. setattribute ("tel", utel); // store the utel value in the request object
%>
<JSP: Forward page = "getattribute. jsp"/>
The code is described as follows.
The JSP Action Element <JSP: Forward page = "getattribute. jsp"/> must be used at the end of the program to forward requests to the getattribute. jsp page. Therefore, the request forwarding page setting data can be obtained on the getattribute. jsp page.
(2) create a page named getattribute. jsp. The main function of this page is to call the getattribute () method of the request object to display data. The program code is as follows:
Key code of getattribute. jsp:
<Table border = "1">
<Tr>
<TD> name: </TD>
<TD> <% = request. getattribute ("name") %> </TD> <! -- Get the value of the name object through the getattribute () method -->
</Tr>
<Tr>
<TD> Gender: </TD>
<TD> <% = request. getattribute ("sex") %> </TD> <! -- Get the sex object value through the getattribute () method -->
</Tr>
<Tr>
<TD> Age: </TD>
<TD> <% = request. getattribute ("Age") %> </TD> <! -- Get the value of the age object through the getattribute () method -->
</Tr>
<Tr>
<TD> City: </TD>
<TD> <% = request. getattribute ("city") %> </TD> <! -- Get the value of the city object through the getattribute () method -->
</Tr>
<Tr>
<TD> Tel: </TD>
<TD> <% = request. getattribute ("tel") %> </TD> <! -- Get the value of the Tel object through the getattribute () method -->
</Tr>
</Table>
Note:
Data retrieved from the memory within the request range can only be taken on the page after the request is forwarded, but not on other pages.
(3) After the program is released, start the Tomcat server and enter the following address in the address bar of the browser: http: // 127. 0.0.1: 8080/02, as shown in result 1.
Figure 1 get data using the getattribute () method
Complete code:
Complete index. jsp code:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
String uname = "city Wolf ";
String usex = "male ";
String uage = "26 years old ";
String ucity = "Changchun ";
String utel = "12345678987 ";
Request. setattribute ("name", uname );
Request. setattribute ("sex", usex );
Request. setattribute ("Age", uage );
Request. setattribute ("city", ucity );
Request. setattribute ("tel", utel );
%>
<JSP: Forward page = "getattribute. jsp"/>
Complete getattribute. JSP code:
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> untitled document </title>
</Head>
<Body>
<Table width = "222" border = "1" cellpadding = "1" cellspacing = "1" bordercolor = "# ffffff" bgcolor = "#000000">
<Tr bgcolor = "# ffffff">
& Lt; TD width = "101" & gt; Name: & lt;/TD & gt;
<TD width = "108"> <% = request. getattribute ("name") %> </TD>
</Tr>
<Tr bgcolor = "# ffffff">
<TD> Gender: </TD>
<TD> <% = request. getattribute ("sex") %> </TD>
</Tr>
<Tr bgcolor = "# ffffff">
<TD> Age: </TD>
<TD> <% = request. getattribute ("Age") %> </TD>
</Tr>
<Tr bgcolor = "# ffffff">
<TD> City: </TD>
<TD> <% = request. getattribute ("city") %> </TD>
</Tr>
<Tr bgcolor = "# ffffff">
<TD> Tel: </TD>
<TD> <% = request. getattribute ("tel") %> </TD>
</Tr>
</Table>
</Body>
</Html>
Complete WEB-INF/Web. XML code:
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>

These two methods are often used when you jump to a servlet or JSP page. But what is the difference. Response. sendredirect re-sends the request to the client, so that the client re-organizes the request and then accesses the relocated URL. Therefore, all the attributes in the previously assembled request are lost. However, if you are using a session, the values in the session are still there.
Request. getrequestdispatcher ("Servlet/platformlist. jsp"). Forward (request, response); redirects within the server with request and response. The attribute in the previously set request is also in. Therefore, you can use the attribute or session of the request to transmit values.
However, I encountered a request during a specific experiment. getrequestdispatcher ("Servlet/platformlist. JSP "). forward (request, response); you can jump to the past, but you cannot get the attribute in the request. In addition, since the use of getservletcontext (). getrequestdispatcher to redirect, when the JSP page jumps back to the servlet, the address does not need to carry the servlet, And you can simply use xxxservlet. I am surprised that there is no time for experiment discussion. Therefore, record it first. At present, the function is implemented.

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.