-
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
-
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
-
Re: Difference between request.getParameter() and request.getAttribute()
It was clear and it helped me a lot.
Thank you Bnt.
-pkalp
-
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.
-
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
-
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
-
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
-
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
-
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
-
Re: Difference between request.getParameter() and request.getAttribute()
Quote:
Originally Posted by vaibhav13091980
Ya byron ur reply is very helpfull for me.
Thanks a lot.
It's my pleasure
Quote:
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
-
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
-
Re: Difference between request.getParameter() and request.getAttribute()
Quote:
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.
Quote:
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
-
Re: Difference between request.getParameter() and request.getAttribute()
But how we can post new thread I not found any link
Plz tell
-
Re: Difference between request.getParameter() and request.getAttribute()
Go to Java Programming Forum home page and click "new thread".
-
Re: Difference between request.getParameter() and request.getAttribute()
Go HERE to post a new thread.
Byron
-
Re: Difference between request.getParameter() and request.getAttribute()
Byron i posted with Title "Sort data of excel file through core java"
plz tell me
Thanks vaibhav
-
Re: Difference between request.getParameter() and request.getAttribute()
Sorry but I don't see the thread there. Did you create a new one?
-
Re: Difference between request.getParameter() and request.getAttribute()
Hello byron,
Thanks a lot , i posted new thread with title "Sort data of excel file through core java ".
Please provide me solu. as soon as possible.
Yours admiring friend
Vaibhav
-
Re: Difference between request.getParameter() and request.getAttribute()
hi
can u help me.........
wats the error in this
String id = request.getParameter("email");
System.out.println("value"+id);
its showing "unreachable code"
thank you
jittu
-
Re: Difference between request.getParameter() and request.getAttribute()
Hi,
Firstly, please could you start a new thread for a new question.
Secondly, please post more code, it's hard to see what code is unreachable whith only those 2 lines of code.
-
Re: Difference between request.getParameter() and request.getAttribute()
hi,
i have a question regarding getting/setting parameters at html/jsp.
Code:
<input type="hidden" id="mode" name="mode">
its value is set thru javascript depending on case and then retrieved in servlet like:
Code:
String mode = request.getParameter("mode");
now i need to reset its value to blank wen it goes back to jsp but m not able to do it. m using following at onload event of <body>
Code:
document.getElementById('mode').value = '';
i assume as an alternate, using getAttribute/setAttribute is not a ryt approach. so how to reset its value once the page loads?
in need of some kind guidance!
regards,
rabs
-
Re: Difference between request.getParameter() and request.getAttribute()
You'd probably be better off posting a new thread instead of replying to a post that's a year old...
A prudent question is one-half of wisdom...
F. Bacon
-
Re: Difference between request.getParameter() and request.getAttribute()
getAttribute() is used to access values in the HTTP header. You can do setAttribute and this value is set into the HTTP header when transmitting to the server. However , there is no setParameter as these parameter are expected to come from the form only.
-
Re: Difference between request.getParameter() and request.getAttribute()
Byron Keep the good work and thanks for sharing knowledge.
-
Re: 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.
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")%>
java development