aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP - Servlet - Session problem" Watch "JSP - Servlet - Session problem" New topic
Author

JSP - Servlet - Session problem

wawa piggy
Greenhorn

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);
session.setAttribute("empID","TUU565");
:
:
response.sendRedirect(strEncodeUrl);

} // 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
hi,
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.
regards,
ravi
Abdul Narayan D'souza TP
Greenhorn

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
code---
RequestDispatcher rd = request.getRequestDispatcher("JSP path");
rd.forward(request,response);
--You dont need to store the values in a session, cause
u get access to the orignal request object.
wawa piggy
Greenhorn

Joined: Apr 05, 2004
Posts: 4

yup it works. I use
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/jsppage.jsp");
request.setAttribute("resp","success");
rd.forward(request, response);
Thanks for all the help!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSP - Servlet - Session problem
 
Similar Threads
a session problem
Secure session Data
Session variables in Netscape 4.76?
HttpClient and sendRedirect
how to call servlet from struct action class