jsp------JAVA server page

JSP

JSP is a web page display technology launched by sun to replace html.
JSP is a technology based on the Java language, and it is a server language. It needs to be run through a server such as tomcat.
JSP is essentially a Servlet, a page representation of a Servlet, and a Servlet is the underlying implementation of jsp.
Jsp will eventually be parsed into a servlet by the tomcat server

JSP script instructions (understanding, no need to master)

<%= %> : output script
<% %> : define code snippets
<%! %> : define member properties and internal classes and methods

JSP page directive

The page command mainly sets some global configurations related to the page, and the encoding configuration is more commonly used

<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
  • pageEncoding: Set the page encoding method, mainly to solve the garbled Chinese characters in the jsp page
  • contentType : Set the page encoding method, mainly to solve the dynamic data returned by the backend in jsp
  • import : Import the class used, which is the same as the import usage of classes in Java
  • isELIgnored : Whether to ignore EL expressions, the default is false, usually no need to modify
  • errorPage: Used to specify the wrong page, if the jsp page generates an exception, it will jump to the page specified by errorPage
  • isErrorPage: Used to mark whether the current jsp page is an error page, if it is marked, you can use the built-in variable exception

EL expression

It is a unique expression in JSP pages. Its main responsibility is to output data and replace<%= %>

基本语法: ${key} 

The essence of ${key} is to use pageContext to call findAttribute(key) to find data from the four major scopes in turn and return the corresponding data
EL expression. If the obtained value is null, then when outputting in the web page, won't show anything

key is the key stored in the scope, by default, the corresponding key will be found from pageContext, request, session, application

Four scopes of JSP

The scope has three methods, setAttribute(key, value) , getAttribute(key), removeAttribute(key)

  • pageContext: It is a scope based on the page level, which is unique to JSP. The data stored in this scope can only be used in the current jsp

pageContext has a special method findAttribute(key), which will search for the corresponding key from pageContext, request, session, and application in turn.
If the key is found, it will return the value corresponding to the key, otherwise it will return null.

  • request
  • session
  • application

Use EL to get values ​​from the four major scopes

  • pageContext : ${pageScope.key}
  • request : ${requestScope.key}
  • session : ${sessionScope.key}
  • application : ${applicationScope.key}

EL expression to get property value in object

${obj.property}

obj represents the object stored in the scope, and property represents the getter property in the object

EL expression to get the key in the Map object

${map.key} 
${map[key]}

map represents the Map container stored in the scope, key represents the name of the corresponding key in the map, the key in map.key must meet the identifier name specification, and the key in map[key] can be any string

EL expression to get data in array/Collection

${array[index]} 
${list[index]} 

index represents the index, starting from 0

Use EL expressions to get the three major scopes

  • request
${pageContext.request}

Get project name: ${pageContext.request.contextPath}

  • session
${pageContext.request.session} 
${pageContext.session}
  • application
${pageContext.request.session.servletContext} 
${pageContext.servletContext}

EL expression to get request parameters

${param.key}  

${param} is used to obtain request parameters. If a key corresponds to multiple values, only the first value passed in can be obtained, and a Map<String, String> is returned

${paramValues} 

${paramValues} is used to get request parameters and return a Map<String, String[]>

Get request header information

${header} : 返回一个 Map<String,String> 
${headerValues} : 返回一个 Map<String,String[]> 

Get cookie information

${cookie}

JSTL tag library

In form, it is more similar to a web page, but in essence it is Java code
and EL expressions
. The jstl library is a third-party library that needs to be imported separately

  • C tag library
  • fn tag library
  • fmt tag library
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

C tag library

In the JSP page, use the taglib directive to import the C tag library

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  • c:set stores data in the specified scope through c:set
<c:set var=""  value=""  scope="page"></c:set>

var: used to set the key of the stored data, used to get the value, it will be automatically stored in the scope set by scope
scope: the default is page, representing the scope of data storage
value: used to set the value, which can be A literal value, which can also be obtained from an EL expression

  • c:out outputs data to the browser. Compared with the output of EL expressions, c:out supports escape processing of strings to prevent malicious attacks.
<c:out value="${username}" escapeXml="false" default="admin" />

value: used to set the content to be output
escapeXml: whether to escape the label contained in the string, the default is true
default: used to set the default value, when the value of value is null, the default value can be displayed

  • c:if label used for judgment
<c:if test=""  var="" scope="" />

test (master): supports EL expressions, supports relational operators, logical operators, supports empty (empty), not empty (non-empty) var: defines a
variable name to store the result returned by the test expression
scope: will In which scope is the variable corresponding to the result var of the test expression stored

关系运算符 :  > , >=  ,  < ,  <=  ,  !=  ,  == 

EL 中同时支持 gt(大于) , ge(大于等于),  lt (小于) , le (小于等于) ,  ne (不等于) ,  eq (等于)

逻辑运算符 :  &&  , ||  ,  !

EL 中同时支持  and ,  or ,  not 

  • c:forEach Label used for data loop
    • number traversal
  <c:forEach var=""  begin="1"  end="9"  step="1">
     ....
  </c:forEach>

var: the variable class used to set the counter of the loop, similar to i in the fori loop, and the variable will be automatically placed in the pageContext begin
: the value is a number, supports EL expressions, and represents the beginning of the
end: the value is a number 、Support EL expressions, which represent the end
step: set the frequency and step size of each increase, the default is 1, and the value cannot be negative

  • Array/Collection traversal
  <c:forEach var=""  items="" varStatus="vs">
  
  </c:forEach>

var: used to represent the variable corresponding to the elements in the collection/array, similar to the enhanced for loop
items: used to set the collection/array object to be traversed
varStatus: The value can be set arbitrarily, just satisfy the name of the identifier, varStatus corresponds to several A more important attribute
- index: the index corresponding to the data, starting from 0
- count: the position corresponding to the data, starting from 1, and index is always 1 different
- first: whether it is the first data
- last: whether it is the last data

  • c:choose realizes multi-branch condition judgment
<c:choose>
   <c:when test="">
   
   </c:when>
   
    <c:when test="">
   
   </c:when>
   
    <c:when test="">
   
   </c:when>
   
   ...
   <c:otherwise>
   
   </c:otherwise>

<c:choose>
  • c:forTokens splits the string according to certain rules and traverses it
<c:forTokens items="" delims=""  varStatus="" var="" ></c:forTokens>

items : the target string to traverse
delims : set the delimiter of the string to be split
var and varStatus usage reference c:forEach

  • c:url is used to define a label of the URL, which will automatically add a project name in front of the URL, replacing ${pageContext.request.contextPath}

fn (function) tag library

When displaying data on the page, some data may need to be processed, especially strings, such as hiding the middle four digits of the mobile phone number, desensitizing the ID number,
etc. Specially handled tag library
fn should mainly be in EL expressions

Import method

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  • fn:length Get the length of the Collection collection/string
${fn:length(users)}
  • fn:contains Determines whether the specified substring is contained in the string
${fn:contains('hello' , 'he')}
  • fn:containsIgnoreCase Case-insensitive judgment whether the string contains the specified substring, the usage is the same as fn:contains

  • fn:indexOf Gets the index position of the first occurrence of a substring in the string

  • fn:lastIndexOf Gets the index position of the last occurrence of a substring in the string

${fn:indexOf('hello' , 'e')}   --->  1
  • fn:startsWith Gets whether the string starts with the specified substring
  • fn.endsWith Gets whether the string ends with the specified substring
${fn:endsWith('D:/xxx.jpg',  '.png')}   ---> false
  • fn:substring intercepts a string
${fn:substring('hello' , 2 , -1)}    --->  llo
  • fn:toLowerCase returns the string in all lowercase

  • fn:toUpperCase returns the string in all uppercase

  • fn:trim removes spaces before and after the string

  • fn:replace replaces the content specified in the string

${fn:replace('hello' ,  'o' ,  'y')}   --->  helly 

fmt formatted tag library

It is mainly a tag library for formatting data, digital formatting, and date formatting

Import method

<%@taglib prefix="fmt uri="http://java.sun.com/jsp/jstl/fmt" %>
  • fmt:formatNumber
<fmt:formatNumber value="3.1415926" pattern="#.##" /> 

#.## retains two decimal places, # stands for retaining integers, and has the effect of rounding

  • fmt:formatDate
<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" /> 

Only supports java.util.Date date class, does not support the new version of JDK date class

JSP contains include directive

<%@include file="" %> 

The include command is a static inclusion technology in JSP, which directly introduces the included page into the current page, forms a large page as a part of the current page, and finally performs unified compilation. The same structure and content in the web page can be
included Stripping, extracting into a separate content, and importing in include

JSP dynamic include

<jsp:include page=“”>
<jsp:param name=“key” value=“” />

jsp:include is a dynamic inclusion technology in jsp, which introduces the compiled result of the included page into the current page to form a large page. Dynamic inclusion can
pass data to the included page through jsp:param, and in the included page The page can get the corresponding data through ${param.key}

Nine built-in objects of JSP

Nine built-in objects that can be used directly in java script instructions

  • pageContext , page
  • request , response
  • session
  • application
  • config : 是 ServletConfig
  • out : is the return value of response.getWrite
  • exception

am name=“key” value=“” />

jsp:include is a dynamic inclusion technology in jsp, which introduces the compiled result of the included page into the current page to form a large page. Dynamic inclusion can
pass data to the included page through jsp:param, and in the included page The page can get the corresponding data through ${param.key}

Nine built-in objects of JSP

Nine built-in objects that can be used directly in java script instructions

  • pageContext , page
  • request , response
  • session
  • application
  • config : 是 ServletConfig
  • out : is the return value of response.getWrite
  • exception

Guess you like

Origin blog.csdn.net/weixin_52953038/article/details/126984948