Parag
Parag

Reputation: 175

What happens when we explicitly create servlet object on jsp page or in java class? How it will effect on performance?

When we create Servlet object on JSP page or in Java class, How it works internally ? How it will effect on performance ?

Upvotes: 0

Views: 3233

Answers (2)

Taran
Taran

Reputation: 27

We can create an object of our servlet class. But because servlet operation depends on the servlet context, request, response, etc provided by the web container, there is nothing to be gained by creating one outside the container environment.

In one sentence - By doing so, we cannot expect to work as a servlet.

Upvotes: 0

Hardik Mishra
Hardik Mishra

Reputation: 14877

you should not call the servlet explicitly by the new keyword as we normally do.In the case of servlet, servlet container is responsible for instantiating the servlet.

For each servlet defined in the deployment descriptor of the Web application, the servlet container locates and loads a class of the type of the servlet. This can happen when the servlet engine itself is started, or later when a client request is actually delegated to the servlet.

There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.

When one create Servlet object on JSP page or in Java class,

You cannot expect to work it as a Servlet.

For More in details answer, Refer BalusC's answer here.

Upvotes: 1

Related Questions