File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes starting two servlets in load on startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "starting two servlets in load on startup" Watch "starting two servlets in load on startup" New topic
Author

starting two servlets in load on startup

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jan 01, 2009
Posts: 874


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

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jan 10, 2002
Posts: 61456
    
  67

What is the purpose of loading servlets just to emit console output?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
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.


Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



Not Sure,remove this from your HelloWorld Servlet . and then try
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Uhhh, Ulf Beat me Here
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jan 28, 2008
Posts: 5575

I need to run the servlet continuously


I guess , you can go for Socket
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
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

Joined: Jan 28, 2008
Posts: 5575

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
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: starting two servlets in load on startup