1

I have server running in JBOSS. there is a Filter which alistenes to all the requests i.e all the requests come to the Filter and are passed on to other Servlets from here. I have noticed this :

When I use the following code, only the filter is invoked but the control is not passed on to the respective Servlet(The filter prints the correct servlet when I print using request.getRequestURI(). It also prints correct value of the request headers username and password)

 HttpURLConnection connection=gs.getconnection("send_user_detail");
          connection.setRequestProperty("user", gs.get_login_id());
          connection.setRequestProperty("password", gs.get_pass());
        connection.setRequestProperty("timezone", TimeZone.getDefault().getDisplayName());
            connection.connect();

BUT when I use the following code, the control is passed on to the respective Servlet and works fine.

 HttpURLConnection connection=gs.getconnection("send_user_detail");
          connection.setRequestProperty("user", gs.get_login_id());
          connection.setRequestProperty("password", gs.get_pass());
        connection.setRequestProperty("timezone", TimeZone.getDefault().getDisplayName());
            //connection.connect();
 ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
          out.writeObject("string"); //some random string not used in the servlet


So the control is only passed on to the servlet when I write something on the OutputStream. But with connection.connect(), it still goes up to the filter and even prints the correct name of the requested Servlet. What is the reason?

Ashwin
  • 10,386
  • 29
  • 103
  • 172

2 Answers2

3

Writing to request body in URLConnection implies a HTTP POST request.

Your servlet is apparently doing the job in doPost(), not in doGet().

You need to perform the job in doGet() if you want your servlet to act on HTTP GET requests.

This has completely nothing to do with filters. You'd have exactly the same problem when removing the filter.


Unrelated to the concrete problem, the connection.setRequestProperty() lines sets request headers, not request parameters. Make sure that you aren't abusing headers as parameters (bad design). Request parameters should in case of POST be written as URL-encoded query string in the request body.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • No. actually it is the opposite. I am implementing doPost(). Thanks. just change the answer to the opp. I will accept it. – Ashwin Nov 12 '12 at 12:54
  • Right, mixed it. Fixed the answer. – BalusC Nov 12 '12 at 12:55
  • Seen that post of yours a million times. That is how I came to learn all the http requests and all. Just was a little careless here:) – Ashwin Nov 12 '12 at 12:56
  • Please see the answer update about using `setRequestProperty()`, you seem to be actually willing to pass request parameters, not request headers. – BalusC Nov 12 '12 at 12:58
  • yeah, you are right about that. But the reason I am putting it in header is that. The header is only used in the filter and I don't want to access the body in the filter. – Ashwin Nov 12 '12 at 13:09
  • Uh, it's just available by `request.getParameter()` like as in servlet .. ? – BalusC Nov 12 '12 at 13:10
  • The body actually contains an object. I am using ObjectOutputStream to pass the object. – Ashwin Nov 12 '12 at 13:12
  • Ouch .. What importable. Good luck with future maintenance. I'd really consider standard and more portable formats before it's too late. – BalusC Nov 12 '12 at 13:15
0

Filters work in a chain. The filter contract is;

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException

Generally a filter will do some work and call

chain.doFilter(request, response);

Note that the filter can do its work before or after the filter chain call. Code before the call is invoked on the way in, before the servlet. Code after the call is invoked on the way out, after the servlet.

Note that there can be many filters in the chain, and the chain is ordered. Any filter in the chain can miss out the filter chain call which has the effect of stopping the request from propogating further up the chain and eventually reaching the servlet.

I suspect in your case that this may be what is happening.

EDIT:
Just noticed that in your second example you have commented out the call to connection.connect(); maybe this is throwing an error in the first example?

Qwerky
  • 17,564
  • 6
  • 43
  • 76