JSP self use notes

JSP

JSP (full name JavaServerPages) is created by [Sun Microsystems]( https://baike.baidu.com/item/Sun Microsystems led the creation of a Dynamic web technology Standard. JSP deployed on the web server can respond to the request sent by the client and generate dynamically according to the request content HTML,XML Or other format documents Web The page is then returned to the requester. With JSP technology Java Language as scripting language , for the user's HTTP Request to provide services, and can handle complex business requirements together with other Java programs on the server.

JSP page elements

  • jsp annotation
Syntax:<%-- This is jsp notes --%>
jsp Comments will not be resolved
  • Small script
Syntax:<%  These are some Java code
    	 int i=0;
         System.out.println(i);
         System.out.println(getString());
         print();
     %>
Appear in_jspService Method
  • jsp expression
Syntax:<%=value    %>
Output the specified value to the page
  • jsp declaration
Syntax:<%! Define methods and properties
    	public Stirng name;
    	public String getString(){
       		return "This is a method that returns a string";
      	}
     %>
Members of the class generated after parsing(Methods and variables)

JSP instruction

Syntax: <% @ instruction type instruction attribute% >

  • page
    • contentType (text type of web page, for example: text/html;charset=UTF-8)
    • language code
    • pageEncoding (configure the character encoding of the current page)
    • isELIgnored (whether EL expressions are supported)
  • include: import resources
    • file (resource path)
  • taglib: import tag library
    • Prefix (prefix when referencing)
    • uri (path of tag library, for example: uri)=“ http://java.sun.com/jsp/jstl/core ”)

Nine built-in objects of JSP

  • pageContext: page context
  • Request: request
  • Response: response
  • Session: session
  • application: application
  • config: Configure
  • out
  • Page: page
  • ServletException: exception

Four scopes of JSP

  • pageScope: page field (pageContext [built-in object])
  • requestScope: request domain (request [built-in object])
  • sessionScope: session domain (session [built-in object])
  • applicationScope: application domain (application [built-in object])

EL Expression Language

EL is to make JSP easier to write. It provides a method to simplify expressions in JSP, which makes the code of JSP more simplified.

Syntax: ${}

EL expression is used to obtain corresponding content from the four scope objects of JSP

When the EL expression takes values from the domain object, the default query starts from the smallest scope,
If it is not found, continue to find the scope of the previous level and look up level by level. If it is not found, the string "" is returned. You can also specify the scope for searching

[] and operator
  • **.** You can point attributes directly

  • [] for collections and arrays

JSTL

JSTL (Java Server Pages standardized tag library, i.e JSP standard tag library )By JCP (Java community processes), which mainly provides Java Web developers with a standard general label library, which is maintained by Apache's Jakarta team. Developers can use these tags to replace the tags on JSP pages Java Code, so as to improve the readability of the program and reduce the difficulty of program maintenance.

Use the jar package that needs to import the response. The core package is JSTL Jar, dependent package: standard jar

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
core tag library
  1. c:out
<c:out value="${user.userName}" />
  1. c:set
<c:set target="${user.userName}" property="userName" value="Jack" />
  1. c:remove
<c:remove var="user" scope="session" />
  1. ⭐c:if
<c:if test="Judgment conditions" >
    <c:out value="Output of conditional execution" />
</c:if>
  1. c:choose c:when c:otherwise
<c:choose>
    <c:when test="Judgment conditions">value</c:when>
    <c:when test="Judgment conditions">value</c:when>
    <c:when test="Judgment conditions">value</c:when>
    <c:otherwise>value</c:otherwise>
</c:choose>
  1. ⭐c:forEach
<c:forEach items="Traversal object" var="Obtained value" begin="Starting subscript" end="End subscript" step="step" varStatus="state v">
    v.index:subscript  v.count:Which value of the current set
    Execute statement
</c:forEach>
  1. c:redirect redirect
<c:redirect url="index.jsp" />
  1. c:url is the URL
<c:url   value="http://www.baidu.com" />
Format label
SQL tag
XML tag
JSTL function

Keywords: Java Web Development JSP

Added by jusitnm on Tue, 08 Feb 2022 15:08:23 +0200