Whenever I study JSP and Servlets I come across word implicit objects, what does the term mean?
How they are called in my program without instantiating objects? Who instantiates implicit objects? Please elaborate.
Thanks
explicit--Explicit objects are declared and created within the code of your JSP page, accessible to that page and other pages according to the scope setting you choose.
The available implicit objects are out, request, config, session, application etc.
The JSP request is an implicit object which is provided by HttpServletRequest. In the servlet, we have to first import javax. servlet. http. HttpServletRequest then we have to create its object for taking input from any HTML form as.
E vector and in are not implicit objects.
Those are objects which are already been placed in the scope by the servlet container, so that it's accessible by EL (Expression Language), such as the PageContext
, HttpServletRequest#getParameter()
, HttpServletRequest#getHeader()
and so on. Those are just for convenience so that you don't need to use old-fahioned scriptlets to grab them.
So instead of for example
<%= pageContext.getSession().getMaxInactiveInterval() %><br>
<%= request.getParameter("foo") %><br>
<%= request.getHeader("user-agent") %><br>
<% for (Cookie cookie : request.getCookies()) { // Watch out with NPE!
if (cookie.getName().equals("foo")) {
out.write(cookie.getValue());
}
}
%><br>
you can just do
${pageContext.session.maxInactiveInterval}<br>
${param.foo}<br>
${header['user-agent']}<br>
${cookie.foo}<br>
You see that they follows the JavaBean conventions to be accessed (i.e. you can just invoke the getters the JavaBean way). You see that I used the brace notation []
to get the user-agent
, that's because the -
is a reserved character in EL, so ${header.user-agent}
isn't going to work, it would try to return the result of request.getHeader("user") - pageContext.findAttribute("agent")
which is obviously not going to work.
For an overview of them all, check the chapter Implicit Objects in the Java EE tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With