What if you want more information from the request?

What if you want, say, the server host information that comes with the “host” header in the request? If you look in the HttpServletRequest API, you can see a getHeader(String) method. We know that if we pass “host” to the getHeader() method, we’ll get back something like: “localhost:8080” (because that’s where the web server is).

Getting the “host” header

We know we can do it with scripting

Host is: <%= request.getHeader("host") %>

But with EL, we’ve got the header implicit object

Host is: ${header["host"]}

Host is: ${header.host}

Note

The header implicit object keeps a Map of all the headers. Use either access operator to pass in the header name and the value of that header will print. (Note: there’s also a headerValues implicit object for headers with multiple values. It works just like paramValues.)

Getting the HTTP request method

Uh-oh. This is a little trickier... there’s a method in the HttpServletRequest API for getMethod(), that returns GET, POST, etc. But how do I get it using EL?

We know we can do it with scripting

Method is: <%= request.getMethod() %>

But with EL, this will NOT work

Method is: ${request.method}

Note

NO! NO! NO! There IS no implicit request object!

And this will NOT work

Method is: ${requestScope.method}

Note

NO! NO! NO! There IS an implicit requestScope, but it’s NOT the request object itself.

Can you figure out how to do it?

Hint: look at the other implicit objects.

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.