aspose file tools*
The moose likes Servlets and the fly likes JSP to Servlet to JSP attribute problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP to Servlet to JSP attribute problem" Watch "JSP to Servlet to JSP attribute problem" New topic
Author

JSP to Servlet to JSP attribute problem

clay jay
Greenhorn

Joined: Feb 05, 2009
Posts: 25
This must be a common problem but for some reason I can't find a solution for it, even on these forums, maybe I'm just searching with the wrong words so my apologies in advance if this question has been covered before.

I have a JSP page where the user clicks a submit button which targets my servlet, once my servlet has finished running I want the user to go back to that very same jsp page and depending on an attribute set in the servlet(or something else if that's wrong) to display some sort of error or confirmation message. I've tried this both ways and in both cases it seems I'm very close but each way has a small problem I can't figure out. The first method I tried was with a RequestDispatcher as can be seen below.



This way everything works out and I can get the "AAA" attribute but unfortunately the URL still points to the servlet so if a user hits refresh then the servlet runs again. I then tried using a redirect while setting an attribute as many ways as I could think of which can be seen below with just one of the setAttribute methods.



This way I get the URL I want but unfortunately I can't get the "AAA" attribute. If anyone can shed some light on this for me and tell me what I'm doing/understanding wrong it would be greatly appreciated.
Kuldeep Tewari
Ranch Hand

Joined: May 22, 2006
Posts: 35
How are you accessing the attribute in your jsp for second scenario?
clay jay
Greenhorn

Joined: Feb 05, 2009
Posts: 25
JSP for the first method which works

JSP for the second method which doesn't work

For the second method I've also tried getServletContext().setAttribute("AAA", "AAA"); in the servlet and then in the JSP I've tried pagecontext.getAttribute, session.getAttribute and request.getAttribute to get the attribute but can't in any case.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You're setting the attribute in the servletcontext and trying to retrieve it from the session.

That said, writing Java code in JSP files is a bad idea. Use taglibs and EL.
clay jay
Greenhorn

Joined: Feb 05, 2009
Posts: 25
Am I at least on the right track then wanting to store the attribute in the session so I can then retrieve it in the JSP? If so how do I then set it properly in the servlet?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
If you want to get it from session, obviously set it in the session instead of the servletcontext.
clay jay
Greenhorn

Joined: Feb 05, 2009
Posts: 25
Aw man I'm an idiot. For some reason I was doing session.getServletContext().setAttribute("AAA", "AAA"); instead of just session.setAttribute("AAA", "AAA");

I don't know why I just assumed I had that part correct and I was doing something else wrong so I just overlooked it and focused elsewhere... Anyways thanks for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP to Servlet to JSP attribute problem