File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP - Servlet - Session problem

 
wawa piggy
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic