0

I'm new to using Servlets so please forgive me if I use incorrect terminology. I have an Object called "Provider" in JSF Bean Class "Detector" which needs to be instantiated once and then can be used for all other requests. I've done some searching and found the ServletContextListener interface which seems to do what I need. Ive mentioned it in my web.xml file like so:

<listener>
    <listener-class>
        p1.ContextListener
    </listener-class>
</listener>

and the class looks like this:

package p1;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextListener implements ServletContextListener{

   @Override
   public void contextInitialized(ServletContextEvent sce) {
      Detector.startProvider();
   }

   @Override
   public void contextDestroyed(ServletContextEvent sce) {
     Provider.dispose();
   }  
}

And here is my Detector Class:

package p1;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;


@ManagedBean
public class Detector{

    private static Provider p;
    FacesContext context;
    String userAgent;

    public Detector() {
        context = FacesContext.getCurrentInstance();
    }

    public String getValue() {
        return p.getValue();
    }

    public String getUserAgent() {
        return ((HttpServletRequest) context.getExternalContext().getRequest()).getHeader("User-Agent");
    }

    public static void startProvider(){
        p = Creater.create();
    }
}

My code all works, but the only way that seems right to me is to have the Provider Object as a static but that seems like a bad idea in an Bean that will be used for different requests. My question is whether it is right to have the Provider Object as a static?

Chris B
  • 925
  • 4
  • 14
  • Your question is confusing. You're not using a servlet. You're using a JSF backing bean. As to the concrete problem, this can't be answered as long as the concrete functional requirements are unclear. We have no idea what `Detector`, `Provider` and `Creater` are supposed to do. But, generally, using `static` this way is frowned upon in enterprise multithreaded applications. You should rather "just create one". – BalusC Sep 05 '12 at 14:31

2 Answers2

1

Using "static" is a bad idea. If you want an object in your servlet to be shared between all the HTTP requests processed by this servlet then simply made it a field of your servlet class. The best place for initialization of that field variable is init() method.

public class MyServlet extends HttpServlet {
    private MyProdiver provider;

    public void init() throws ServletException {
        this.provider = new MyProdiver();
        // do init
    }
}

Unless your servlet class implements SingleThreadModel there is only one servlet instance per servlet declaration in your deployment descriptor (web.xml)

Alex Vayda
  • 6,154
  • 5
  • 34
  • 50
0

I found the answer I need on this question JSF initialize application-scope bean when context initialized. I set the Provider Object as an attribute of the ServletContextEvent in my "ContextListener" and retrieved it in my Detector class from my FacesContext Object "context". (This is shown in more detail in the accepted answer of the link provided)

Community
  • 1
  • 1
Chris B
  • 925
  • 4
  • 14