1

Im interesting in Creating a Global Object which will be shared by all my servlets and all my JSP files. But i dont understand how to do it. Please advice.

for a senario this object would contain lots of information my servlets and jsp files would like to take information from. I know how to pass objects between servlets and jsp's. But i dont know how to initialize 1 global object for the whole "system" or" website"


<display-name>WebTest1</display-name>
<listener>
    <listener-class>listeners.AppListener</listener-class>
</listener>
<welcome-file-list>
    <welcome-file>MainPage.html</welcome-file>
    <welcome-file>MainPage.htm</welcome-file>
    <welcome-file>MainPage.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>SimpleServlet</servlet-name>
    <servlet-class>servlets.SimpleServlet</servlet-class>
</servlet>

1 Answer 1

4

You should store the object inside application context. You can do this when the application starts using ServletContextListener.

public AppListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        //application is being deployed
        //register the "global" object here
        ServletContext sc = sce.getServletContext();
        MyGlobalClass globalObject = ...
        sc.setAttribute("globalObject", globalObject);
    }
    public void contextDestroyed(ServletContextEvent sce) {
        //application is being undeployed
    }
}

Register this class as a listener in web.xml:

<listener>
    <listener-class>package.where.defined.AppListener</listenerclass>
</listener>

Then you can access to this object in both Servlet and JSPs:

Servlet:

public void doGet(...) throws ... {
    ServletContext servletContext = request.getServletContext();
    MyGlobalClass globalObject = (MyGlobalClass)servletContext.getAttribute("globalObject");
    //use it...
}

JSP:

${globalObject.someField}
Sign up to request clarification or add additional context in comments.

15 Comments

in this implementation, say i have 10 servlets. how exactly all those servlets know my global object? And when does AppListener runs? When i start my server it runs?
@user1804659 the listener will be fired automatically when the application is deployed and undeployed. The ServletContext is a global object for the whole application. Every servlet may access to ServletContext by using HttpServletRequest, you can access to any object in JSP using Expression Language (that thing inside ${}).
Now its much CLEARER! Thanks alot mate. I will try this on and see what comes of it!!!
Luiggi, I want to ask u another question if its ok. If i can access my Object in the JSP file, why would i want to use a servlet? When i can clearly write what ever code i wish in the JSP file ?
@user1804659 you asked how can you access to this object in Servlets and JSP, and I showed how you can. If you need to access to this global object in one or in others, will entirely depend on your specific functional requirements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.