Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Instance  RSS feed

 
rajesh kumar rayavarapu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the normal behavior.
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • 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
 
gkumar kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!