users@glassfish.java.net

Re: invoke servlet from EJB w/o posting ?

From: <glassfish_at_javadesktop.org>
Date: Thu, 15 Nov 2007 18:40:55 PST

> I can't just instantiate the servlet's class because
> since its resources probably wouldn't get initialized.

Really? Have you tried?

The contract between the container and the servlet are pretty minimal.

You need to call "init", "service", and "destroy".

The "hard part" is fabricating an effective replacement for HttpRequest and HttpResponse (as well as filling in the appropriate bits, like for the ServletContext, the HttpSession). But I bet you can contrive a pretty limited environment (just null everything out and then fill the blanks until the NullPointerExceptions stop :-)).

Obviously, you don't get (ready) access to the servlet runtime, for the utility functions, but the classes are there in the runtime if you want to shadow them (need to dig in to the source for the details, but they're all Tomcat/catalina classes -- depends on what it wants to use).

Do you simply not want to expose it on a live socket so you can just make an HTTP call to it?

It will be a little bit of work to come up with a "fake" servlet environment, but it's not unheard of, and that's the only thing stopping you from calling the servlets service method directly. Really depends on how complicated the servlet itself is. Perhaps you can punt on the servlet entirely and call it's children classes directly? (Assuming it's just a facade.)

I'd just play with it, and hack away until the stacktrace stop and the results start showing up. JNDI might be a bit of a trick tho...
[Message sent by forum member 'whartung' (whartung)]

http://forums.java.net/jive/thread.jspa?messageID=245750