Hi Jim,
As a general rule, use only locale variables in your Action class as there's
only one instance but numerous threads wanting to access the methods and
properties.
>From your code, it seems you're using version 1.0 of Struts. The servlet
will be set via the "setServlet" method when needed, so keep the default
method.
You just have to write your specialized "perform" processing. And in that
code, the "servlet" property is set, so you can write:
public class LoginAction extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse
response)
throws java.io.IOException,
javax.servlet.ServletException
{
// Obtain the connection to the DB
ConnectionPool pool = (ConnectionPool)
servlet.getServletContext().getAttribute("com.hotu.intra.db.DB");
...
Pierre M�tras
----- Original Message -----
From: "Jim Newsham" <[EMAIL PROTECTED]>
To: "struts-user" <[EMAIL PROTECTED]>
Sent: Tuesday, November 21, 2000 9:47 PM
Subject: getting servlet context attributes within action
>
>
> Hi,
>
> I'm trying to set up a global resource as described in the Struts User
> Guide (under the heading "An Aside: Accessing Relational Databases"),
> by creating it in a startup servlet and attaching it to the servlet
> context as an attribute. Within my Action class, how do I retrieve the
> attribute? I tried calling Action.getServlet() as shown in the code
> snippet below, but this seems to return null.
>
>
> public class LoginAction extends Action {
>
> private DB db;
>
> // constructor sets up db access
> public LoginAction()
> throws ApplicationException {
> super();
>
> Servlet servlet = getServlet(); // this is returning null !
> ServletConfig config = servlet.getServletConfig(); // this is line
> 29
> ServletContext context = config.getServletContext();
> this.db = (DB) context.getAttribute("com.hotu.intra.db.DB");
> //this.db = (DB)
>
getServlet().getServletConfig().getServletContext().getAttribute("com.hotu.i
ntra.db.DB");
>
> if (db == null) {
> throw new ApplicationException(ApplicationException.DB_INIT_ERR);
> }
> }
>
> ....
>
> }
>
> /caucho.com/http/host/: java.lang.NullPointerException
> at com.hotu.intra.LoginAction.<init>(LoginAction.java:29)
> at java.lang.Class.newInstance0(Native Method)
> at java.lang.Class.newInstance(Class.java:237)
> at
>
org.apache.struts.action.ActionServlet.processActionCreate(ActionServlet.jav
a:1263)
>
> at
> org.apache.struts.action.ActionServlet.process(ActionServlet.java:1232)
> at
> org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:435) at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:115)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:83)
> at
> com.caucho.server.http.Invocation.service(Invocation.java:236)
> at
> com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:142)
>
> at
> com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:211)
> at
> com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:145)
>
> at
> com.caucho.server.TcpConnection.run(TcpConnection.java:139)
> at java.lang.Thread.run(Thread.java:484)
>
>
>
>
> Thanks in advance,
>
> Jim Newsham
>
>