Hi Andi;

Since code in the init() method is executed *exactly* once at servlet initialization 
time (*not*
once per request), you wouldn't want to have:

context.setAttribute("connection", connection");

in the init method at all. (Because i don't imagine you want to share the connection 
object amongst
all users.)

Instead you want to initialize the ConnectionPool in the init method (since that needs 
to be done
be only once), and in the doPost/doGet (of the first servlet) you may want to have 
code like this:

HttpSession session = req.getSession(true);
Connection connection = myConnectionPool.getconnection();
session.setAttribute("connection", connection);

Then in your second servlet you should be able to access that connection object via:

HttpSession session = req.getSession(false); //dont create a session if one doesn't 
exist
if (session ==  null){
      //code for redirecting to first sevlet..
}
else {
      Connection connection = session.getAttribute("connection");
      //etc.
}


Andi Setiyadi wrote:

> I try to use connection pooling to make database connection and put the
> connection object in ServletContext.  Here is how my application work.
> - user login through JSP and submit the request which handled by first
> level servlet.  In the init( ), I did:
>      ServletContext context = getServletContext( ), then make database
> connection  and put in the attribute
>      context.setAttribute("connection", connection");
>
> - first level servlet then call second level servlet.  In the second
> servlet I try to getServletContext( ):
>      ServletContext context = getServletContext( );
>
> When executing that line, I got the following error:
>
> null
> java.lang.NullPointerException
>            at
> javax.servlet.GenericServlet.getServletContext(GenericServlet.java:205)
>            at com.wg.hr.PersonnetWelcome.execute(PersonnetWelcome.java:37)
>            at com.wg.hr.Personnet.doGet(Personnet.java:67)
>            at com.wg.hr.Personnet.doPost(Personnet.java:85)
>            ...
>
> Any suggestion is appreciated
> Thanks,
> Andi

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to