• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with servlet initialization  RSS feed

 
Davin Matsuda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I have a question regarding the usage of the servlet's init method. I have a class called DatabaseServlet which opens two database connections (through classes called UserCollection and LogCollection). In my web.xml, I set my DatabaseServlet's load on startup to 1 and my ActionServlet's load on startup to 2. The ActionServlet (from struts) handles all web requests. My questions are:
1) Is this a good pattern or not? Does anyone have a better approach?
2) Are there circumstances which may cause this approach to fail? It works in Tomcat 3.2.3 but will it screw up on other web containers such as weblogic?
3) Should I make it extend GenericServlet instead? I'm leaning towards changing it.
I posted the snippets of DatabaseServlet.java and web.xml for reference. I'll appreciate if someone can give me some insight on this.
-----------------------------------------------------------
public final class DatabaseServlet extends HttpServlet {
/**
* Opens a connection to the two database files. This servlet should be started up
* before the <code>ActionServlet</code>.
*
* @exception ServletException if an exception occurs that interrupts the servlet's
* normal operation
*/
public void init() throws ServletException {
LogCollection.getInstance().open();
UserCollection.getInstance().open();
System.out.println("End of DatabaseServlet");
}
public void destroy() {
UserCollection.getInstance().close();
LogCollection.getInstance().close();
}
}
-----------------------------------------------------------
<!-- Database Initialization Servlet Configuration -->
<servlet>
<servlet-name>database</servlet-name>
<servlet-class>edu.hawaii.icsprofiler.DatabaseServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>edu.hawaii.icsprofiler.ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
 
Tim Holloway
Bartender
Posts: 18663
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a fairly popular approach. I have no complaint with it - it allows you to set up services (such as database connection pools) without having to extend the ActionServlet class. Thus, you can simplify by keeping things distinct.
The only problem there is if you had an appserver that didn't support a fixed sequence of servlet startups.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!