tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Justin Ruthenbeck <>
Subject Re: instantiating a bean in a Servlet, not a JSP
Date Fri, 10 Jan 2003 23:07:41 GMT
At 02:23 PM 1/10/2003, you wrote:
>>>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".
>Oh, okay -- a slip of the mind there.  Is this the official way to do it, 
>because I can't find in my book (Core Servlets) where it says that the 
><jsp:useBean> tag knows to look in the session for the string used in the 
>"id" attribute.
>I want to avoid using a workaround since I'm just starting out.

This is the "correct" way to do what you're trying to do.  This is just my 
opinion, but if you're new to servlet/jsp programming and you're really 
trying to get a good foundation, ditch the jsps and focus on understanding 
the concepts behind servlets.  Once you've got servlets nailed, you'll find 
that jsps are just a shorthand way of coding servlets.  Too many people 
start out with both and get confused because you've got twice as many terms 
for the same number of things.  This is especially true for the jsp "scope" 
notion, but, as you're finding is the same way for bean usage.


Justin Ruthenbeck
Software Engineer, NextEngine Inc.
Confidential -

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

View raw message