From the course: Jakarta EE Servlets

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Obtaining data from headers

Obtaining data from headers - Jakarta EE Tutorial

From the course: Jakarta EE Servlets

Obtaining data from headers

- [Instructor] When writing the logic for a servlet, it can be useful to obtain information that an upstream system or the client places into the headers of a request. Let's take a look at how this can be achieved by adding some content negotiation logic into our servlets, which is one of the many ways that we can use a header. So within our product servlet, I'm going to add a bit of logic that will retrieve a header from the HTTP servlet request. And then based upon the value of that header, we'll conditionally render the output using different formats like JSON or XML. So the first thing that we need to do is we need to obtain that header. The header is going to be the accept header. It's used by clients to indicate their preference for the response format. Using a MIME type, they can say always return the response in JSON or XML. So to get that header, we can invoke the getHeader method on the HTTP servlet request.…

Contents