You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Erik Price <ep...@ptc.com> on 2003/01/10 22:19:36 UTC

instantiating a bean in a Servlet, not a JSP

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" ????



Thanks,

Erik


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: instantiating a bean in a Servlet, not a JSP

Posted by Justin Ruthenbeck <ju...@nextengine.com>.
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


____________________________________
Justin Ruthenbeck
Software Engineer, NextEngine Inc.
justinr@nextengine.com
Confidential -
    See http://www.nextengine.com/confidentiality.php
____________________________________


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: instantiating a bean in a Servlet, not a JSP

Posted by Erik Price <ep...@ptc.com>.

Paul Yunusov 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.

Thanks again Paul!


Erik


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: instantiating a bean in a Servlet, not a JSP

Posted by Paul Yunusov <py...@rogers.com>.
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

Paul

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>