2

I have query , that If I mentioning the web.xml for my servlet application such as shown below ..

<servlet>
<servlet-name>AuthenticationServlet</servlet-name>
<display-name>AuthenticationServlet</display-name>
<servlet-class>com.trading.AuthenticationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet>
<servlet-name>AuthorizationServlet</servlet-name>
<display-name>AuthorizationServlet</display-name>
<servlet-class>com.trading.AuthorizationServlet</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>

Now I want for AuthenticationServlet servlet container should create two instances of it , How I will do that , Please advise

user1538526
  • 85
  • 7
  • 22

1 Answers1

5

You can just define multiple servlet instances using different <servlet> entries in web.xml pointing to the same servlet class.

<servlet>
    <servlet-name>AuthorizationServlet1</servlet-name>
    <servlet-class>com.trading.AuthorizationServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>AuthorizationServlet2</servlet-name>
    <servlet-class>com.trading.AuthorizationServlet</servlet-class>
</servlet>

You'll only need to map them on different URL patterns.

That said, I have the strong impression that you're looking in the completely wrong direction while looking for a solution for the particular functional requirement which you didn't tell anything about in your question. In a properly designed servlet web application, there should usually be no need to have more than one instance of the servlet. If you for example need to store user-specific information, use the HTTP session for that. To learn how servlets work and how they are to be used, read How do servlets work? Instantiation, sessions, shared variables and multithreading

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452