What is the difference between request.getParameter() and request.getAttribute()?
Always do a 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.
Always 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, we have first.jsp page
//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=xyzws and the output on your browser is
From Which Page : first.jsp
Data From Client : xyzws
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.
Most Viewed jsp Faqs
- What is difference between page and pageContext in JSP pages?(6027)
- How to retrieve values from HTML form input elements in JSP?(5132)
- How to config a JSP file in web.xml?(4840)
- How to disable browser caching for a specific JSP?(4543)
- What is the difference between jsp:forward and response.sendRedirect?(3529)
- How to get the current JSP page name?(3341)
- What is the difference between request.getParameter() and request.getAttribute()?(3233)
Most Recent jsp Faqs
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
- What are the request and response implicit objects?
- How to get the current JSP page name?