yaylitzis
yaylitzis

Reputation: 5554

Diffrerent users have access the same variables in my servlet


Firstly, thanks for reading my question.. I am newbie in programming servlets, and i have come up with this problem: In my webApplication differrent users have access in the same variables and this is sth that i dont want to happen. I have the sense that i haven't construct My webApplication well so i will present it. In my JSP pages when i want to call the servlet to do some process, i always call it with this way:

<a href="MyServlet?check">Some Html Code</a>
<a href="MyServlet?show">Some Html Code</a>

I chose this way because i wanted to pass FROM the jsp TO the servlet a parameter (in this case "check", in order to inform the servlet "hey you, the user clicked the button check") - (can i do this with another way?) Anyway! So, in MyServlet i have written this:

MyServlet

import javax.servlet.http.HttpServlet
//i import and many others..

public class MyServlet extends HttpServlet{
    private int count1; //these are the variables that see all the users
    private String Title;

    protected void processRequest(HttpServletRequest request, HttpServletResponse
           response)throws ServletException, IOException {

           if(request.getQueryString().equals("check")){
                 //do some stuff and then put a value (its not random) in the count1
                 count1 = 10; //lets say this value its 10 for a user1.
                 request.setAttribute("count", count1);           
                 RequestDispatcher disp = getServletContext().getRequestDispatcher("/page1.jsp");
                 disp.forward(request, response);
           }
           else if (request.getQueryString().equals("show")){
                 //do some stuff and then put a value in the count1
                 title = "title"; //same here
                 request.setAttribute("title", title);
                 RequestDispatcher disp = getServletContext().getRequestDispatcher("/page2.jsp");
                 disp.forward(request, response);
           }
     }

So in MyServlet i have nested if-else statements for all the links in my jsp. As i said in the beginning all the users in my application have access in the same variables. So if user1 after he clicked the button check the variable count1 takes a value 10, and then an other user2 clicks the same buttons and the variable takes an another value (e.g 20), then the user1 has also the value 20...

I tried to put the definitions of the variables inside the method processRequest but then i have to initialize the variables firstly, as the IDE Enviroment i use, alert me that in the lines i use these variables that the variables may not have been initialized.But i dont want to init the variables, because everytime i call the servlet all the variables init and i loose the value that had before..

What should i do? thanks a lot!

Upvotes: 1

Views: 236

Answers (2)

Mark Thomas
Mark Thomas

Reputation: 16660

In most Servlet containers there will be a single instance of your Servlet class that serves all requests for all users.

If you want variable per user then you'll need to create an HTTP session and store the variables there. Something like

HttpSession session = request.getSession(true);
Integer count = (Integer) session.getAttribute("count");
if (count == null) {
    count = Integer.valueOf(10);
}
// Do stuff with count
session.setAttribute("count", count);

Upvotes: 2

Tomas Narros
Tomas Narros

Reputation: 13488

The servlet engine creates and keep a single Servlet instance, thread unsafe, for all the web application life.

This means, that every property set at the servlet level, will be shared between all the threads (calls, users...) that access these functionality.

So you never should set Servlet attributes to handle request or session values.

Adiotionally, to send parameters via GET, you should inform them as a key/value collection.

This way, you'll be able to acccess these parameters through the getParameter() method of the Request.

So, applying to your code:

<a href="MyServlet?action=check">Some Html Code</a>
<a href="MyServlet?action=show">Some Html Code</a>

Now, you are sending a parameter called "action" to the Servlet. To retrieve it:

public class MyServlet extends HttpServlet{

   //removed instance level properties

    protected void processRequest(HttpServletRequest request, HttpServletResponse
           response)throws ServletException, IOException {

           String action=request.getParameter("action");
           //add some validation code here on the "action" value
           if(action.equals("check")){
                 //do some stuff and then put a value (its not random) in the count1
                 int count1 = 10; //lets say this value its 10 for a user1.
                 request.setAttribute("count", count1);           
                 RequestDispatcher disp = getServletContext().getRequestDispatcher("/page1.jsp");
                 disp.forward(request, response);
           }
           else if (action.equals("show")){
                 //do some stuff and then put a value in the count1
                 String title = "title"; //same here
                 request.setAttribute("title", title);
                 RequestDispatcher disp = getServletContext().getRequestDispatcher("/page2.jsp");
                 disp.forward(request, response);
           }
     }

And that's it.

Upvotes: 2

Related Questions