Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getAttribute() and getParameter()

Tags:

java

jsp

servlets

What is the difference between getAttribute() and getParameter() methods within HttpServletRequest class?

like image 701
priya Avatar asked Mar 09 '11 09:03

priya


People also ask

What is the difference between getAttribute and setAttribute?

getAttribute is used to get values from that object name. setAttribute() sets the data into the database and getAttribute() fetch the data from the database. getAttribute is used to get values from that object name. Setattribute() sets the data into the database and getattribute() fetch the data from database.

What is the return type of getAttribute ()?

The getAttribute() method is declared in the WebElement interface, and it returns the value of the web element's attribute as a string. For attributes having boolean values, the getAttribute() method will return either true or null.

What is getAttribute in Java?

Syntax: public Object getAttribute(String name) Parameters: This method accepts a single parameter name which is the name of the attribute to retrieve.

What is getParameter in Java?

getParameter. java.lang.String getParameter(java.lang.String name) Returns the value of a request parameter as a String , or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.


4 Answers

  • getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. 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. Can be used for any object, not just string.

like image 161
Bozho Avatar answered Oct 22 '22 20:10

Bozho


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
like image 38
krock Avatar answered Oct 22 '22 18:10

krock


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.

like image 34
AVI Avatar answered Oct 22 '22 20:10

AVI


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.

like image 22
oneiros Avatar answered Oct 22 '22 19:10

oneiros