JSP-- nine hide objects of the four domain objects

  You must have used the following nine objects in your document Jsp monitoring this segment of several: out, config, page, pageContext, exception, request, response, application, session. Like this:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- Jsp脚本片段:用来在里面写Java代码-->
    <%
        for(int i=0;i<10;i++){
            out.print(i);
        }
    %>
    
    <-! JSP expression: to want to output object browser -> 
    <% = " the I'm JSP expression The "  %>
    
    <-! JSP nine implicit objects -> 
    < h1 > Welcome, <% = request.getParameter ( " username " ) %> </ h1 > 
</ body > 
</ HTML >

  Objects used in out script fragment above paragraph tag, but this is how the object come out of it? Previous Jsp about the underlying principles that we introduced, Tomcat servlet that will produce the corresponding jsp for each file, which is in the servlet class, to help us create this long-hidden objects, in which the exception rather special, only when you specify a isErrorPage = "true" in the Jsp file, Tomcat servlet class will be created for this purpose with the exception object jsp.

 

   Although the use of hidden object displayed in the jsp files, but actually use it in the corresponding Servlet class, so all jsp files hidden objects are evidence-based. In the nine subjects, there are four more specific, and also more important, they are called domain objects.

  They are: page, request, session, application . The following describes the differences of the four: <% @ Page Language = " Java " contentType = " text / HTML; charset = UTF-. 8 "

    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!--
        Four domains:
            page domain
                Range: the current page
                Corresponding domain objects: pageContext
                Type domain object: PageContext
            request domain
                Range: the current request (first request)
                Corresponding domain objects: request
                Type domain objects: HttpServletRequest
            session域
                Scope: The current session (first session)
                Corresponding domain objects: session
                Type domain object: HttpSession
            application域
                Range: the current Web application
                Corresponding domain objects: application
                Type domain object: ServletContext
            Four domain objects are the following three methods:
                void setAttribute(String key , Object value)
                Object getAttribute(String key)
                void removeAttribute(String key)
            Four domain objects using rules:
                Do not use small large    
     -> 
     <! - add the current page to the four domains are four attributes -> 
     <% 
         pageContext.setAttribute ( " pageKey " , " pageValue " );
         request.setAttribute("reqKey", "reqValue");
         session.setAttribute("sessKey", "sessValue");
         application.setAttribute ( " AppKey " , " appValue " );
      %> 
     < h1 of > getting a property value of the four fields in the current page </ h1 of > 
     attribute values page domain is: <% = pageContext.getAttribute ( " pageKey " ) %> < br > 
     attribute value domain request is: <% = the request.getAttribute ( " reqKey " ) %> < br > 
     attribute values session scope is: <% = session.getAttribute("sesskey " ) %> < br > 
     attribute value field of application is: <% = application.getAttribute ( " AppKey " ) %> < br > 
     <-! forwarded to scope2.jsp page -> 
<% -       < JSP: Forward page = " /scope2.jsp " > </ JSP: Forward >  - %> 

< A the href = "/ Web_JSP / scope2.jsp" > to scope2.jsp page </ A > </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

     < H1 of > getting a property value in the four domains scope2.jsp page </ h1 of > 
     attribute values page domain is: <% = pageContext.getAttribute ( " pageKey " ) %> < br > 
     attribute domain request value is: <% = the request.getAttribute ( " reqKey " ) %> < br > 
     attribute value domain session is: <% = session.getAttribute ( " sesskey " ) %> < br > 
     attribute value field of application is : <%=application.getAttribute("appKey") %><br>
</body>
</html>

 

  In this page above, are provided to the four values ​​in the object field, the page will find the four open domain object of the present page can be taken out of the corresponding value, and the present page by page can not be removed forwarded pagevalue, the reason is page scope only in this domain page, other pages can not get the value of this page page object.

And if you jump forward instead, that once again send a request to the server, you will find the second requested page pagevalue and reqValue have taken out because of the scope of the request object is limited to the first request, it has been able to in the above the second page to get warm Value, because although the page jump, but jump the way is "forward", so in essence, is a request. If at this time and then another browser to access the page number two, you can only get to the value of the application, the reason is session scope is limited to one session. And after restarting the server, and then access the page number two, four values ​​are not available.

 

  

  

Guess you like

Origin www.cnblogs.com/superlsj/p/11834366.html