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.intra.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.java: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