-
July 27th, 2005, 02:48 AM
#1
Difference between request.getParameter() and request.getAttribute()
What is the difference between request.getParameter() and request.getAttribute()?
I too have another doubt in session. What is the difference between session.getValue() and session.getAttribute()?
New kid to Java.
-pkalp
-
July 27th, 2005, 07:20 AM
#2
Re: Difference between request.getParameter() and request.getAttribute()
Hi pkalp
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. Does this make sense?
I had a look at the API docs for session.getAttribute() and getValue() and saw that getValue is deprecated and getAttribute should be used in its place, so rather use getAttribute. Although it seems like they achieve the same result. I personally have never used getValue.
Byron
-
July 27th, 2005, 07:24 AM
#3
Re: Difference between request.getParameter() and request.getAttribute()
It was clear and it helped me a lot.
Thank you Bnt.
-pkalp
-
August 9th, 2005, 07:19 PM
#4
Re: Difference between request.getParameter() and request.getAttribute()
Bnt,
Thankx for the valuable description. I too was just browsing for some information related to request.getAttribute(String) and found your reply extremely terse and helpful.
Ankur.
-
January 4th, 2006, 02:11 AM
#5
Re: Difference between request.getParameter() and request.getAttribute()
Hello Ban,
Happy New Year 2006
Ban although u already explained the difference b/w request.getAttribute() & request.getParameter() but i want more differences b/w them so that i can understand more.
Thanks
Vaibhav
-
January 4th, 2006, 04:42 AM
#6
Re: Difference between request.getParameter() and request.getAttribute()
Hi,
Not quite sure what you are asking, those are the differences, what other differences would you like to know about?
Byron
-
February 17th, 2006, 04:55 AM
#7
Re: Difference between request.getParameter() and request.getAttribute()
Hi byron,
U r correct this are the differences u explained but i want more description along with some examples of both.
Please provide me as soon as possible.
Thanks
Vaibhav
-
February 17th, 2006, 05:59 AM
#8
Re: Difference between request.getParameter() and request.getAttribute()
Hi,
Ok, getParameter() first:
Code:
<html>
<body>
<form name="testForm" method="post" action="testJSP.jsp">
<input type="text" name="testParam" value="Hello Vaibhav">
<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 Vaibhav". It will then print it out, so you should see "Hello Vaibhav" 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 <code>POST</code> 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 <code>GET</code> and <code>POST</code> 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 <code>POST</code> 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 <code>GET</code> and <code>POST</code> 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. Does this help you?
Byron
-
February 21st, 2006, 05:24 AM
#9
Re: Difference between request.getParameter() and request.getAttribute()
Ya byron ur reply is very helpfull for me.
Thanks a lot.
Can u do for me one more favour
Plz tell me how we can access values from one java file to anther java file with the help of parameter and attribute methods.
Please elaborate through examples.
Thanks a lot
Vaibhav
-
February 21st, 2006, 05:31 AM
#10
Re: Difference between request.getParameter() and request.getAttribute()
Originally Posted by vaibhav13091980
Ya byron ur reply is very helpfull for me.
Thanks a lot.
It's my pleasure
Originally Posted by vaibhav13091980
Plz tell me how we can access values from one java file to anther java file with the help of parameter and attribute methods.
I'm not sure what you mean by values from one java file to another. What kind of java file do you mean? In my previous post I explained how to send the "attributes" from one servlet to another servlet (which are java files). As well as "parameters" from HTML/JSP to another JSP, which could also both be java files.
Byron
-
February 21st, 2006, 06:08 AM
#11
Re: Difference between request.getParameter() and request.getAttribute()
Ya for attribute this stuff is ok but i want to know how we can excess parameters values from one java into anthr java file.
But before this could tell me one core java program
In this prog. we have data in excel file in the form of Key value pair like for eg key-1 and value-a , key-3 and value -b and so on.
So i have to write core java prog in which i have to sort data on the basis of key and write again in the same excel file.
Please tell me this prog. first as soon as possible.
Thanks
Vaibhav
-
February 21st, 2006, 06:14 AM
#12
Re: Difference between request.getParameter() and request.getAttribute()
Ya for attribute this stuff is ok but i want to know how we can excess parameters values from one java into anthr java file.
Sorry but I still don't understand what you are asking.
But before this could tell me one core java program In this prog. we have data in excel file in the form of Key value pair like for eg key-1 and value-a , key-3 and value -b and so on. So i have to write core java prog in which i have to sort data on the basis of key and write again in the same excel file. Please tell me this prog. first as soon as possible.
Please start a new thread with this as it doesn't belong in this thread.
Byron
-
February 21st, 2006, 06:26 AM
#13
Re: Difference between request.getParameter() and request.getAttribute()
But how we can post new thread I not found any link
Plz tell
-
February 21st, 2006, 06:28 AM
#14
Re: Difference between request.getParameter() and request.getAttribute()
Go to Java Programming Forum home page and click "new thread".
-
February 21st, 2006, 06:35 AM
#15
Re: Difference between request.getParameter() and request.getAttribute()
Go HERE to post a new thread.
Byron
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|