How to by using an Init Servlet.

Some examples, since explaining would take too long :

Java startup servlet
---------------------------cut---------------------------
package org.init.whatever;

import javax.servlet.*;
import javax.servlet.http.*;

public class InitServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException {
super.init(config);
//DO WHATEVER YOU NEED TO DO HERE
//config.getServletContext().setAttribute("states","some object with 
states in it");
}

public void destroy() {

}

/** Processes requests for both HTTP <code>GET</code> and 
<code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, 
HttpServletResponse response)
throws ServletException, java.io.IOException {
//do nothing
}

/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse 
response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse 
response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Init ME";
}

}
----------------------------------------------------------------cut 
again-----------------
in you web.xml:
---------------------------------cut cut --------------------
<servlet>
<servlet-name>Init Servlet</servlet-name>
<display-name>Init Servlet</display-name>
<description>Servlet to initialize whatever</description>
<servlet-class>org.init.whatever.InitServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>WEB-INF/maybe.some.file.if.you.need.such.a.thing.properties</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
---------------------------------/cut cut --------------------

Is it clearer or as mud ?

Jf

Eddie Bush wrote:

> Michael,
>
> I'm currently building some functionality on top of Struts for a 
> project. The way I found to do this the easiest was to keep my config 
> in an XML file and use the commons digester in a Plugin. Plugin 
> writing is very straight-forward, and there are a couple of standard 
> examples (TilesPlugin/ValidatorPlugin) out there for you to look at. 
> Personally, when I started in, I looked at the ValidatorPlugin. It's 
> much shorter and covered everything I cared about.
>
> Day, Michael-IBM/TT wrote:
>
>> This is probably one of the stupidest questions, but I don't know how 
>> to do something once during application startup.
>>
>> For instance, if I wanted a bean to hold a list of states, how would 
>> I create that once during startup?
>>
> Note: If you needed to get persistent stuff out of a database, you 
> could do your DB-access here to load your beans.
>
> HTH
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to