What are implicit objects? What does it mean?

前端 未结 7 1579
谎友^
谎友^ 2020-12-30 01:52

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 instantiat

相关标签:
7条回答
  • 2020-12-30 02:17

    JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.

    JSP supports nine Implicit Objects which are listed below:

    • request : This is the HttpServletRequest object associated with the request.
    • response : This is the HttpServletResponse object associated with the response to the client.
    • out : This is the PrintWriter object used to write any data to buffer.
    • session : This is the HttpSession object associated with the request.
    • application : This is the ServletContext object associated with application context.
    • config : This is the ServletConfig object associated with the page.
    • pageContext : This encapsulates use of server-specific features like higher performance JspWriters.
    • page : This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
    • Exception : The Exception object allows the exception data to be accessed by designated JSP.
    0 讨论(0)
  • 2020-12-30 02:21

    Implicit objects are created automatically and are ready for you to use. You cannot create other variables with same name with these objects.

    0 讨论(0)
  • 2020-12-30 02:22

    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

    0 讨论(0)
  • 2020-12-30 02:34

    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.

    0 讨论(0)
  • 2020-12-30 02:39

    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

    0 讨论(0)
  • 2020-12-30 02:40

    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

    0 讨论(0)
提交回复
热议问题