Today’s content

  1. JSP:

    1. instruction
    2. annotation
    3. Built-in objects
  2. MVC Development pattern

  3. EL expression

  4. JSTL tags

  5. Three layer architecture

JSP:

  1. instruction

    • Function: used to configure JSP page, import resource file
    • Format: <%@ Directive Name Attribute Name 1= Attribute value 1 Attribute name 2= Attribute value 2… % >
    • Classification:
      1. Page: configures the JSP page

        • ContentType: Equivalent to Response.setContentType ()
          1. Sets the MIME type and character set of the response body
          2. Set the encoding of the current JSP page (this only works with advanced IDES; if using low-level tools, you need to set the pageEncoding property to set the character set of the current page)
        • Import: package
        • ErrorPage: if an exception occurs on the current page, the system automatically switches to the specified errorPage
        • IsErrorPage: indicates whether the current page is an error page.
          • True: If yes, the built-in object exception can be used
          • False: no. The default value. The built-in object Exception cannot be used
      2. Include: included in the page. Import the resource file for the page

        • <%@include file=”top.jsp”%>
      3. Taglib: Imports resources

        • <%@ taglib prefix=”c” uri=”Java.sun.com/jsp/jstl/co…” %>
          • Prefix: user-defined prefix
  2. Note:

    1. HTML Comments: : Only HTML code snippets can be commented
    2. JSP comments: <%– –%> is recommended: you can comment all
  3. Built-in objects

    • Objects that do not need to be created in a JSP page and used directly
    • A total of nine: variable name true type effect
      • PageContext pageContext The current page shares data and can also get eight other built-in objects
      • Request HttpServletRequest Multiple resources accessed at a time (forwarding)
      • Session HttpSession Between multiple requests for a session
      • Application ServletContext Share data between all users
      • Response HttpServletResponse Response object
      • Page Object This is the Object of the current page (Servlet)
      • Out JspWriter outputs objects and data to the page
      • Config ServletConfig Configuration object of the Servlet
      • Exception Throwable Specifies the exception object

MVC: Development pattern

  1. JSP evolution history

    1. In the early days, only servlets could output label data using response, which was very troublesome
    2. Later JSP, simplified the development of Servlet, if the excessive use of JSP, in JSP that is to write a large number of Java code, write HTML table, resulting in difficult to maintain, difficult to division of labor cooperation
    3. Later, Java Web development, using MVC development mode for reference, makes the program design more reasonable
  2. MVC:

    1. M: That’s right. JavaBean
      • Perform specific service operations, such as querying databases and encapsulating objects
    2. V: View. JSP
      • Display data
    3. C: Controller: indicates the Controller. Servlet
      • Get user input
      • Invocation model
      • Give the data to the view for presentation
    • The advantages and disadvantages:
      1. Advantages:

        1. Low coupling, easy maintenance, can facilitate division of labor and collaboration
        2. High reusability
      2. Disadvantages:

        1. It complicates the project architecture and makes high demands on developers

EL expression

  1. Expression Language Expression Language

  2. What it does: Replace and simplify Java code writing in JSP pages

  3. Syntax: ${expression}

  4. Note:

    • JSP supports el expressions by default. If YOU want to ignore the el expression
      1. Ignored=”true” Set the page directive in the JSP to ignore all EL expressions in the current JSP page
      2. ${expression} : ignores the current el expression
  5. Use:

    1. Operation:

      • Operator:
        1. Arithmetic operator: + – * /(div) %(mod)
        2. Comparison operators: > < >= <= =! =
        3. Logical operators: && (and) | | (or)! (not)
        4. Air transport operator: empty
          • Function: Determines whether a string, collection, or array object is null, or whether the length is 0
          • ${empty list}: Checks whether a string, collection, or array object is null or has a length of 0
          • ${not empty STR}: checks whether the string, set, or array object is not null and has a length greater than 0
    2. Get the value

      1. El expressions can only get values from domain objects
      2. Grammar:
        1. ${domain name. Key name} : retrieves the value of the specified key from the specified field

          • Domain name:
            1. pageScope –> pageContext
            2. requestScope –> request
            3. sessionScope –> session
            4. ApplicationScope –> Application (ServletContext)
          • Example: Name = jack 3 is stored in the request field
          • Access: ${requestScope. Name}
        2. ${key name} : indicates whether there is a value corresponding to the key in the smallest field until it is found.

        3. Gets the value of the object, List collection, and Map collection

          1. Object: ${Domain name. Key name. Attribute name}

            • It’s essentially calling the getter method of the object
          2. List collection: ${domain name. Key name [index]}

          3. Map collections:

            • ${domain name. Key name. Key name}
            • ${name [“key “]}
    3. Implicit objects:

      • There are 11 implicit objects in the EL expression
      • The pageContext:
        • Gets the JSP’s other eight built-in objects
          • The ${pageContext. Request. ContextPath} : dynamic virtual directory

JSTL

  1. JavaServer Pages Tag Library JavaServer Pages Tag Library

    • Is an open source, free JSP tag provided by the Apache organization.
  2. Purpose: To simplify and replace Java code on JSP pages

  3. Use steps:

    1. Import JSTL related JAR packages
    2. Introduce the taglib directive: <% @taglib %>
    3. Use a label
  4. Commonly used JSTL tags

    1. If: Equivalent to the if statement of Java code

      1. Properties:
        • Test must be a property that accepts Boolean expressions
          • If the expression is true, the body of the if tag is displayed, if false, the body of the tag is not displayed
          • Typically, the test attribute value is used in conjunction with the EL expression
      2. Note:
        • The c:if tag does not have an else case. If you want an else case, you can define a C :if tag
    2. Choose: corresponds to the Switch statement of Java code

      1. Using the choose label declaration is equivalent to the Switch declaration
      2. Using the when tag is equivalent to case
      3. Declaration of other cases using an otherwise tag is equivalent to default
    3. Foreach: The for equivalent of Java code

  5. Practice:

    • Requirement: There is a List collection of User objects in the Request field. You need to use JSTL + EL to display the list collection data in a TABLE on a JSP page

Three-tier architecture: Software design architecture

  1. Interface layer (presentation layer) : the user sees the interface. Users can interact with the server through components on the interface
  2. Business logic layer: Deals with business logic.
  3. Data access layer: Manipulates data store files.

Case: Display user information list

  1. Requirement: add, delete, change and check user information

  2. Design:

    1. Technical selection: Servlet+JSP+MySQL+JDBCTempleat+Duird+BeanUtilS+ Tomcat
    2. Database design: Create database day17; Create database use day17; Create table user(– create table ID int primary key auto_increment, name varchar(20) not null, gender varchar(5), age int, address varchar(32), qq varchar(20), email varchar(50) );
  3. Development:

    1. Environment set up

      1. Creating a Database Environment
      2. Create a project and import the required JAR packages
    2. coding

  4. test

  5. Deployment operations