One article is done

1, Why use JSP

The difference between HTML and JSP is like I have a huge wall, and then paste the advertising cloth on it in the traditional way. Today, Zhang San pays 10000 yuan to display his adult products advertisement, and tomorrow, Li Si pays 20000 yuan to display his advertisement for treating kidney deficiency... I have to change the advertising cloth every day???

No, just use the electronic screen!

  • HTML cannot display content dynamically
  • HTML cannot set variables
  • HTML also cannot make business logic judgment
  • Data cycling is not possible

You can respond to browser data through the response object, and control the response data through Java code in the servlet to achieve the effect of dynamic display. However, it needs to be displayed in a complete page, but the amount of code is large

JSP (Java server pages), a dynamic page running in the server, displays data through Java code

2, Components of jsp

1. HTML tags and text

**Parsing method: * * through out Write() response

2. jsp script fragment:

Format: <% java code% >

* * function: * * execute a piece of Java code

Resolution method:_ Directly in the servlet translated by jsp_ jspService() execution

3. jsp expression:

Format: <% = Data% >

* * function: * * output data to the page

* * parsing method: * * through out Print () responds to the browser

4. jsp declaration

Format: <%! Format;% >

* * function: * * declare a member variable in the servlet translated by jsp

* * resolution method: * * declare a member variable in the servlet translated by jsp

3, jsp principle

jsp is essentially a servlet

jsp is hidden in tomcat's work directory. The Java class and generated class translated by jsp file are distant cousins of a servlet, inheriting HttpJspBase, while HttpJspBase inherits HttpServlet

The class that jsp translates into is a servlet in the servlet that jsp translates into_ jsService(), pass the HTML code through out Write () responds to the browser and directly the code in the jsp script fragment in the_ Executed in jspService(), the content in the expression in jsp will pass through out Print () responds to the browser, and the jsp declaration will directly declare the corresponding member variables in the servlet into which the jsp is translated

4, Four domain objects of jsp

Method of operating database in domain object

void setAttribute(String attributeName,Object attributeValue)://Set data shared in domain objects
Objcet getAttribute(String attributeName);//Gets the data shared in the domain object
void removeAttribute(String attributeName);//Delete shared data in domain objects

1,pageContext

**Type: * * PageContext

Function:

  • Get the other eight implicit objects
  • Domain object, which can share data within one side

Usage scenario:

  • Most tags in jsp share data in pageContext by default

2,request

**Type: * * HttpServletRequest

Function:

  • Get request parameters
  • forward
  • Domain object, which can share data in the scope of one request

Application scenario:

  • Error message prompt
  • Query and display all data in the page
  • Data echo

3,session

**Type: * * HttpSession

Function:

  • Domain object, which can share data in the scope of one session (one session refers to the browser from opening to closing)

Application scenario:

  • Record user login status
  • Shopping Cart

4,application

**Type: * * ServletContext

Function:

  • Gets the initialization parameters of the current project
  • Gets the context path of the current project
  • Get the deployment path of the current project
  • Domain objects that share data across the entire project

Application scenario:

spring will share the IOC container object in the ServletContext

Next, let's look at the range in the domain object:

First, create two pages, write the data to be shared in the four domain objects in these two pages, and then do the following control experiment

<%--index page
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>This is index.jsp Page, setting up shared data</h1>
<%
    //Set the data shared by the four domain objects
    //pageContext. SetAttribute ("testpagecontext", "shared data set by pagecontext");
    request.setAttribute("testRequest","request Shared data set");
    session.setAttribute("testSession","session Shared data set");
    application.setAttribute("testApplication","application Shared data set");
    
%>
//<%=pageContext.getAttribute("testPageContext")%><br>
<%=request.getAttribute("testRequest")%><br>
<%=session.getAttribute("testSession")%><br>
<%=application.getAttribute("testApplication")%><br>
</body>
</html>




<%--test page
  Created by IntelliJ IDEA.
  User: Django
  Date: 2021/4/14
  Time: 18:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>This is test page</h1>
//pageContext:<%=pageContext.getAttribute("testPageContext")%><br>
//The pageContext object was intended to be tested, but an error will be reported in the idea. I don't know why. You can study it next
request:<%=request.getAttribute("testRequest")%><br>
session:<%=session.getAttribute("testSession")%><br>
application:<%=application.getAttribute("testApplication")%><br>
</body>
</html>

1. In index JSP to share data with the four domain objects, and observe whether the shared data can be obtained in the current page

2. In index JSP to share data with the four domain objects, and jump to test.jsp by forwarding JSP, in test JSP to see whether the shared data can be obtained

3. In index JSP to share data with the four domain objects, and jump to test.jsp through redirection JSP, in test JSP to see whether the shared data can be obtained

//By redirecting to test jsp
//response.sendRedirect(request.getContextPath()+"/test.jsp");


4. In index JSP to share data with the four domain objects, open a new tab and access test JSP, in test JSP to see whether the shared data can be obtained


5. In index Open one of the four big data sharing tabs in test.jsp JSP, in test In JSP, first observe whether the shared data can be obtained; Restart the browser and directly access test JSP, in test JSP and then observe whether the shared data can be obtained. After observing on the basis of the previous step, restart the browser and directly test jsp

6. In index JSP to share data with the four domain objects, open a new tab and access test JSP, in test In JSP, first observe whether the shared data can be obtained; Restart the server and directly access test JSP, in test JSP and then observe whether the shared data can be obtained

7. In index JSP to share data with the four domain objects, open a new tab and access test JSP, open a new browser and visit test JSP to observe whether the shared data can be obtained

summary

From the above comparisons, it is not difficult to summarize the following contents:

  • Forwarding can access the data in the request domain, while redirection cannot access the data in the request domain (redirection is a secondary request)
  • The data in the session domain object distinguishes the browser (corresponding to the 7th)
  • The data in the session domain object has nothing to do with whether the server is closed, but only with whether the browser is closed
  • The data in the application domain object has nothing to do with whether the browser is closed, but only with whether the server is closed

In fact: you only need to understand this logic - pageContext is to get the current page; In the request, if it is a jump, it is a request, which can obtain the data in the request domain, while redirection is equivalent to a second request, which is not a jump within the server and cannot obtain the shared data; Session. If the browser is not closed, You can get the shared domain data (session depends on cookie s. The passivation function enables data serialization to be saved even if the server is shut down. When the server is started again, it will be deserialized, also known as activation; while application is only related to the server, because only one domain object is created when the server is started, and the same object will be used as long as the server is not shut down

5, JSP instruction

Format of jsp instruction: <% @ instruction name attribute name = "attribute value" attribute name = "attribute value"% >

5.1. Page - set relevant information of the current page

Common attributes:

  • contentType: sets the content format of the current page
  • Import: import package
  • errorPage: if there is an exception on the current page, it will jump to the customized error page
  • isErrorPage: used to obtain the page exception information of the error in the current page when the property isErrorPage="true" is set on the error page specified in errorPage (in fact, these two are rarely used in actual development)
<%--
  Created by IntelliJ IDEA.
  User: Django
  Date: 2021/4/14
  Time: 19:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="isErrorPage.jsp"%>

<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    System.out.println(2/0);
%>
</body>
</html>



<%--
  Created by IntelliJ IDEA.
  User: Django
  Date: 2021/4/14
  Time: 19:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Show on current page errorPage Error message for</h1>
<%=exception.getMessage()%>
<%--
Eventually forwarded to errorPage.jsp,And displayed on the page/by zero
--%>
</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: Django
  Date: 2021/4/14
  Time: 19:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="isErrorPage.jsp"%>

<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    System.out.println(2/0);
%>
</body>
</html>



<%--
  Created by IntelliJ IDEA.
  User: Django
  Date: 2021/4/14
  Time: 19:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Show on current page errorPage Error message for</h1>
<%=exception.getMessage()%>
<%--
Eventually forwarded to errorPage.jsp,And displayed on the page/by zero
--%>
</body>
</html>

5.2,include

**Common attributes: * * file sets the path of the included page

6, JSP action tag

Format of JSP action tag: < jsp: tag name attribute name = "attribute value" attribute name = "attribute value" > < / JSP: tag name >

  • 1. Forward

    <jsp:forword page="/index.jsp"></jsp:forword>
    
  • 2. Dynamic inclusion

    <jsp:include page="/include.jsp"></jsp:include>
    

Static include:

<%@ include file="Included pages"%>

Only the content in this page is translated, not the included files, and it is implemented in the stage of converting JSP to Java

The contents of the included page will be copied to the main page and translated together

Dynamic include:

<jsp:include page="/include.jsp"></jsp:include>

This page and the included files are translated and added dynamically in the stage of executing class files

If there's something wrong, please give dad a lot of criticism; Gao Ming penetrates Kyushu and stretches his neck to look at the eight wastelands; I want to sleep m, see you next time!

Keywords: Java

Added by Heero on Mon, 07 Mar 2022 21:40:52 +0200