This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Is there any way to call servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is there any way to call servlet?" Watch "Is there any way to call servlet?" New topic
Author

Is there any way to call servlet?

mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 243

hi sorry for asking this stupid question.
Is there any way to call a servlet dynamically on start up of applcation (Not running that servlet)
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

Invocation of a Servlet requires to have a request object and a response object. Since the event of starting the application has nothing to do with a request or response, there isn't a proper way to call a Servlet. If you need some code to be executed at the application startup, you can setup a context listener.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1075
    
    1

Servlet can be invoked using HTTPRequest and HTTPResponse objects. As Devaka mentioned, this is the only way to call Servlet.

If you want Container to run your servlet at start up of application, try exploring <load-on-startup> element of web.xml. With this, you can load servlet at the start of application.

Please let us know the purpose with which you want to call Servlet dynamically on start up of applcation (Not running that servlet).

Do you want to setup some variable, call some generic method to iniatilize DB connection etc?

Please tell us your reason. We may find some other way to achieve your requirement.

~ abhay


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

Abhay Agarwal wrote:If you want Container to run your servlet at start up of application, try exploring <load-on-startup> element of web.xml. With this, you can load servlet at the start of application.

Just for not to make the OP confused: <load-on-startup> does nothing other than instantiating the servlet and calling the init() method. In other words, neither doGet or doPost is called by that.
mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 243

I have an application which execute applet and it displays a window in swings and in parallel to applet i have to execute servlet to communicate with server for fetching data for swings.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

Note that an Applet is an application that runs at the client end. If you need to send something from your Applet to a web server, you can do that by sending a typical HTTP request. If you have a problem in doing so, please start a new topic in our Applets forum.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

The applet has to call a server to get the data for the Swing model. If the server is a web server using servlets, the servlet will be automatically started when the first request comes in.

You can specify load-on-startup in the web.xml file to cause the servlet to be loaded when the webapp first starts up, and that will save a little time when the first servlet request is made, but other than that, the results will be the same as the default on-demand servlet startup. Once the servlet has started, it remains available until the webapp is shut down.

Note that I said "available", not "running". That's because servlets are not programs, and they don't "run". They don't have their own private execution threads. A servlet is more like a DLL than an executable in that respect, if you prefer to think of it in Microsoft Windows terms.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
wood burning stoves
 
subject: Is there any way to call servlet?
 
Similar Threads
calling servlet from java in different paths
load a servlet after startup
send response to calling servlet
How to call Jasper Report
Checking a Boolean Value In JavaScript