Parameter and attribute use occasions (reproduced from grasslands and trees)

Source: Internet
Author: User

The difference between Attribute and Parameter

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

(2) When the two Web Components are link relationships, the linked component obtains the request parameters through the GetParameter () method.

(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.

Typically use GetParameter for parameters passed on forms and links

Use Request.getattribute ("name") to assign values by Request.setattribute ("name", "Jerry")

This question is the difference between request and session, the request scope is smaller, just a petition, simply say that you are on the page an operation, Request.getparameter () is from the previous page in the URL, form to get parameters, But if a request involves more than one class, the parameters are taken later, Request.setattribute () and Request.getattribute () can be used, but when the result is output, the request ends.

The session can span many pages and can be understood as multiple requests from the client's same IE window. This can be passed between parameters, such as a lot of Web site user login is used.

Can generally use GetParameter to get page parameters ... String...

GetAttribute () can get the object ...

GetParameter can get the parameters such as the page coming in? Id=123 or something like that.

GetAttribute () is commonly used for servlet page pass parameters to JSP

Personal Insights:

When the user passes the value as a link parameter to the next page or serve, it is obtained with getparameter (). such as aa.jsp?id=1; and the submission of the form.

When the user places a value in the request in a property (Request.setattribute ("AA", "tt"), the "AA" attribute name can be arbitrarily taken), with the GetAttribute (attribute name).

The request range is smaller, just a petition, simply a single action on your page, and Request.getparameter () gets the parameters from the URL and form in the previous page. But if a request involves more than one class, the parameters are taken later, Request.setattribute () and Request.getattribute () can be used, but when the result is output, the request ends.

The session can span many pages. The range is greater than request.

Request.getparameter () and Request.getattribute ()
(1) Request.getparameter () is obtained through the implementation of the container to obtain the data passed through similar post,get, Request.setattribute () and getattribute () just flow inside the Web container, Only the request processing phase.
(2) The data passed by the Request.getparameter () method is uploaded from the Web client to the Web server side and represents the HTTP request data. The Request.getparameter () method returns data of type String.
The data passed by the Request.setattribute () and GetAttribute () methods will only exist inside the Web container
Another point is that the HttpServletRequest class has a setattribute () method and no Setparameter () method.
Take an example, if two Web pages are linked, that is, if you want to link to 2.jsp from 1.jsp, the link is 2.jsp can get the request parameters through the GetParameter () method.
If there is a 1.jsp in
<form name= "Form1" method= "Post" action= "2.jsp" >
Please enter your name: <input type= "text" name= "username" >
<input type= "submitted" name= "submit" value= "Submission" >
</form> in 2.jsp by Request.getparameter ("username") method to obtain the request parameter username:
<% String username=request.getparameter ("username"); %>
************************************************************
However, if the two Web is a forwarding relationship, the forwarding destination Web can use the GetAttribute () method to share data in the request scope with the source web, or an example.
With 1.jsp and 2.jsp
1.jsp want to pass the current user name to 2.jsp, how to pass this data? First call the following setattribute () method in 1.jsp:
<%
String Username=request.getparameter ("username");
Request.setattribute ("username", username);
%>
<jsp:forward page= "2.jsp"/>
The user name is obtained through the GetAttribute () method in 2.jsp:
<% string Username= (String) Request.getattribute ("username"); %>

getattribute and the GetParameter the Difference

1.getAttribute is obtained using setattribute in JSP attribute
2.parameter gets the String;attribute is the object
The data passed by the 3.request.getparameter () method is uploaded from the Web client to the Web server side, representing the HTTP request data; Request.setattribute () and getattribute () The data passed by the method will only exist inside the Web container and be shared between Web components that have a forwarding relationship. The Request.getattribute () method returns the object that exists in the request scope, and the Request.getparameter () method is to get the data submitted by HTTP.

What is the difference between getparameter and getattribute in JSP?
-To tell the truth, this problem I also puzzled for a long time, I also know how to use, but what is the difference, I am not very clear, and later found a lot of information to understand. Yesterday, a friend asked me this question, I thought I was also confused, so I put this issue, so that the same confused friend doubts.
--getparameter gets all of the string types. or ahttp://a.jsp?id=123123, or a form submits past data.
The--getattribute can be an object.
--getparameter () is the parameter value that gets the Post/get pass;
--getattribute () is the data value in the object container;
--getparameter: Used for client redirection, that is, clicking on a link or submitting a simultaneous value, which is used to receive data when using a form or URL redirection value.
--getattribute: When used for server end multiplicity orientation, the forward function is used in Sevlet, or mapping.findforward is used in struts. GetAttribute can only receive the value passed by the program with SetAttribute.
--getparameter () is the parameter value that gets the Post/get pass;
--getattribute () is the value that gets the session;
In addition, you can send a Receive object with Setattribute,getattribute. And GetParameter obviously can only pass strings.
SetAttribute is the application server put this object in the corresponding piece of memory in the page, when your page server redirects to another page, the application server will copy the memory of the other page corresponding to the memory. This way getattribute can get the value you set, of course, this method can be transmitted to the object. The session is the same, except that the object's life cycle in memory is different.
GetParameter is just the application server when you analyze the text of the request page you sent up, get the value that you set on the form or URL redirection.

GetParameter returns a string that reads the value from the submitted form;
GetAttribute return is object, need to convert, can be set to any object setattribute, use is flexible, can be used at any time;

Personal Opinion:
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 page
Http://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
<form method=post>
<form method=get>
<a href= "1.jsp?id=1" >ok</a>
Only Accept Java.lang.String
In other words, string hotel_id = Request.getparameter ("hotel_id");
Request.getattribute
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);
Only Accept Java.lang.Object
That is to say list ret = (list) request.getattribute ("ret");
If you use JSP only, you can't use Request.getattribute ()

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

The general Web application, as Chenshaizi (Chen Shaobin) said, is basically based on the post-mode transfer, with the GetParameter value. 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 the parameter value set by SetAttribute, which is equivalent to using getattribute to get a parameter that is defined by itself, not 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 () in the request and session, but the scope is different, before taking it must be stored somewhere, this stuff can access the object

Oh
http://community.csdn.net/Expert/topic/4763/4763471.xml?temp=.1793177
See the back of the temp=.1793177? Request.getattribute ("temp") to get the string ". 1793177",
Before GetAttribute (), you must be in the page or logic with Serattribute () to use, it is clear, I no longer say ha

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.

Parameter and attribute use occasions (reproduced from grasslands and trees)

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.