Note that there are some explanatory texts on larger screens.

plurals
  1. PODiffrerent users have access the same variables in my servlet
    primarykey
    data
    text
    <p><br/> 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:</p> <pre><code>&lt;a href="MyServlet?check"&gt;Some Html Code&lt;/a&gt; &lt;a href="MyServlet?show"&gt;Some Html Code&lt;/a&gt; </code></pre> <p>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:<br/><br/><strong>MyServlet</strong></p> <pre><code>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); } } </code></pre> <p>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... <br/><br/>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..<br/><br/>What should i do? thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload