• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Instance

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have faced one question on servlets that is,How to create single instace for multiple requsts? pls send the answer as early.

thanks,

rajesh
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the normal behavior.
 
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Container loads the Servlet class and creates a single instance and for every request that comes in(for that servlet),the Container creates a separate thread(so a single request per thread).
This is the normal behavior as Bear says.

Regards,
Srikkanth.M
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In xml file
<servlet>
<servlet-name> servlet1 </servlet-name>
<servlet-class>com.pack.HelloServlet </servlet-class>

<servlet-name> servlet1 </servlet-name>
<url-pattern> /index </url-pattern>
</servlet>


Depending on url it calls servlet1 and then creates
HelloServlet instance.
I am not sure but read some where if

<servlet>
<servlet-name> servlet1 </servlet-name>
<servlet-class>com.pack.HelloServlet </servlet-class>

<servlet-name> servlet2 </servlet-name>
<servlet-class>com.pack.HelloServlet </servlet-class>


<servlet-name> servlet1 </servlet-name>
<url-pattern> /index </url-pattern>

<servlet-name> servlet1 </servlet-name>
<url-pattern> /welcome </url-pattern>
</servlet>


here it creates two instances of HelloServlet class.
will it work,Is it correct
 
gkumar kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In this way also we can have multiple instances of a servlet.This works and iam sure

New instances of any servlet will be created for as many times as the
servlet is named differently. For instance, all these refer to the same
servlet, but each will be a separate instance....

<servlet>
<servlet-name>MyServletName</servlet-name>
<servlet-class>com.mypackage.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServletName</servlet-name>
<url-pattern>/mymapping</url-pattern>
</servlet-mapping>


http://localhost:8080/mycontext/mymapping

http://localhost:8080/mycontext/servlet/MyServletName

http://localhost:8080/mycontext/servlet/com.mypackage.MyServlet
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rajesh kumar rayavarapu:
I have faced one question on servlets that is,How to create single instace for multiple requsts? pls send the answer as early.



The container does it that way.It creates a single instance of the servlet and multiple request processing threads use that instance only.

For each incoming request a new thread is created (or , using a thread pool for request processing) and appropriate methods are copied to its stack for execution.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"gkumar",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic