About Question enthuware.jwpv6.2.1179 :

Moderator: admin

Post Reply
ETS User

About Question enthuware.jwpv6.2.1179 :

Post by ETS User »

The servlet container may maintain multiple instances of the servlet class for a servlet. - isn`t it a valid answer for a SingleThreadModel servlets and servlets defined with different names and the same class?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by admin »

Yes, but the question is asking, "Which is true for ***all*** servlets?"
If you like our products and services, please help us by posting your review here.

sebastien
Posts: 12
Joined: Tue Mar 26, 2013 2:38 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by sebastien »

Hi

Even without SingleThreadModel, container may use multiple instance of a servlet ...

Tell me if I'm wrong ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by admin »

No, a container is not allowed to create multiple instances of the Servlet class if it doesn't implement SingleThreadModel (which is deprecated, btw).

-Paul.
If you like our products and services, please help us by posting your review here.

kumarkhiani
Posts: 9
Joined: Sun Aug 25, 2013 2:36 am
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by kumarkhiani »

The following method setInitParameter of ServletContext can change the Context Parameters since Servlet 3.0.

Given this method, are the context parameters still thread safe?

Code: Select all

setInitParameter

boolean setInitParameter(java.lang.String name, java.lang.String value)
Sets the context initialization parameter with the given name and value on this ServletContext.
Parameters:
name - the name of the context initialization parameter to set
value - the value of the context initialization parameter to set
Returns:
true if the context initialization parameter with the given name and value was set successfully on this ServletContext, and false if it was not set because this ServletContext already contains a context initialization parameter with a matching name
Throws:
IllegalStateException - if this ServletContext has already been initialized
UnsupportedOperationException - if this ServletContext was passed to the ServletContextListener#contextInitialized method of a ServletContextListener that was neither declared in web.xml or web-fragment.xml, nor annotated with WebListener
Since:
Servlet 3.0

kumarkhiani
Posts: 9
Joined: Sun Aug 25, 2013 2:36 am
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by kumarkhiani »

If the Servlet name in annotations and in <servlet-name> tag are different, then container can make 2 different instances of the servlet-class. According to this, can't the container maintain multiple instances of the same servlet?
admin wrote:No, a container is not allowed to create multiple instances of the Servlet class if it doesn't implement SingleThreadModel (which is deprecated, btw).

-Paul.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by admin »

That statement of mine that you have quoted was meant for simple case where there is only one name. If you have configured multiple servlet names with same class, you may have multiple instances. Even so, all requests for one name will go to the same instance.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

niallcol
Posts: 5
Joined: Sat Apr 05, 2014 5:44 am
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by niallcol »

Hi Paul,

Does this question need to revised given that:

1. It is possible for the container to maintain multiple instances of a servlet class for a servlet.
2. Context parameters are not inherently thread safe because of the ServletContext.setInitParameter() method.

Kind Regards

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by admin »

1. No, it is not possible for the container to maintain multiple instances of a servlet class for the same servlet unless it implements SingleThreadModel, which is deprecated and is not on the exam.
2. The setInitParameter works only once i.e. once a parameter is set, you cannot change it. Also, it works on until the servlet is initialized. After that you cannot use this method to modify the parameters. So effectively, servlet context parameters are still thread safe.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

niallcol
Posts: 5
Joined: Sat Apr 05, 2014 5:44 am
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by niallcol »

Hi Paul,

Thanks for the quick reply. Just on the issue of the container maintaining multiple instances of a servlet class. The spec says this for the case where a servlet is defined using annotations and the DD:

If the same servlet class is declared in the deployment
descriptor under a different name, a new instance of the servlet MUST be
instantiated.

Does this not imply that the container can maintain multiple instances of the same servlet class?

Thanks

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by admin »

Yes, but I said for the same servlet. You can use the same servlet class to define two servlets and in that case two instances will be created. But that is not the case here. The option is talking about a servlet class for a particular "servlet".
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by himaiMinh »

There is no <context-param> in dd.

Code: Select all

<% 
       getServletContext().setInitParameter("myValue", "20");
   %>
java.lang.IllegalStateException: Initialization parameters can not be set after the context has been initialized
The setInitParameter method cannot return successfully for the first time because context has been initialized.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1179 :

Post by admin »

If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests