What I'd like to do is to have the page backing bean call a method in the servlet for the status and then filter values that this user does not wish to (or should) see (e.g. polling 10 machines but user only wants to see 5). The servlet is created (it's writing start/stop messages to the logs) but I can't seem to find an example on how to call a method from a bean to a servlet.
Are you trying to call the method as a Java method or through a URL? If it's a Java method, you call it the same way as any other method. (instantiate the servlet in Java and call the method.) If it's through a URL, take a look at the URLConnection class.
Generally not a good idea with Servlets. The servlets should be handled by the servlet container, or they won't behave in a servlet-like manner (no servlet context or servlet config, no resquest or response, no session, ...). If you can do the job by just instantiating the servlet and calling the method then you should re-factor the method into a stand-alone POJO. If you absolutely need the servlet and the servlet's environment, then your servlet could pass itself into the bean to let the bean call a method on it - as a form of dependency injection. But this also suggests that the servlet is doing more than just controlling the web application and the work it is doing is better re-factored into another class, that Object built by the servlet with the data the servlet knows about, and passing this new business object into the bean.
Based on your question, you are probably using JSF in your implementation (based on the use of "backing bean").
May I ask why the managed bean should call a method in servlet?
If the servlet were to be used as objects with methods that will not handle response rendering, then refractoring them to plain POJO is a good alternative since the control flow will reside in JSF.
If the servlet were to handle processing and response rendering, then you have to delegate the control to the servlet by calling dispatch in the ExternalContext object of JSF implementation.
thanks for the input and let me clarify what I'm trying to do. First, yes it's JSF with backing beans. Per the documentation, an application or session bean will only be instantiated when it's first called, not when the application actually starts.
So, here's the scenario: Use a ServletContextListener (which will be started with the application) and create a polling thread within it to poll the devices in scope. It would store the results within a class variable and then results could be displayed by the pages based upon a filter (static or ajax updated). Eg.
(display page) <-> backing bean <-> query ServletContextListener.method()
From what I've read (I'm new to web programming if you haven't figured that out), with the servlet spec 2.x, the context.getServlet is deprecated so I can't find the servlet that way to call a method. Also, if I'm reading it correctly, a ServletContextListener does not have a URL I can call?
So, I think (input would be appreciated)I could:
1) Have the Listener continuously update a context variable with a status object and have the bean read that?
2) Have the Listener call an application scope bean at startup and that would instantiate the application bean where I could put the poll thread. That bean is easily found (e.g. getBean()). However, I don't know how to do this. If there's some sample code I'd appreciate a pointer
3) Call the servlet results/add device/remove device method directly (my first choice). But I don't know how to find it within the application scope (e.g. get.Listener).
My thanks for any and all input
A common approach to solve this is to have the ServletConextListener put itself in the application scope (ServletContext) that it is listening to. That way any object with access to the application scope can access the data the Listener is providing.
I don't know JSF, so I don't know the quickest route to the ServletContext. A quick look around google gave me this:
So you might have a Listener that looks like this:
and code inside your bean that looks like this:
Thank you so much for the guidance/example, that makes it perfectly clear. Being new to Web programming I took the documentation at its word that "Servlet" was the correct term. However, the documentation sure seemed to differ from the normal servlet functionality (URL, POST/GET, Etc.)
If I may ask one more question? The servlet context is a global hash table for the application where you can store and/or pass information. Coming from a "C" backgroud, is the "this" I'll store into context similar to a pointer, or is it a copy of the object? The reason I ask is that in other examples (say DB connection objects created at startup), they pass the created conn object into the context and I'm not sure whether it's a copy or it's a pointer/reference to the variable maintained within the class?
Coming from a "C" backgroud, is the "this" I'll store into context similar to a pointer, or is it a copy of the object? The reason I ask is that in other examples (say DB connection objects created at startup), they pass the created conn object into the context and I'm not sure whether it's a copy or it's a pointer/reference to the variable maintained within the class?
The value that gets stored is a reference value - not quite the same as a pointer in C but as close as Java gets to one. The object does not get copied, the amount of storage space required for storing the reference is roughly equivalent to storing an integer.
Oops, Steve. My apologies
Hehe, no problem. Happens about 20 times a day :-) The drawback of having 2 first names I guess.