• Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpURLConnection from a filter  RSS feed

 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a Servlet filter that forwards a 'copy' of incoming requests to another Servlet based app. This would allow the stream of requests coming in to a production server to also be sent to an application under test on another server.

I was planning to do this with HttpURLConnection in a filter. The filter would modify the incoming url to make it the url of the application under test. All of these urls are gets so this part is simple. For this type of testing I don't care about the response, I just want to subject the application under test to the same load as the production site. So I don't want the filter to wait for a response and tie up the original request. In my filter do I just do nothing after calling HttpURLConnection.openConnection() or do I need to call HttpURLConnection.disconnect()?

The other half of this test scenario would be filter on the application under test that just throws away the response after some logging. I'm also unsure about this, how does a Servlet just swallow a response and send nothing out?

Thanks for any help, advice and/or references to how to do this,
-=beeky
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Stafford:
... The other half of this test scenario would be filter on the application under test that just throws away the response after some logging. I'm also unsure about this, how does a Servlet just swallow a response and send nothing out?


One you close the ServletOutputStream or the PrintWriter, the connection will be closed. As long as you don't try to write to it after that, you can do anything else in your servlet's doPost method that you want.

If you run the following servlet while tailing the server's logs, you'll see the browser connection finish right away. Five seconds later, you'll see your servlet write to the log.




Note: If you comment out lines 17 through 19, the servlet will wait until the end of the doPost method before completing the transaction with the browser.
[ January 28, 2008: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that you might want to keep in mind, when writing your filter, is that the body of the request stream is not actually parsed until you call either getRequestStream, or one of getParameter, getParameterNames, getParameterMap, or getParameterValues.Once you do this you're committed to using one or the other.

In other words, if you call getReqeustStream in your filter before passing the request to one of your servlets, it will not be able to call any of the getParameterXxx methods, and vice versa.

If all your application does is handle plain HTTP GETS and POSTS with form-encoded data, you're OK, just don't call getRequestStream from your filter. If, on the other hand, your app has to handle multipart posts (file uploads) or parses large XML posts, your filter might make the request useless to the servlet behind it.
 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
Thanks for the great info and example.
-=bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!