(FYI I asked a similar question here: http://www.coderanch.com/t/493784/Tomcat/Filtering-creating-outgoing-requests but no luck)
Chomik Rufus wrote:Is there any kind of way to generate HTTP request within a servlet, dispatch it to the server and get back the answer delivered to the servlet? Or are the servlets meant only to respond to passed requests, not generate them?
Servlets are web components that run on the server (in the container). I'm not sure what your asking, "dispatch it to the server."
A servlet's main objective is to "serve" requests. If your problem doesn't include the container receiving a request from the client, then it likely servlets aren't the solution to your problem. The container calls the servlet's service method for each request it receives. Here's an oldie, but goodie. It should be required reading for all Java developers: http://java.sun.com/products/servlet/whitepaper.html
If you look at the API, you'll notice there's only one setter method for ServletRequest interface and none for HttpServletRequest. The servlet expects the container to pass the request to the its service method.
Coding is exactly the same as how a stand-alone Java app would issue and respond to an HTTP request/response. The only consideration is that the extra time it takes to run the request/response means that the servlet will itself respond slower to requests made to it, since request/response is a synchronous activity.
Chomik Rufus wrote:(FYI I asked a similar question here: http://www.coderanch.com/t/493784/Tomcat/Filtering-creating-outgoing-requests but no luck)
I don't find that to be a similar question at all: this question is about making requests from a servlet (easy, as mentioned). The other one talks about intercepting outbound requests to make sure they don't bypass intercepting mechanisms--personally, I don't understand that question as it's worded.
Maibe you could use this in Servlet A:
You would need to create a fake response that writes the data to some array (ByteArrayOutputStream) or something like that...
But, I suggest to reorganize your architeture because it is not common and probably there are better ways to do that... maybe creating a class with functionality that both ServletA and ServletB could call
1. There are my services running on Tomcat (so they're Servlets/JSPs).
2. I have to intercept requests to and responses from these services - done using filters.
3. The interception routine (filter code) must invoke some specific web services (using WSDL) methods in reaction for the catched request/reponse <- how to do that?
4. My services running on Tomcat must have the ability to invoke OTHER services on their own (nesting calls), and after that send back the response (which comes through my filter) <- I have to be able to intercept those nested calls as well
So the questions are: how can I make a separate request and get a response from my servlet code (not only respond for requests) AND how to filter those "outgoing" (from servlet container perspective) requests?
Ulf Dittmer wrote:You could use the HttpClient library to send a request and read the result; the servlet API itself has no support for this.
Thanks for the suggestion, I used the Apache HttpClient library and it works pretty well. Can I intercept this request/response just like I did with the servlet filter? Is there a way to do this in Tomcat or similar container/server/whatever? I have to take control over ALL requests/responses made by applications hosted on the server, and modify them as needed.
Neither my custom servlet filter nor my Tomcat Valve sees communication established by HttpClient. If you're sure it's not possible, could you please give me any suggestion, how can I achieve my goal using other techniques?