Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are implicit objects? What does it mean?

Tags:

java

jsp

servlets

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

like image 579
giri Avatar asked Jan 21 '10 22:01

giri


People also ask

What are explicit objects?

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.

Which of the following is an implicit object?

The available implicit objects are out, request, config, session, application etc.

What are implicit objects available to the JSP page?

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.

What is not an implicit object?

E vector and in are not implicit objects.


1 Answers

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.

like image 115
BalusC Avatar answered Oct 03 '22 19:10

BalusC