aspose file tools*
The moose likes Servlets and the fly likes Calling Servlet method from Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling Servlet method from Bean" Watch "Calling Servlet method from Bean" New topic
Author

Calling Servlet method from Bean

Bob Hodges
Greenhorn

Joined: Feb 02, 2007
Posts: 10
I'm planning on using a ServletContextListener to start a poll cycle when a web app is started. These polls would continue whether any user was logged into the web app itself.

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.
Bob Hodges
Greenhorn

Joined: Feb 02, 2007
Posts: 10
Moderator, can you possibly move this question to the Servlet Forum for me?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

Bob,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

From Jeanne: (instantiate the servlet in Java and call the method.)

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.


Steve
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
I agree with Steve Luke.

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.

Regards.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
Bob Hodges
Greenhorn

Joined: Feb 02, 2007
Posts: 10
To all,

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()
<filter>

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The first thing to clear up is that the ServletContextListener is a Listener, not a Servlet. Even if you make it an implementation of javax.servlet.Servlet and map it to a URL, the Listener will be a new instance that doesn't act like a Servlet at all... so it is better to consider the ServletContextListener as a Listener and not as a Servlet.

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:
Bob Hodges
Greenhorn

Joined: Feb 02, 2007
Posts: 10
Luke,

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?
Bob Hodges
Greenhorn

Joined: Feb 02, 2007
Posts: 10
Oops, Steve. My apologies
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.
Bob Hodges
Greenhorn

Joined: Feb 02, 2007
Posts: 10
Steve,

Coded something up using your sample and it works perfectly. My thanks for the help and also clearing up the pointer/reference issue. You've made my day
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling Servlet method from Bean