Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

starting two servlets in load on startup

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to start two servlets when tomcat starts.For that I have included load-on-startup in web.xml inside the war file as below.

web.xml:-


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
Dialer</display-name>
<servlet>

<servlet-name>HelloWorld</servlet-name>
<servlet-class>
HelloWorld</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>

<servlet-name>HelloWorld1</servlet-name>
<servlet-class>
HelloWorld1</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloWorld1</servlet-name>
<url-pattern>/HelloWorld1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>


The code for the two servlets are as follows.

HelloWorld2:-



HelloWorld1:-



Thanks
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Preeti,

What do you mean "start" here ? load-on-startup will actually load your servlet on to the servlet container when the web application is started . It just avoid the overhead of loading your servlet on its first access.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.Actually i need to run the two servlets and print the "Hello world" from the first servlet and "This is second servlet " from the second servlet. But in the console,The output "Hello world" only printed and second servlet does not prints its output.Is this possible to do? or am i doing wrong?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of loading servlets just to emit console output?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.close();

You can't expect anything to appear in a stream after you're closing it. Your code should not touch the system streams beyond printing to them.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Not Sure,remove this from your HelloWorld Servlet . and then try
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhhh, Ulf Beat me Here
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.Now it works fine.I need to run the servlet continuously.for that i am using searcher thread as follows:

HelloWorld:

HelloWorld1:


When starting tomcat,It just prints the HelloWorld's output.Using searcher.thread is wrong way to make the program to run continuously ?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to run the servlet continuously


I guess , you can go for Socket
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really should not use load-on-startup servlets; they're way outdated. Any background threads should be started in a servlet context listener.

I guess , you can go for Socket

What do sockets have to do with background threads?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
What do sockets have to do with background threads?


My Apologies. Even i am not having clear idea. but if servlet needs to run continously then we can go for ServerSocket right? please Correct me
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:but if servlet needs to run continously then we can go for ServerSocket right?

No. Server sockets, too, have nothing at all to do with running a task continuously. You might want to read the Sun Java Tutorial on network programming to get an idea what sockets are and how they work. But let's not derail this topic by talking about sockets.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic