What are implicit objects? What does it mean?
Whenever I study JSP and Servlets I come across word im开发者_运维问答plicit objects, what does the term mean?
How they are called in my program without instantiating objects? Who instantiates implicit objects? Please elaborate.
Thanks
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.
Implicit objects in JSP are the objects that are created by the container automatically and the container makes them available to the developers, the developer does not need to create them explicitly. Since these objects are created automatically by the container and are accessed using standard variables; hence, they are called implicit objects.
The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration. Implicit objects are used for different purposes. Our own methods (user defined methods) can't access them as they are local to the service method and are created at the conversion time of a jsp into a servlet. But we can pass them to our own method if we wish to use them locally in those functions.
Source: roseindia.net
Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page. These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.
For a detailed overview and use please see the page below.
http://www.gulland.com/courses/JavaServerPages/jsp_objects.jsp
Implicit objects are created automatically and are ready for you to use. You cannot create other variables with same name with these objects.
JSP implicit objects are created by container while translating JSP page to Servlet source to help developers
There are the nine type of implicit object, Implicit Objects are also called pre-defined variables.
1) request
2) response
3) application
4) session
5) page
6) pageContext
7) out
8) exception
9) config
精彩评论