CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Jul 2005
    Posts
    123

    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

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    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

  3. #3
    Join Date
    Jul 2005
    Posts
    123

    Re: Difference between request.getParameter() and request.getAttribute()

    It was clear and it helped me a lot.

    Thank you Bnt.

    -pkalp

  4. #4
    Join Date
    Aug 2005
    Posts
    1

    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.

  5. #5
    Join Date
    Jan 2006
    Posts
    32

    Thumbs up 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

  6. #6
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    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

  7. #7
    Join Date
    Jan 2006
    Posts
    32

    Question 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

  8. #8
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    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

  9. #9
    Join Date
    Jan 2006
    Posts
    32

    Smile 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

  10. #10
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    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

  11. #11
    Join Date
    Jan 2006
    Posts
    32

    Question 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

  12. #12
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    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

  13. #13
    Join Date
    Jan 2006
    Posts
    32

    Re: Difference between request.getParameter() and request.getAttribute()

    But how we can post new thread I not found any link

    Plz tell

  14. #14
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Difference between request.getParameter() and request.getAttribute()

    Go to Java Programming Forum home page and click "new thread".

  15. #15
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Difference between request.getParameter() and request.getAttribute()

    Go HERE to post a new thread.

    Byron

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured