Difference between getAttribute() and getParameter()
Asked Answered
R

10

336

What are the differences between the getAttribute() and the getParameter() methods in HttpServletRequest?

Romano answered 9/3, 2011 at 9:33 Comment(3)
In what class/package/API? Did you try reading the JavaDocs for the relevant methods?Leannleanna
Related: How to servlets work? Application/Session/Request scopes?Capitalism
attribute is only for http request that helps servlets work togetherAnse
I
378
  • getParameter() returns http request parameters. These are passed from the client to the server. For example http://example.com/servlet?parameter=1. They can only return String.

  • 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. These can be used for any object, not just string.

Increasing answered 9/3, 2011 at 9:37 Comment(3)
I suppose when you use something like ${attrName} in a jsp page, those are the attributes you set using request.getSession().setAttribute()?Ie
Since request attribute are stored on server side, can I set request attribute for a inter-server post-redirect-get request? I want to send information from one server to another server in JSF through an attribute in redirect request. I am able to send via a POST request as of now.Labradorite
In this case, "Parameter" seems to be synonymous with "query string"Taskmaster
G
59

Generally, a parameter is a string value that is most commonly known for being sent from the client to the server (e.g. a form post) and retrieved from the servlet request. The frustrating exception to this is ServletContext initial parameters which are string parameters that are configured in web.xml and exist on the server.

An attribute is a server variable that exists within a specified scope i.e.:

  • application, available for the life of the entire application
  • session, available for the life of the session
  • request, only available for the life of the request
  • page (JSP only), available for the current JSP page only
Griffe answered 9/3, 2011 at 9:39 Comment(2)
"Generally, a parameter is a string value" Other than strings what could we be sending?Neoclassicism
^Objects as in the case of request.setAttributeKufic
S
54

request.getParameter()

We use request.getParameter() to extract request parameters (i.e. data sent by posting a html form ). The request.getParameter() always returns String 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. using request.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 use request.setAttribute() to add extra-information and forward/redirect the current request to another resource.

For example,consider about first.jsp,

//First Page : first.jsp
<%@ page import="java.util.*" import="java.io.*"%>
<% request.setAttribute("PAGE", "first.jsp");%>
<jsp:forward page="/second.jsp"/>

and second.jsp:

<%@ page import="java.util.*" import="java.io.*"%>
From Which Page : <%=request.getAttribute("PAGE")%><br>
Data From Client : <%=request.getParameter("CLIENT")%>

From your browser, run first.jsp?CLIENT=you and the output on your browser is

From Which Page : *first.jsp*
Data From Client : you

The basic difference between getAttribute() and getParameter() 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.

Sulfaguanidine answered 15/12, 2015 at 17:16 Comment(2)
This was a great example. This should be the accepted answer. The accepted answer is correct but for new comers an example clarified everything. Thank you.Appellant
Great answer. Maybe worth adding that scope of attributes come into play when you intercept a client's request server side and append some additional information to the request.Kristlekristo
H
28

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.

Hazan answered 28/11, 2012 at 19:26 Comment(0)
H
11

The difference between getAttribute and getParameter is that getParameter will return the value of a parameter that was submitted by an HTML form or that was included in a query string. getAttribute returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource (JSP / Servlet). So before you forward the request you can set an attribute which will be available to the next resource.

Haemolysis answered 27/2, 2013 at 18:27 Comment(0)
R
6

-getParameter() :

<html>
    <body>
        <form name="testForm" method="post" action="testJSP.jsp">
            <input type="text" name="testParam" value="ClientParam">
            <input type="submit">
        </form>
    </body>
</html>

<html>
    <body>
    <%
    String sValue = request.getParameter("testParam");
    %>
    <%= sValue %>
    </body>
</html>

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.

Rover answered 1/8, 2014 at 6:51 Comment(0)
R
4

getParameter - Is used for getting the information you need from the Client's HTML page

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

Radiative answered 24/7, 2014 at 20:53 Comment(0)
I
4

from http://www.coderanch.com/t/361868/Servlets/java/request-getParameter-request-getAttribute

A "parameter" is a name/value pair sent from the client to the server - typically, from an HTML form. Parameters can only have String values. Sometimes (e.g. using a GET request) you will see these encoded directly into the URL (after the ?, each in the form name=value, and each pair separated by an &). Other times, they are included in the body of the request, when using methods such as POST.

An "attribute" is a server-local storage mechanism - nothing stored in scoped attribues is ever transmitted outside the server unless you explicitly make that happen. Attributes have String names, but store Object values. Note that attributes are specific to Java (they store Java Objects), while parameters are platform-independent (they are only formatted strings composed of generic bytes).

There are four scopes of attributes in total: "page" (for JSPs and tag files only), "request" (limited to the current client's request, destroyed after request is completed), "session" (stored in the client's session, invalidated after the session is terminated), "application" (exist for all components to access during the entire deployed lifetime of your application).

The bottom line is: use parameters when obtaining data from the client, use scoped attributes when storing objects on the server for use internally by your application only.

Incognito answered 1/3, 2016 at 5:49 Comment(0)
T
4

Another case when you should use .getParameter() is when forwarding with parameters in jsp:

<jsp:forward page="destination.jsp">
    <jsp:param name="userName" value="hamid"/>
</jsp:forward>

In destination.jsp, you can access userName like this:

request.getParameter("userName")
Tupungato answered 8/8, 2017 at 8:3 Comment(0)
H
-8

Basic difference between getAttribute() and getParameter() is the return type.

java.lang.Object getAttribute(java.lang.String name)
java.lang.String getParameter(java.lang.String name)
Happygolucky answered 5/9, 2014 at 15:37 Comment(1)
you are not rigt about this.React

© 2022 - 2024 — McMap. All rights reserved.