Results 1 to 3 of 3
  1. #1
    Walter is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Cookies and request.getAttribute() / request.setAttribute() question


    I'm a new programmer to Java, I'm currently developing a tiny project in Java EE.
    In my servlet > doGet method, I have two parameters, request that is an instance of "HttpServletRequest" and response, instance of "HttpServletResponse".

    I set the language variable as following :

    request.setAttribute("lang", "en");
    Then I forward the servlet to a JSP file using :
    getServletContext().getRequestDispatcher("/file.jsp").forward(request, response);

    Then in the JSP file I can of course, get the language using :
    String lang = (String) request.getAttribute("lang");

    My question is, if the visitor clicks on a link that figures on my "file.jsp" file, then the "request" and "response" won't be forwarded I guess (there's no forward(request,response)), so will I still be able to use request.getAttribute("lang") to get the language variable? Is it like a cookie stored in the browser that I can recover from whatever servlet?

    If not, how can I store that variable so I can recover it from any other servlet?
    Thank you in advance :)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    London, UK
    Blog Entries
    Rep Power

    Default Re: Cookies and request.getAttribute() / request.setAttribute() question

    If they land on the "file.jsp" page directly, you should send them backward to the starting point (wherever that may be in your system).

    It's just like any website with a login area, if you try to move directly to "members.jsp" and you aren't logged in you should be sent back to "login.jsp" to go through the process properly.

    So you can check that just by checking the values of variables that weren't set.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Cookies and request.getAttribute() / request.setAttribute() question

    The data in the request object is for a single request only, so the next time the user clicks on a link a new request is constructed on the server.
    Consequently any attributes you added in a previous request are no longer around.

    If you need data for a session between requests then either store it in the session (which has its own set of attributes) or store it in hidden fields on the page so they are submitted as parameters next time round.
    Exactly which one is suitable in this case is entirely down to your requirements.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Get request
    By Lacrim in forum Networking
    Replies: 0
    Last Post: 12-16-2011, 11:38 AM
  2. Replies: 0
    Last Post: 07-06-2011, 01:12 PM
  3. How to use request.setAttribute() in JSP?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 06-01-2011, 10:03 AM
  4. Replies: 1
    Last Post: 05-10-2011, 08:10 AM
  5. Question about Ajax request
    By ulix83 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-23-2010, 10:01 AM

Posting Permissions

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