Is there a way to obtain the rendered output of a servlet from another servlet? I need to grab it and email it.
Thanks,
Rob
Is there a way to obtain the rendered output of a servlet from another servlet? I need to grab it and email it.
Thanks,
Rob
You’ll need to open a HTTPURLConnection to the servlet in question, and get the output back in a buffered reader/input stream… you can then do with it as you please.
That’s the simple answer.
If you want code, I can probably mock some up, but in short it is very do-able.
That is what I did. It works, but there is an issue with getting the host name and port. We are using a clustered environment with sticky sessions, so the request has to go to the same server that the current action is executing on or the jsessionid will be no good. Can it be done without going through http? In other words, can you get that by somehow invoking the servlet directly? I am actually using Struts 1.2.4, so I really need to know if it can be done using that.
Thanks for your help,
Rob
is the servlet code available to you in the classpath? If so, then you can probably just call the appropriate doStuff method within it, passing along the request/response…without knowing your code/environment it’s a little tuff to be too specific.
Is the code your executing on the same server as the code you wish to call? Have you considered just trying ‘localhost’ ?
But yes, you can call methods within other code directly (assuming you have access to it via the classpath).
simster, he’d need to somehow get a hole of a reference to the servlet object, which wouldn’t be easy.
I’m pretty sure you know what you’re saying, but I’m not sure you used the right words.
If he’s in a servlet/action, he has access to the entire request/response object(s), and all of the relevant items therein. From what I recall with Struts, sometimes getting to those bits was a little bit of a runaround, but they were still there.
I think the trick to what he is trying to do will be wether or not the “other servlet” was constructed in a way that he can access the method(s) that generate the output he desires directly, and wether or not the current action in fact resides on the same box as does the servlet in question… in a perfect world, he would be ble to do the HTTPURLConnection and pass along the jsessionid as an identifier and hang on to the session that he wants to work with… but I don’t know enough specifics about his environment or code to get into that level of detail.
Unfortunately, in the lastest version of J2EE, there’s no way to access the servlet handling the current request, let alone any other servlets in the container.
sweeteviljesus, are you sure that you need to go through a servlet? Could you get the output from an object that you share among multiple Actions?
In our production environment, we use a four-node cluster, each running JBoss 4.0.1. Each node is, I believe, running several apps, each configured to listen on a different port, and the port numbers aren’t the same for each node. We decided to go with my original solution, i.e. getting the URL and calling openStream on it, but to do that we needed a property which returns the actual servername and port of the machine that is handling the request. It is apparently a big pain in the ass for reasons unknown to me. localhost doesn’t work, but maybe that is because the port is wrong. It’s kind of academic now, as we needed to finalize the code for our release this week.
With respect to getting the rendered output from another servlet, I don’t think it can be done without either writing a third method that returns the rendered output for both (an ugly hack) or completely reworking Struts.
Thanks for all your help,
Rob