The difference between GetParameter and getattribute

Source: Internet
Author: User
Tags html header

Request.getattribute (): Is the value of the variable set at request, with Request.setattribute ("name", "Your Own value"), to set the value,
Request.getparameter (): Extract the parameters sent over such as: this pagehttp://community.csdn.net/Expert/topic/4633/4633804.xml?temp=.3488123
Request.getparameter ("temp") = = ". 3488123"
Request.getparameter
is used to accept parameters from the Get method or the Post method
<formMethod=post>
<formMethod=get>
<ahref= "1.jsp?id=1" >ok</a>
Only Accept Java.lang.String
Which means stringhotel_id= request.getparameter ("hotel_id");   
     &NBSP;REQUEST.GETATTRIBUTE&NBSP;&NBSP;
   is used to accept a variable or action from a servlet (in fact the action is a special servlet)   
   in Action, Request.setattribute (" ret ", ret);   
   can only accept java.lang.object  
  ret       =        (List) request.getattribute ("ret");   
   If you use JSP only, you don't have Request.getattribute ()

The difference between Request.getattribute () and Request.getparameter () is that Request.getattribute () obtains the object type,

and Request.getparameter () gets the string type
The general Web application, like Chenshaizi (Chen Shaobin), is basically a post-based delivery, with GetParameter

Values are taken. For their own control, can be achieved through Request.setattribute and getattribute worthy of delivery.
for the application of the structs framework, getattribute use a little more, the other basically with GetParameter

My understanding:
Session.getattribute (); Get session
Request.getparameter (); get parameter

1.getParameter can get the parameter values that the client sends to the server side.
getattribute can get a parameter value set by SetAttribute, which is equivalent to using getattribute to get a defined

parameters, rather than the parameters obtained from the client.
2.getParameter can only pass variables of type string, getattribute can pass vectors.


GetParameter (), get the value of the form getattribute () Get the value of session

Getparameternames () gets an array of parameters in the form or URL
Getattributenames (): Returns the name of all properties of the request object, which is an instance of a enumeration (enum) class

According to the upstairs, is not the value of getparameter () if the next time you do not commit or save it, the next redirect will not be?
: Yes, I understand.
GetAttribute () The attribute value to be obtained because there is a session, so the redirect can still be removed?
GetAttribute () is in the request and session, but the scope is different, before taking it must be stored somewhere, this

The object can be accessed by Dongdong

http://community.csdn.net/Expert/topic/4763/4763471.xml?temp=.1793177
See the back of the temp=.1793177? Request.getattribute ("temp") to get the character.

String ". 1793177",
Before GetAttribute (), you must be in the page or logic with Serattribute () to use, it is clear that I no longer

That's it, huh?


parameter is HTML-like checkbox TextField password Radio ... The value
GetAttribute is made in JSP with SetAttribute set attribute

And .....
Parameter got a string.
Attribute got an object.

Personal experience: When using forward tags and request.getRequestDispatcher.forward (), you can use SetAttribute and getattribute, Both form submissions and hyperlink submissions can read only the value associated with the Name property, using GetParameter (), Reponse.sendrediretion ("Myjsp.jsp?username=luocong"), This can also be extracted with getparameter ("username").

The Get method passes the user's data through a URL request, connecting the field names in the form with their contents, paired strings, and placing the URL of the program referred to by the Action property, such as [URL]HTTP://WWW.MDM.COM/TEST.ASP?NAME=ASD &password=sad[/url], the data will be displayed directly on the URL, just as the user clicks on a link, and the Post method uses the HTTP post mechanism to place the name of each field in the form with its contents in the HTML header (header) Sent to the server by the Action property can refer to the program processing, the program through the standard input (stdin) method, the form of data read out and processed

Request.setattribute cannot be submitted with a form, the request for a set value, and requested by the form requests are not one request, not generated at the same time.

The difference between the GetParameter and GetAttribute methods of request

Httpservletrequest.getparameter ("modelname"); Can you get the modelobject you want? After testing, the discovery is not possible. Then think about it, the other reason is quite simple, when the two Web components are a forwarding relationship, the forwarding source will share data in the request scope first with setattribute to put the data into the HttpServletRequest object, and then forward the target through GetAttribute method to obtain the data to be shared. And MVC uses the forwarding between the Web Components! Why didn't you think of it when you were so stupid?

Here's a look at the differences between GetParameter and GetAttribute and their respective ranges of use.

(1) The HttpServletRequest class has a setattribute () method and no Setparameter () method

(2) When the links between two Web components are linked, the linked component obtains the request parameters through the GetParameter () method, for example, assuming the link relationship between welcome.jsp and authenticate.jsp, the following code is in welcome.jsp:
<a href= "Authenticate.jsp?username=wolf" >authenticate.jsp </a>
Or:
<form name= "Form1" method= "Post" action= "authenticate.jsp" > Please enter user name: <input type= "text" name= "username" > & Lt;input type= "Submit" name= "Submit" value= "Submission" > </form>
The Request.getparameter ("username") method is used in authenticate.jsp to obtain the request parameter username:
<% String username=request.getparameter ("username"); %>

(3) When a forwarding relationship is between two Web components, the forwarding target component shares the data in the request scope with the forwarding source component through the GetAttribute () method.

Assume that there is a forwarding relationship between authenticate.jsp and hello.jsp. Authenticate.jsp want to pass the current user name to hello.jsp, how to pass this data? First call the SetAttribute () method in authenticate.jsp:
<% String username=request.getparameter ("username"); Request.setattribute ("username", username); %>
<jsp:forward page= "hello.jsp"/>
Use the GetAttribute () method to get the user name in hello.jsp:
<% string Username= (String) Request.getattribute ("username"); %> Hello: <%=username%>
From a deeper level of consideration, the data passed by the Request.getparameter () method is uploaded from the Web client to the Web server side, representing the HTTP request data. The Request.getparameter () method returns data of type String.
The data passed by the Request.setattribute () and GetAttribute () methods only exist inside the Web container and are shared between Web components that have a forwarding relationship. These two methods enable you to set shared data of type object.
Request.getparameter () is obtained through the implementation of the container to obtain the data passed through similar post,get.

Request.setattribute () and getattribute () only flow inside the Web container, only the request processing phase.

GetAttribute is the return object, GetParameter returns the string Overall: The Request.getattribute () method returns the objects that exist within the request scope, and the Request.getparameter () method is to get the data submitted by HTTP.

The difference between GetParameter and getattribute

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.