jsp's built-in object, scope and servlet are briefly introduced

1. 9 built-in objects of JSP (used directly without creating)

pageContext, page, page

Request completes a request and the response object responds

Session one session, application the whole server, exception exception exception, out output object, config configuration

2.4 large scope (different scope of storage attributes < scope >)

Scope from small to large: pagecontext ------ request ------ session ------ application

The four scopes have the following two methods:
pageContext.setAttribute(); or setAttribute(); Method of setting property value
pageContext.getAttribute(); or getAttribute(); Method to get property value
  • page

    A page cannot be used directly. It is completed by pageContext, and the properties are valid on a page

    <%--a.jsp--%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%--pageContext Three properties are set, which can only be accessed on the current page--%>
    <%
        pageContext.setAttribute("msg","correct");
        pageContext.setAttribute("name","Zhang San");
        pageContext.setAttribute("date",new Date());
        %>
    <%--<%--%>
    <%--    String msg=(String)pageContext.getAttribute("msg");--%>
    <%--    String name=(String)pageContext.getAttribute("name");--%>
    <%--    Date date=(Date)pageContext.getAttribute("date");--%>
    <%--%>--%>
    <%--<%=msg%>--%>
    <%--<%=name%>--%>
    <%--<%=date%>--%>
    <%--Server jump--%>
    <jsp:forward page="b.jsp">
        
    

    getAttribute(); The return value of is of type Object, which should be transformed downward when used

    <%--b.jsp--%>
    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        String msg=(String)pageContext.getAttribute("msg");
        String name=(String)pageContext.getAttribute("name");
        Date date=(Date)pageContext.getAttribute("date");
    %>
    <%=msg%>
    <%=name%>
    <%=date%>
    <%--function b.jsp The result is null null null--%>
    
  • request

    In one request, attributes can be transferred across (one) page (associated page) or obtained through multiple page jumps

    For example, page a sets the value, page a jumps to page b through the server, page b jumps to page c through the server, and writes the obtained value code request on page c Getattribute(), page c can get the value of page A. if page b does not jump to page c, page c cannot get the value of page a (such request forwarding is meaningless in actual development).

    The client jump failed to get the value in the request

    <%--a.jsp--%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        request.setAttribute("msg2","Correct 2");
        request.setAttribute("name2","Zhang San 2");
        request.setAttribute("date2",new Date());
        %>
    <a href="b.jsp">b.jsp</a> <%-- Client jump failed to get value--%>
    <%--<jsp:forward page="b.jsp"/>--%><%--The value can be obtained by server jump--%>
    
    <%--b.jsp--%>
    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        String msg2=(String)request.getAttribute("msg2");
        String name2=(String)request.getAttribute("name2");
        Date date2=(Date)request.getAttribute("date2");
    %>
    <%=msg2%>
    <%=name2%>
    <%=date2%>
    <%--function b.jsp The result is null null null--%>
    
  • session

    A session saves the information of one user. The information of each user is independent of each other. Both client-side and server-side jumps can obtain values. The default destruction time of a session is 30 minutes.

    There are three ways to destroy a session: 1 Manual destruction; 2. The request timeout is 30 minutes by default; 3. Close the browser

    1. Manual destruction;

    <%--a.jsp--%>
    <%
        session.setAttribute("msg3","Correct 3");
        session.setAttribute("name3","Zhang San 3");
        session.setAttribute("date3",new Date());
      %>
    <%--<jsp:forward page="b.jsp"/>--%>
    <a href="b.jsp">stay b Page get value</a>
    
    <%--b.jsp--%>
    <%
        String msg3=(String)session.getAttribute("msg3");
        String name3=(String)session.getAttribute("name3");
        Date date3=(Date)session.getAttribute("date3");
    %>
    <%
        out.print("Get value:");
    %>
    <%=msg3%>
    <%=name3%>
    <%=date3%>
    <%
        out.print("\t\t Destruction:");
    %>
    <a href="c1.jsp">Manual destruction</a>
    
    <%--c.jsp--%>
    <%
    session.invalidate();//Manual destruction
    %>
    <a href="b.jsp">Check whether the value can be obtained after destruction</a>
    <%--<jsp:forward page="b.jsp"/>--%>
    

    Execute and click, and the execution results are as follows: Get value on b page ="Obtained value: correct 3 sheets 3 Wed Jan 19 21:20:56 CST 2022 destruction: Manual destruction=>Check whether the value can be obtained after destruction ===Get value: null destroy: Manual destruction

    Failed to get value after destruction

    2. The request timeout is 30 minutes by default;

    Find the installation path of Tomcat used, and open the web. Config in this directory XML file (open with Notepad), find

    30

    Change to the desired default time and restart the server.

    3. Close the browser

  • application

    Running in the whole server, the server will always exist if it is not shut down< Data storage in application is the most memory consuming > (the instance is similar to the other three)

**Note: * * the above attribute values exist in the server, but follow a principle in actual development, that is, if page can complete the request, do not use request, if request can complete the request, do not use session, and if session can complete the request, do not use application, so as to improve the performance of the server

3. The difference between server-side jump and client-side jump

Server jump: the address bar information remains unchanged

<jsp:forward page="Jump.jsp"/>
<%--If possible, in the code/Don't omit it, because I omit that there is an error in the code here--%>

Client jump: it will change the address bar information

<a href="">Jump.jsp<a>
<script>
window.location.href="Jump.jsp";
</script>

4. Introduction and use of Servlet

Servlet is a class that runs on the server and processes the client's requests and responses.

Life cycle of Servlet

​ 1. When loading the Web container, create a Servlet and initialize the operation through init() (it can only be initialized once, that is, performance: the first operation of the Servlet is slow)
​ 2. Handle client requests through service() or doGet() or doPost()
​ 3. Destroy the servlet by shutting down the server or not calling for a long time

Create Servlet: 1 Initialization < init(); > 2. Process client requests (request method: service() service scheduling, doGet(), doPost())3 Destroy servlet(destroy() method)

Schematic diagram of Servlet operation

@WebServlet("/HelloServlet")//Configure servlet
public class HelloServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("establish servlet,Complete initialization");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Processing client requests");
    }

    @Override
    public void destroy() {
        System.out.println("Destroy servlet");
    }
}
/*On the execution page, add "/ HelloServlet" to the address bar. After accessing, you can see the output "create servlet and complete initialization" on the code console
 "Process client requests"
*/

Keywords: Front-end html server

Added by manishsinha27 on Thu, 20 Jan 2022 18:53:13 +0200