This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there any way to call servlet?

 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4588
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4588
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4588
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic