Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating requests from servlets  RSS feed

 
Chomik Rufus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

(FYI I asked a similar question here: http://www.coderanch.com/t/493784/Tomcat/Filtering-creating-outgoing-requests but no luck)
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the HttpClient library to send a request and read the result; the servlet API itself has no support for this.
 
Charles 'King
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf's response covers a multitude of sins. This is how servlets invoke web services, which may be served up by the same server, a different server in the same machine (for example, a PHP-based web service in Apache httpd), or a server on some other machine.

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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rogel garcia
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have Servlet A, from this Servlet A you want to dispatch to Servlet B wich is in the same server? Is that your question?

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

 
Chomik Rufus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm familiar with servlets and I know that's not a standard use of them. The problem is basically this kind:

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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the previous answers didn't you understand?

Making a web service request from a servlet is no different than doing it any other way.
 
rogel garcia
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Continue the chain as you would do...

But, use a wrapped response... to save the data to a byte[]... like i said in the other answer...

As David mentioned
Making a web service request from a servlet is no different than doing it any other way.


 
Chomik Rufus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A request is a request, regardless of its source: if it's not running through your normal request processing, something's wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!