aspose file tools*
The moose likes Servlets and the fly likes Loading Servlet On Startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Loading Servlet On Startup" Watch "Loading Servlet On Startup" New topic
Author

Loading Servlet On Startup

Jaggi Kunal
Ranch Hand

Joined: Jan 21, 2003
Posts: 45
I am using Tomcat 4.1.12. I want to load a Servlet on Startup, i-e when the servlet container starts.
Here is the Servlet Code :
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LoadOnStartup extends HttpServlet{
public void init()throws ServletException{
System.err.println("Mesage at Startup");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.println("<b>Message to the Client</b>");
pw.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doGet(request,response);
}
}
Following is the deployment descriptor (web.xml) code snippet :
<servlet>
<servlet-name>
boot
</servlet-name>
<servlet-class>
LoadOnStartup
</servlet-class>
<load-on-startup/>
</servlet>
Now when I start the server (startup.bat), the message "Mesage at Startup” is not printed at the console. Also when I activate the Servlet by typing the URL’s in the address bar (http://localhost:8080/servlet/boot OR http://localhost:8080/servlet/LoadOnStartup), even then I don’t get the desired output.
Regards,
Kunal Jaggi
SCJP2
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
If you want to get a servlet to init on startup, that's easy enough - add the following line into your servlet definition:
...
<load-on-startup>1</load-on-startup>
</servlet>
the number determines what order the servlets are run in, lowest being first.
I'm guessing the reason why nothing seemed to work is probably because you didn't have the exact right syntax. That's just a guess tho. Try the above line and see if that works for you.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

What Phil said, and I'll add that the invoker servlet doesn't properly follow the load-on-startup tags. You need to reference the servlet through a proper alias.
Jaggi Kunal
Ranch Hand

Joined: Jan 21, 2003
Posts: 45
Hi Phil,
I have tried your code snippet too. But even then I don’t get to see the desired result.
As to what Mike says – “reference the servlet through a proper alias”, in that case it is quite obvious that that init() method will definitely execute because init() method is executed only once before handling 0 or more client requests. But here the requirement is that as soon as the server starts, I need the LoadOnStartup Servlet’s init() method to execute without waiting for any client request.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
You should put the LoadOnStartup class in a package - many odd things happen in Tomcat with servlet classes not in packages.
Bill
Jaggi Kunal
Ranch Hand

Joined: Jan 21, 2003
Posts: 45
Hi William,
Quite amazingly, even if I put the servlet in a package as shown below,
package practice;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LoadOnStartup extends HttpServlet{
public void init()throws ServletException{
System.err.println("Mesage at Startup");
}
……..
……..
It appears as if the init() method is not even called when a client request is handled using http://localhost:8080/servlet/practice.LoadOnStartup. This is really amazing, there is no output (on the servlet container console window) – does this mean that LoadOnStartup servlet is not initialized? Then how the client request was handled?
Changes made to the deployment descriptor are :
servlet>
<servlet-name>
boot
</servlet-name>
<servlet-class>
practice.LoadOnStartup
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Regards,
Kunal Jaggi
SCJP2
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
This is quite mystifying indeed and I would love to get to the bottom of it. The servlet interface uses
public void init( ServletConfig conf);
The GenericServlet class provides a version of init which takes ServletConfig and then calls the
public void init()
method as a convenience - because people keep forgetting to call super.init( config ) when the other method is used.
It sounds like the
public void init(ServletConfig c)
is being called but your init() is not for some reason. Incidently, the way you are addressing the servlet, using the /servlet/etc URL means that Tomcat is probably ignoring your web.xml.
Bill
[ March 27, 2003: Message edited by: William Brogden ]
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
when you specify a servlet to load-on-startup, it calls the init method of that servlet right at startup. I have a startup servlet that I use to initialize a lot of objects and put in the servlet context. You don't have to call it from the web, it should load right after you turn tomcat on. The fact that it doesn't points to a deeper problem, then. So check your web.xml like everybody's been saying Any typos in there are show-stoppers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading Servlet On Startup