Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I use Servlet Session in JSP Page.

 
pras patel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to know wheater a session obj created in servlet can be used in a jsp. I want some parameter to be passed , i have use
sendRedirect() and also forward of the requestdispatcher.
 
saradhi pantra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai patel,
u can use the servlet session object in jsp page.
u use RequestDispatcher .In this u use forward() method
It redirct the request to that JSP page .In that JSP page
u call session.getValue()
saradhi
 
Kiran Kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
i am having the same problem. i am setting some attributes for a session object in my servlet and using RequestDispatcher.forward to a new jsp. i am not able to get the attribute values in my jsp. . when i say session.getAttribute(****) i am getting null, somebody please help me in this.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your servlet, you must use the passed in request object to obtain 'the' session, not just any session. This might be the problem if you're not doing it.

Session ses = request.getSession();
ses.setAttribute("hello", "hello");
.
.
.
Then you get the RequestDispatcher and forward
RequestDispatcher rd = getRequestDispatcher("/somePage.jsp");
rd.forward(request, response);
.
.
inside somePage.jsp, you should be able to access "hello" like you already know how to do.
 
Kiran Kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i called ServletContext.getRequestDispatcher(**). but if i give the whole absolute path of the jsp page it is returning null.
like
RequestDispatcher rd = ServletContext.getRequestDispatcher("http://localhost:7001/checkmail.jsp")
here rd will be null. but if i give relative path like "/checkmail.jsp" i will get the rd object but when i call rd.forward(**,**) the jsp page is not coming
i used the HttpServletRequest.getRequestDispatcher() also.
i am using Weblogic 6.0 application server.
i cannot give relative path like "/checkmail.jsp" becoz the control will be returned from a servlet ie "http://localhost:7001/examplesWebApp/MainServlet" and it is taking "http://localhost:7001/examplesWebApp/checkmail.jsp" so i am not able to view the jsp page.
the MainServlet is a FrontController for the application and all the html/jsp request will go through this servlet. so according to the data got from the backend i should send the objects to the jsp page as session attributes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic