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

servlet to bean to jsp

 
Shrikanth Goloposwami
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys & gals,
I am trying to access a bean which is set from a servlet in a jsp. Is this only accomplished by placing the bean in the session? If this is the case, can servlets and jsps share sessions with one another? If this isn't the case, how do servlets and jsp share objects?
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put a bean in the session in a servlet, you will indeed be able to access it in the session from a JSP (they are the SAME session) use request.getSession().getAttribute(attName)
You could also place the bean in the request (request.setAttribute) but be aware that that bean will only be avaliable in a JSP that is part of the same request....
Other options are to place the bean in the servletcontext (if you want it to be accessable by all JSPs and servlets in your webapp..)

S
Originally posted by Shrikanth Goloposwami:
Hi guys & gals,
I am trying to access a bean which is set from a servlet in a jsp. Is this only accomplished by placing the bean in the session? If this is the case, can servlets and jsps share sessions with one another? If this isn't the case, how do servlets and jsp share objects?

 
Shrikanth Goloposwami
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I do:
request.setAttribute("BeanName", _beanName);
getServletConfig().getServletContext().getRequestDispatcher("/jsp/bean.jsp").forward(request,response);
then I can access this bean in bean.jsp by:
<jsp:useBean id="BeanName" scope="request" class="com.domain.BeanName">
</jsp:useBean>
Is this correct? I am not successful using the above scenerio.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic