0

i have a simple Jsp page that hits to servlet and in the servlet i call a method from another class and in this method i am declaring a static variable globally and setting a value to it and then the servlet's task is over so the control is back to the jsp page (or to a page that i forward the request and response to).

so wat just happened is termed a session???

the value set to that static variable remains the same for all the sessions that are coming next!! why is this happening. dint the earlier session end ?? if it has ended, then why is the value for the static variable that i have set is still remaining like that only in my subsequent sessions?? please correct me if i am wrong. Help me to learn! stackoverflow has never let me down!!!! thanks in advance

Vasanth Nag K V
  • 4,860
  • 5
  • 24
  • 48
  • So you don't know what static variables are? Maybe you shouldn't be working with JSP just yet. Go read the basic tutorials where such things are explained, otherwise you won't be able to understand anything. – Kayaman Apr 11 '14 at 15:08
  • hey kayaman!! i know wat a static variable is, i mean i know that it is a variable that is a single copy to all the objects of its class. OHHH wait a second, i know what you are trying to say!! the object i created or the value that i set to a static variable does not get destroyed once the session ends!???? – Vasanth Nag K V Apr 11 '14 at 15:11
  • Only session variables die when the session ends. Static variables live inside the class, not the session. – Kayaman Apr 11 '14 at 15:14
  • Dont use static varaible to hold session information. set the instance in session. So that once the session destroyed.the object will destroyed – Mani Apr 11 '14 at 15:25

1 Answers1

3

static fields in a class will live until the class itself is unloaded and garbage collected. So, static fields in a serlvet will not only live across all the sessions but across the whole application, in this case, until the web application is undeployed.

In fact, it is not wise to have any field in a servlet unless this field cannot be modified after being initialized or if it is injected by the container like an EJB or a CDI bean. This is because a single Servlet instance will be used to attend several requests made to the server, so even if you have a non-static field in your servlet and you update it through requests, its value can be modified by two or several requests happening at the same time. Try to keep the variables to the shortest possible scope, for example, inside a method only.

More info:


From comments, looks like your real problem is about a design to support synchronization across several threads. A better option would be creating an object instance that will be shared among your threads, then use a final non-static field to handle the synchronization:

class MyClass {
    final Object lock = new Object();
    //other fields in the class...
}

class Multijobs {
    class Job implements Runnable {
        MyClass myClass;
        public Job(MyClass myClass) {
            this.myClass = myClass;
        }
        @Override
        public void run() {
            //handle the job here...
            //using the synchronization point
            synchronize(myClass.lock) {
            }
        }
    }
    static final int NUM_THREADS = 10;
    public void executeSeveralJobs() {
        ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
        MyClass myClass = new MyClass();
        executorService.execute(new Job(myClass));
        executorService.execute(new Job(myClass));
        //initialize the jobs and add them to the ExecutorService
        //...
        executorService.shutdown();
        //...
    }
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • hi Mendoza, i do not have the static variable in the servlet class. i have it in a class that the servlet calls , this is still the same issue what you have explained?? – Vasanth Nag K V Apr 11 '14 at 15:19
  • @VasanthNagKV if is about `static` fields, then yes. – Luiggi Mendoza Apr 11 '14 at 15:20
  • oh thank you!! but my situation here is kinda hard, i have to mnake the field static only because, the class where it is declared is accessed by many threads and each thread is creating an object of this class and checking for the value of the static variable. only one thread out of these(the thread that wins the first race), changes the value of this thread and does task1 and all other threads just check this value and decided to do task1 or task2 . hope i was clear when i explained my situation!! – Vasanth Nag K V Apr 11 '14 at 15:23
  • @VasanthNagKV why not using a single instance of this object and synchronize on a `final` non-`static` field instead? – Luiggi Mendoza Apr 11 '14 at 15:25
  • @VasanthNagKV no, just create a single instance of this class in your method, then when creating your threads (by passing a `Runnable`, creating the `Thread` manually, using `ExecutorService` or whatever other way you do) pass this object instance (**never** a singleton) and use a `final` field that will be shared by all the threads as the synchronization point. – Luiggi Mendoza Apr 11 '14 at 15:28
  • okay now the example you have given is very similar to what i have here, except that, the actual task (file access) happens in the MyClass and the run method calls a method in the MyClass class. now i am creating just a single instance of the class and passing it the service. – Vasanth Nag K V Apr 11 '14 at 15:56