tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Yunusov <>
Subject Re: instantiating a bean in a Servlet, not a JSP
Date Fri, 10 Jan 2003 21:29:47 GMT
On Friday 10 January 2003 04:19 pm, Erik Price wrote:
> Hi,
> I have another very basic question.  If it is a problem tell me to
> leave, sorry.  The index.html page has a form which lets users enter a
> username/password combo, which gets sent to LoginServlet.  LoginServlet
> performs the authentication, and if it fails then the user is redirected
> to an error page.  But if it succeeds, I want to instantiate a UserBean
> class (which I have created) and make the scope of that bean "session".
>   So that I can access this bean in later pages.
> I understand that I can have my JSPs create or refer to a named bean using
> <jsp:useBean id="nameOfInstance"
>               class="UserBean"
>               scope="session"
> />
> This is fine.  But I would like to instantiate the bean in LoginServlet.
>    How can I register the bean in the session scope for later JSP's to
> retrieve it?
> My first guess was that in LoginServlet I could call
>    // "ub" is reference variable of instantiated UserBean
>    HttpSession session = getServletContext().getSession();
>    session.setAttribute("nameOfInstance", ub);
> But I do not think that this is right.  Later JSPs can access this
> instance of UserBean using id="ub" ????

No, the line of thought is correct but there are two problems:
  1) there is no getSession() in ServletContext, use getSession() in 
HttpServletRequest. (HttpSession, as the name suggests, is specific to HTTP 
while ServletContext is application layer protocol agnostic).
  2) id attribute in the jsp:useBean tag would be "nameOfInstance", not "ub".

> Thanks,
> Erik


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message