This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes JSP - Servlet - Session problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP - Servlet - Session problem" Watch "JSP - Servlet - Session problem" New topic

JSP - Servlet - Session problem

wawa piggy

Joined: Apr 05, 2004
Posts: 4
I have a JSP page which will post to a servlet upon clicking the "submit" button.
The servlet will perform some processing and then redirect back to the JSP with all the original form fields prefilled.
Which means i need to have a means for "transferring" a fair amount of data values from JSP to servlet back to JSP
so i cannot append the data as querystring behind my Url when i do a response.sendRedirect() from my servlet to JSP.
In my servlet, i have the below code. I try to set the values into a session before i use a response.sendRedirect() to
direct back to the JSP. But in the JSP, i don't seems to be able to retrieve back the session values
that i have set in the servlet.

public void service ( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
: // processing code
HttpSession session = request.getSession(false);

} // end of service() method

I am using SUNONE server. I need to pass values from servlet back to the JSP.
What am I doing wrong? Or is there any other methods ??
Thank you very much.
Ravikumar Jambunathan
Ranch Hand

Joined: Aug 25, 2003
Posts: 66
instead of using sendRedirect use RequestDispatcher to tranfer the control. RequestDispatcher is the preferred method to transfer the control as far as my experience is concerned.
try out and let me know.
Abdul Narayan D'souza TP

Joined: Apr 01, 2004
Posts: 10
Please check if u have created a session, when u access
your servlet for the first time.
HttpSession session=request.getSession(true);
a beter way to do this is to use RequestDispatcher
RequestDispatcher rd = request.getRequestDispatcher("JSP path");
--You dont need to store the values in a session, cause
u get access to the orignal request object.
wawa piggy

Joined: Apr 05, 2004
Posts: 4

yup it works. I use
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/jsppage.jsp");
rd.forward(request, response);
Thanks for all the help!!!
subject: JSP - Servlet - Session problem
It's not a secret anymore!