What is the difference between request.getParameter() and request.getAttribute()?
Ok, getParameter() first:
Code:
<html>
<body>
<form name="testForm" method="post" action="testJSP.jsp">
<input type="text" name="testParam" value="Hello Param">
<input type="submit">
</form>
</body>
</html>
Ok so when you click the submit button on this HTML page the form will be posted to testJSP.jsp
Code:
<!-- Code for testJSP.jsp -->
<%
String sValue = request.getParameter("testParam");
%>
<html>
<body>
<%= sValue %>
</body>
</html>
request.getParameter("testParam") will get the value from the posted form (named testForm) of the input box named testParam which is "Hello Param". It will then print it out, so you should see "Hello 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.
Now for 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.
Code:
// Your servlet code
public class SomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Processes requests for both HTTP
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("http://www.website.com/yourResource"); //You could give a relative URL, I'm just using absolute for a clear example.
Object object = new Object(); // You can use any type of object you like here, Strings, Custom objects, in fact any object.
request.setAttribute("ObjectName", object);
rd.forward(request, response);
}
}
Now when you call rd.forward() the request will be forwarded to the resource specified ie: in this case "http://www.website.com/yourResource"
Now in this case yourResource is a Servlet, here is the code
Code:
public class YourResourceServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Processes requests for both HTTP
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Retrieve the object stored in the request.
Object myDispatchedObject = request.getAttribute("ObjectName");
}
}
Now you have the object that you passed from resource to resource on the server side.
Ok, getParameter() first:
Code:
<html>
<body>
<form name="testForm" method="post" action="testJSP.jsp">
<input type="text" name="testParam" value="Hello Param">
<input type="submit">
</form>
</body>
</html>
Ok so when you click the submit button on this HTML page the form will be posted to testJSP.jsp
Code:
<!-- Code for testJSP.jsp -->
<%
String sValue = request.getParameter("testParam");
%>
<html>
<body>
<%= sValue %>
</body>
</html>
request.getParameter("testParam") will get the value from the posted form (named testForm) of the input box named testParam which is "Hello Param". It will then print it out, so you should see "Hello 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.
Now for 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.
Code:
// Your servlet code
public class SomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP
POST method.* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Processes requests for both HTTP
GET and POST methods.* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("http://www.website.com/yourResource"); //You could give a relative URL, I'm just using absolute for a clear example.
Object object = new Object(); // You can use any type of object you like here, Strings, Custom objects, in fact any object.
request.setAttribute("ObjectName", object);
rd.forward(request, response);
}
}
Now when you call rd.forward() the request will be forwarded to the resource specified ie: in this case "http://www.website.com/yourResource"
Now in this case yourResource is a Servlet, here is the code
Code:
public class YourResourceServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP
POST method.* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Processes requests for both HTTP
GET and POST methods.* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Retrieve the object stored in the request.
Object myDispatchedObject = request.getAttribute("ObjectName");
}
}
Now you have the object that you passed from resource to resource on the server side.
what the heck!! why the hell did you turn this page into a TECHNICAL blog!! What will happen to dumbos like me, huh? Not Cool man, Not at all Cool :)
ReplyDeleteGreat breakdown of request handling in Java servlets! While you're refining backend skills, don’t forget to maintain your ride too—reliable services from this tyre shop keep you moving both on the road and in your code.
ReplyDeleteThis example clearly explains the difference between request.getParameter and request.getAttribute—very helpful for beginners! If you’re learning on the go and need reliable mobile repair in Dubai, this service can keep your device running smoothly.
ReplyDelete