What is the difference between getAttribute()
and getParameter()
methods within HttpServletRequest
class?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
getParameter
- Is used for getting the information you need from the Client's HTML pagegetAttribute
- This is used for getting the parameters set previously in another or the same JSP or Servlet page.Basically, if you are forwarding or just going from one jsp/servlet to another one, there is no way to have the information you want unless you choose to put them in an Object and use the set-attribute to store in a Session variable.
Using getAttribute, you can retrieve the Session variable.
request.getParameter()
We use
request.getParameter()
to extract request parameters (i.e. data sent by posting a html form ). Therequest.getParameter()
always returnsString
value and the data come from client.request.getAttribute()
We use
request.getAttribute()
to get an object added to the request scope on the server side i.e. usingrequest.setAttribute()
. You can add any type of object you like here,Strings
, Custom objects, in fact any object. You add the attribute to the request and forward the request to another resource, the client does not know about this. So all the code handling this would typically be in JSP/servlets. You can userequest.setAttribute()
to add extra-information and forward/redirect the current request to another resource.For example,consider about first.jsp,
and second.jsp:
From your browser, run first.jsp?CLIENT=you and the output on your browser is
The basic difference between
getAttribute()
andgetParameter()
is that the first method extracts a (serialized) Java object and the other provides a String value. For both cases a name is given so that its value (be it string or a java bean) can be looked up and extracted.getParameter()
returns http request parameters. Those passed from the client to the server. For examplehttp://example.com/servlet?parameter=1
. Can only returnString
getAttribute()
is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP. Can be used for any object, not just string.It is crucial to know that attributes are not parameters.
The return type for attributes is an Object, whereas the return type for a parameter is a String. When calling the
getAttribute(String name)
method, bear in mind that the attributes must be cast.Additionally, there is no servlet specific attributes, and there are no session parameters.
This post is written with the purpose to connect on @Bozho's response, as additional information that can be useful for other people.
from http://www.coderanch.com/t/361868/Servlets/java/request-getParameter-request-getAttribute
-getParameter() :
request.getParameter("testParam")
will get the value from the posted form of the input box named "testParam" which is "Client param". It will then print it out, so you should see "Client Param" on the screen. So request.getParameter() will retrieve a value that the client has submitted. You will get the value on the server side.-getAttribute() :
request.getAttribute()
, this is all done server side. YOU add the attribute to the request and YOU submit the request to another resource, the client does not know about this. So all the code handling this would typically be in servlets.getAttribute always return object.