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}
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}
And this will NOT work
Method is: ${requestScope.method}
Can you figure out how to do it?
Hint: look at the other implicit objects.