Servlet notes-JSP, EL expression, JSTL tags

JSP

Java Server Page

A special page, which is equivalent to HTML+java code
running on the server side, is not actually a page. There is a request in Tomcat for processing url-pattern as *.jsp. The corresponding jsp file will be found and the jsp file will be converted into a servlet class. , Process the data in this servlet, complete the assembly of data and html, and finally return to the browser an assembled static HTML page

Used to simplify writing

JSP script

  1. <% code %>: Will appear in the service method of Servlet finally, what code can be executed in the service method, what code can be written here
  2. <%! code %>: The position where the member variable will be defined in the Servlet at the end
  3. <%= code %>: equivalent to output statement, will output the expression in code to the page

JSP built-in objects (a total of 9)

variable name Real type effect
pageContext PageContext Sharing data on the current page, you can get 8 other objects
request HttpServletRequest A request
session HttpSession Session of a session
application ServletContext Context of the current web application
response HttpServletResponse Response of a request
page Object(this) Current servlet object
out JspWriter Output stream object
exception Throwable abnormal

out: Character output stream, which can output the page. Similar to response.getWriter()

  1. response.getWriter() will output before out no matter where it is defined. This is because tomcat will first look for the response buffer and then the out buffer of the jsp built-in object. Therefore, the data in the response buffer will always be output before out
  2. So in JSP pages, use out as much as possible, and output consistently

JSP instructions

<%@ 指令名称 属性1=值1 属性2=值2 ...%>

There are 3 types

  1. page: Used to configure JSP pages
    1. contentType: equivalent to response.setContentType
    2. import: import java package
    3. errorPage: the page to jump to after the current jsp execution of java code is abnormal
    4. isErrorPage: Identifies whether the current jsp page is an error page
      1. true: you can use the built-in object exception
      2. false: the default value, the built-in object exception cannot be used
  2. include: used to include other jsp pages
  3. taglib: Import JSTL tag library and other resources

EL expression

  1. Definition: Expression Language

  2. Role: Replace and simplify the writing of java code in JSP pages

  3. grammar:${EL表达式}

  4. note

    1. JSP supports EL expressions by default

    2. If you don’t want to parse the EL expression, you can add it to the header of the JSP file

      <%@ page isELIgnored="true" %>
      

      In this way, the EL expression of the entire page will be displayed as a string

      You can also add a backslash before the EL expression to ignore a single EL expression

      \${4 > 3}
      
  5. Use of EL expressions

    1. Do calculations

      1. Arithmetic operators:+ - * / mod

      2. Comparison operators: > < >= <= == !=

      3. Logical operator: && || !orand or not

      4. Null operator: empty

        Null operator, mainly used to determine whether the string, collection, array is null, and the length is 0

        ${empty list} When a property named list in the judgment domain is null or its size is 0, the expression is true

        ${not empty list} Determine whether it is not null, or the length>0

    2. Value

      1. EL expressions can only get values ​​from domain objects (in the built-in objects of JSP, only 4 objects are domain objects)

      2. grammar

        1. ${域名称.键名} Get the key value from the specified domain object

          1. Domain name

            1. pageScope -> pageContext
            2. requestScope -> request
            3. sessionScope -> session
            4. applicationScope -> application
          2. Example: name=zhangsan is stored in the request field

            Then ${requestScope.name}you can get zhangsan. If the EL expression does not get a value, it will be an empty string

        2. ${键名} : By default, the domain object with the smallest range will be searched for the value corresponding to the key in turn until it is found.

          If the key names stored in different domain objects are different, you can simplify the writing without specifying the domain name

      3. Get the property value in the object

        ${域名称.键名.属性名} -> essentially call the getter method of the object

        <%
        	User user = new User();
        	user.setName("yogurt");
        	user.setAge(23);
        	user.setBirthday(new Date());
        	request.setAttribute("user",user);
        %>
        
        <h3>
            获取对象中的属性值
            <!-- 同归对象的属性来获取
        	getter方法,去掉get,将剩余部分首字母变小写,就可以,类似于BeanUtils对类设置属性时的机制,它找的是set/get方法
        	-->
        </h3>
        ${user.name}
        ${user.age}
        
      4. Get the value in List

        ${域名称.键名[索引]}

      5. Get the value in the Map

        ${域名称.键名.key名称}

        ${域名称.键名.["key名称"]}

    3. Implicit objects of EL expressions

      A total of 11, similar to the built-in objects of JSP, just learn 1

      1. pageContext

        Similar to the pageContext of JSP, you can directly obtain the other 8 objects of JSP

        ${pageContext.request.contextPath}You can get the virtual directory of the project. Dynamically obtain virtual directories.

        Remember, redirection is to allow the client to re-initiate a request, so add a virtual directory. The forwarding is performed on the server side, already in the context of the project, and its current directory is already a virtual directory, so there is no need to set a virtual directory

JSTL

  1. Concept: JavaServer Page Tag Library JSP standard tag library

    • It is an open source and free jsp tag provided by the apache organization
  2. Role: used to simplify and replace the java code on the jsp page

  3. Steps for usage:

    1. Import JSTL related jar packages

    2. Introduce tag library: use taglib instruction

      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
    3. Use label

  4. Commonly used JSTL tags

    1. if: equivalent to java if statement

      Attributes:

      • test: required attribute, receiving boolean expression (usually combined with EL expression)

        If the expression is true, display the contents of the tag body

        <c:if test="${not empty list}">
            开始遍历...
        </c:if>
        

        Note that if tag does not correspond to else, if you want to implement else, you need to write another if

    2. choose: equivalent to the switch statement in java

      choose

      when

      otherwise

    3. foreach: equivalent to java for loop

      Attributes

      • begin value
      • end end value
      • var temporary variable
      • step
      • varStatus status, you can get the current iteration position

Guess you like

Origin blog.csdn.net/vcj1009784814/article/details/106084589