Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading Servlet On Startup

 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bhushan Jawle
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try writing it to a file. Don't know why but Tomcat cannot print to console in init()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic