-1

I am developing a Java Web Application which consists of a lot of JSPs, JSs, Servelts, while business logic is handled in POJOs.

My need is to initiate a class inside my Servlet, and set is as a global variable, and use it throughout all the packages in my application.

Now, I have defined a few global variables using

request.getSession().setAttribute("<ATTR_NAME>", <MY_OBJECT>);

But what I am lacking here is I can use the value of this attribute only inside the Servlet, or in other words, places where I have access to HttpServletRequest.

I need a solution to define a global variable, and be able to use it in all my POJOs which contain my business logic, where I may not have access to HttpServletRequest. I also do not want to pass this global variable of mine from the Servlet to all these POJOs either through Constructors or method arguments.

I have gone through a few articles which spoke about ServletContext amongst other things, but I do not get a clear picture.

Any little help is highly appreceated.

Thanks, Sriram Sridharan

Sriram Sridharan
  • 720
  • 18
  • 43
  • possible duplicate of [How do you store Java objects in HttpSession?](http://stackoverflow.com/questions/5766521/how-do-you-store-java-objects-in-httpsession) – Abhijeet Dhumal Jul 06 '15 at 10:09
  • @abhijeetdhumal Not even close to being duplicates. – Anthony Grist Jul 06 '15 at 10:11
  • possible duplicate of [Retrieving Web Session from a POJO Outside the Web Container](http://stackoverflow.com/questions/4764285/retrieving-web-session-from-a-pojo-outside-the-web-container) – Shekhar Khairnar Jul 07 '15 at 04:27

3 Answers3

-1

Why don't you initiate a Java Bean in your base servlet and use it throughout the runtime?

public class User implements java.io.Serializable {

private String name;
private Integer age;

public String getName(){
    return this.name;
}

public void setName(String name){
    this.name = name;
}

public Integer getAge(){
    return this.age;
}

public void setAge(Integer age){
    this.age = age;
}

}

Jeeppp
  • 1,553
  • 3
  • 17
  • 39
-1

One solution can be create a load-on-startup servlet which set the context as in the article in link access context in DAO/POJO or another solution is using filter as in link session from pojo

Community
  • 1
  • 1
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
-1

If you want to use some object in application context may use some context holder (singletone) and store object in the ServletContext's attributes:

Context holder:

public class CtxHolder {
    private static ServletContext ctx;
    private static Context nCtx;

    private DSHolder() {
    }

    public static void setCtx(ServletContext ctx) {
        DSHolder.ctx = ctx;
    }

    public static ServletContext getCtx() {
        if (ctx == null) {
            throw new RuntimeException("Need to set servlet context");
        }
        return ctx;
    }
}

Then define Servlet Context Listener and invoke CtxHolder.setCtx() (from contextInitialized() method) when application starts.

Then you can get/set "global" (application's scope) attributes as:

CtxHolder.getCtx().setAttribute("key", new Object());

But remember about synchronization! Each request processes in own thread.

Constantine
  • 482
  • 3
  • 10