Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple users and static objects 1

Status
Not open for further replies.

thrybergh

MIS
Feb 10, 2003
52
GB
Hello.

I have written a web app which requires the users to login with a username and password.

I have noticed that if another user is logged in already then a new user will be shown the first user's screen. I think this is because my servlets use STATIC "request handler" objects to create the pagess (via Xalan/XSLT).

Is this a likely cause of my problem?

Thanks.
 
Remember that Servlets generally have only one instance, so anything that is in the class scope, if altered or set, will be seen by each and every request.

Consider the below servlet :

Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class TServlet extends HttpServlet {

	public int counter = 0;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			PrintWriter out = response.getWriter();
			out.println(&quot;<HTML><BODY>counter is &quot; +counter +&quot;</BODY></HTML>&quot;);
                        counter++;
			out.flush();
			out.close();
    }



}

You may think that every request would see the counter variable as &quot;0&quot; in the browser. But, because all requests share the same Servlet instance, only the first request will see &quot;0&quot;. The next will see &quot;1&quot;, the next &quot;2&quot; and so on.

Hence you must design your servlet carefully to avoid this issue and make your responses safe.
 
Thanks for the advice.

It sounds like I need to make a new instance of each request handler servlet.
 
No don't !!! It'll slow everything down ... the whole point of a single servlet instance is that its much quicker to run if one point-of-contact services ALL requests ... there must be another way !

 
Thanks sedj.

I think in my case it will be OK? :

The main servlet is &quot;ShowPredictions&quot; which creates a (currently static) &quot;ShowPredictionsRequestHandler&quot; object that runs the SQL queries and creates XML, calls Xalan and outputs the HTML to the browser. I think I need to have a &quot;ShowPredictionsRequestHandler&quot; for each user. I hope that this will keep everyone's session independent.

I would not expect to have more than 5 users at once on the whole site, so performance is not a vitally important issue. However, I am amazed that there have only been a small number (no more than 10) of instances of different users logging on at the same time over the last few weeks.
 
>>>> I think I need to have a &quot;ShowPredictionsRequestHandler&quot; for each user

Definitely ...
 
Thanks for your trouble sedj.

I checked the code and it was returning a new RequestHandler for each user, but I had some static variables in an abstract servlet that were being overwritten during the execution of the servlet.

I made these non-static and it's OK now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top