-2

I have a simple JSP page that has a form taking value of parameter val

<form action="test">
Insert Value<input type="text" name="val"/>
<input type="submit"/>
</form>


Next I have a servlet test.java which takes val and prints it. The servlets doGet method looks like this-

        String val=request.getParameter("val");
        response.setContentType("text/html");
        PrintWriter p=response.getWriter();
        p.println("Value of parameter is "+val);


Now, I've read that during initialization only one instance of servlet is created i.e. only one object of servlet is created and multiple requests are handled using Multiple Threads.

Now, since only one instance of servlet is created therefore only one instance of variable val must have been created which can hold just one value at a time, then why is it that during multiple requests each user can access its own value of val ?
Shouldn't the value of val be shared among multiple requests and reflect its change in corresponding requests ?
Please Help. Thanks.

Shivam Aggarwal
  • 734
  • 2
  • 9
  • 23
  • I can see that you are storing the value of val element in a variable as per below line String a=request.getParameter("val"); And in the end you are printing the val attribute itself: p.println("Value of parameter is "+val); Can you please share where exactly you are assigning this variable. – Aman Chhabra Jun 28 '16 at 15:40
  • Did you mean `a` to be called `val` in your second snippet? Do you know what local variables are and how they relate to each thread's stack? – Sotirios Delimanolis Jun 28 '16 at 15:40
  • @AmanChhabra sorry it was my mistake. I've corrected the code now. – Shivam Aggarwal Jun 28 '16 at 15:45
  • @SotiriosDelimanolis Yes exactly. I know what local variables are but not how they relate to each thread's stack. Please explain. – Shivam Aggarwal Jun 28 '16 at 15:46
  • 2
    http://stackoverflow.com/questions/12825847/why-are-local-variables-thread-safe-in-java – Sotirios Delimanolis Jun 28 '16 at 15:47
  • @SotiriosDelimanolis thanks alot. Much helpful. Now if I define a variable outside my function i.e. inside my class then will it also get shared or will get allocated on a separate thread frame. – Shivam Aggarwal Jun 28 '16 at 15:50
  • your question is answered in answers to the question Sotirios linked to, like [this one](http://stackoverflow.com/a/12825906/217324). – Nathan Hughes Jun 28 '16 at 16:03

3 Answers3

1

Besides the fact that there is just one servlet instance, each HTTP request is executed inside a thread so each execution of doGet has it's own call stack. The following command creates a local variable on that stack:

String a=request.getParameter("val");

So, for each thread there is a call stack, so each thread has its own version of this variable.

Leonardo Cruz
  • 1,189
  • 9
  • 16
0

I'm not sure of your code : val is not defined ? Where do you run those lines of code ?

Assuming val is :

String val=request.getParameter("val");

And you are in a method like :

public void doStuff(HttpRequest request){
   ...
}

Each call have a unique request as parameter, so a unique "val" parameter.

J.Mengelle
  • 341
  • 3
  • 11
0

So now as you have corrected the code. For every request, you are updating the value of val attribute which will be updated by the latest request:

String val = request.getParameter("val");

So, if you want to retain the value ensure you are increasing the scope of this variable to class, make it volatile and then while assigning you are verifying if it has any existing value in it.

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
  • Sorry, but not exactly what I'm trying to ask. I mean to ask that since there will be only one instance of servlet and hence only one instance of val variable, so how is it possible for different users to access val as per their requests. Basically how is val storing different values in spite of just having one instance. – Shivam Aggarwal Jun 28 '16 at 15:56
  • val will not be having single instance as its scope is within a function. So whenever that function will be called a new instance of val attribute will be created. – Aman Chhabra Jun 28 '16 at 16:04
  • Also, if you increase the scope of val to class, even then it can have different values for different requests as for each request you are updating the value of val. So if you define the val in class instead of function and then while assigning the value check if it is having any existing value, it will have same value for all requests. – Aman Chhabra Jun 28 '16 at 16:06