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>

Eran
  • 125
  • 1
  • 11

1 Answers1

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}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 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? – Eran Jun 13 '14 at 17:47
  • @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 `${}`). – Luiggi Mendoza Jun 13 '14 at 17:50
  • Now its much CLEARER! Thanks alot mate. I will try this on and see what comes of it!!! – Eran Jun 13 '14 at 17:51
  • 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 ? – Eran Jun 13 '14 at 18:12
  • @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. – Luiggi Mendoza Jun 13 '14 at 18:16
  • yes u did. i just wanted to know logically why i would wanna use servlets (for my education) :] – Eran Jun 13 '14 at 18:17
  • @Eran for example, you use Hibernate and load a list of countries in `AppListener#contextInitialized` and store them in `Map countryMap` (because it is extremely rare a new country appears :) ). Then, you use this map to fill a ` – Luiggi Mendoza Jun 13 '14 at 18:24
  • My great savior Luiggi, when im running my app, it works. but when im adding the listeners.AppListener to the XML file, it doesnt. its writing HTTP 404, The requested resource is not available. What is wrong? – Eran Jun 13 '14 at 19:08
  • @Eran please post how you added the listener in your web.xml file by editing your question. – Luiggi Mendoza Jun 13 '14 at 19:48
  • @Eran looks like you got an error while creating the object in your listener. Try to wrap the code with a `try-catch` block and print the stacktrace to check which error occurs. – Luiggi Mendoza Jun 13 '14 at 19:55
  • Again, u are totally right, was a stupid mistake. thanks alot man u are so helpfull!!!!!!!!! – Eran Jun 13 '14 at 20:18
  • @Eran please don't forget to mark this post as an answer. – Luiggi Mendoza Jun 13 '14 at 20:52
  • What do u mean? Where do i do that? – Eran Jun 13 '14 at 21:23
  • Luiggi, when i try to use my global object as u told me above i do the folloing: ${db.forums.get(1).getName()} but it doesnt work. do i need to do some kind of decleration before? maybe <% %> or somthing like that ? – Eran Jun 13 '14 at 21:38
  • @Eran refer to [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197). Also, please refer to [Expression Language](http://stackoverflow.com/tags/el/info). – Luiggi Mendoza Jun 13 '14 at 21:49