O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required