If i have a web application. Then when several clients make requests for the same page (or different pages in the same web application) at the same time.
Is there only one Servlet instance will be created?
I know that the init() method only be invoked once? Then if i initiate some variables in the init() method,if there are several requests,will the variables be create several instances for each requests?
But if i initiate the variables in the doPost() method, what'll be different?
Answer:
Is there only one Servlet instance will be created?
I know that the init() method only be invoked once? Then if i initiate some variables in the init() method,if there are several requests,will the variables be create several instances for each requests?
But if i initiate the variables in the doPost() method, what'll be different?
Answer:
> If i have a web application. Then when several
> clients make requests for the same page (or different
> pages in the same web application) at the same time.
>
> Is there only one Servlet instance will be created?
There may be. The spec says that the server may create only one servlet to handle all requests, but does not require it. Some servers may create several so as to share the work...
> I know that the init() method only be invoked once?
Per instance. So if one instance then the method willonly be called once.
> Then if i initiate some variables in the init()
> method,if there are several requests,will the
> variables be create several instances for each
> requests?
>
No. All requests will share the values. This is why it is bad practice to store data in class level variables. If you do it, you should treat that data as if it had the static key word. If you change the value in one request, all other requests using the same instance of the servlet (maybe all) will see that change.
> But if i initiate the variables in the doPost()
> method, what'll be different?
It brings the initiatiation closer to the request, but every request will still see the changes. If you do not want to see the same data in all your requests, then move the variables out of the class and into the method:
> clients make requests for the same page (or different
> pages in the same web application) at the same time.
>
> Is there only one Servlet instance will be created?
There may be. The spec says that the server may create only one servlet to handle all requests, but does not require it. Some servers may create several so as to share the work...
> I know that the init() method only be invoked once?
Per instance. So if one instance then the method willonly be called once.
> Then if i initiate some variables in the init()
> method,if there are several requests,will the
> variables be create several instances for each
> requests?
>
No. All requests will share the values. This is why it is bad practice to store data in class level variables. If you do it, you should treat that data as if it had the static key word. If you change the value in one request, all other requests using the same instance of the servlet (maybe all) will see that change.
> But if i initiate the variables in the doPost()
> method, what'll be different?
It brings the initiatiation closer to the request, but every request will still see the changes. If you do not want to see the same data in all your requests, then move the variables out of the class and into the method:
//Not goo public class MyServlet extends HttpServlet { private String someData = ""; public void doPost(...) ... { someData = request.getParameter("Data"); ... } } //better public class MyServlet extends HttpServlet { public void doPost(...) ... { String someData = request.getParameter("Data"); ... } }