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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access