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