Results 1 to 3 of 3
- 03-27-2012, 09:07 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Cookies and request.getAttribute() / request.setAttribute() question
Hello,
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 :)
-
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.
- 03-29-2012, 10:32 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
Similar Threads
-
Get request
By Lacrim in forum NetworkingReplies: 0Last Post: 12-16-2011, 11:38 AM -
[XML-RPC Apache] The api create request handler for each request! HELP
By dario in forum Advanced JavaReplies: 0Last Post: 07-06-2011, 01:12 PM -
How to use request.setAttribute() in JSP?
By makpandian in forum JavaServer Pages (JSP) and JSTLReplies: 7Last Post: 06-01-2011, 10:03 AM -
problem in understanding request.setAttribute() method
By jnjh in forum New To JavaReplies: 1Last Post: 05-10-2011, 08:10 AM -
Question about Ajax request
By ulix83 in forum JavaServer Faces (JSF)Replies: 0Last Post: 09-23-2010, 10:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks