简体   繁体   中英

Messing up with response headers in a Servlet

In a Servlet what happens If the developer messes up with the headers.

Let's say client's HTTP version is 1.0 and in the request is not present a connection header,
inside my servlet I can do the following:

resp.setHeader("connection","keep-alive");

I also can set content-length to a specific value
and then send way less or more data than the specified value

I'm sure exsist other situations that can cause similar "problems" as well.

How are these problems handled?

This all handles on client side, and depends on browser, and vary from one to another.

But I suppose that described situation with connection header will be hadled very simple: the browser with HTTP 1/.0 only is not supporting keep-alive connections, so will simply close connection afer each request.

In general, it is unpredictable. In most cases the relevant specifications don't say what should happen. And even when they do, there is no guarantee that the respective server-side (or client-side) will do what is specified ... or advised by the spec.

Let's say client's HTTP version is 1.0 and in the request is not present a connection header, inside my servlet I can resp.setHeader("connection","keep-alive");

The client probably should just ignore the connection response.

I also can set content-length to a specific value and then send way less or more data than the specified value.

I recall seeing in the Servlet spec that the server-side framework is supposed to close the response automatically if the webapp writes more bytes than the content-length . But if the webapp writes less, there is nothing that the server-side can do. The client simply has to cope with this. The situation is indistinguishable from the case where the server crashes.

How are these problems handled?

It is generally implementation dependent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM