Hello, today for you children to share JSP, quickly take out a small book to write it down!

First, JSP overview

In dynamic web pages, most of the content is fixed, only local content needs to be dynamically generated and changed.

It is a very painful thing to use Servlet to output static web Pages through string concatenation. In order to make up for this defect of Servlet, SUN company launched JSP (Java Server Pages) technology on the basis of Servlet.

JSP is a technology that simplifies Servlet writing by allowing Java code and HTML statements to be mixed together in the same file

JSP only in the web page to produce dynamic content using Java to write, to the fixed static content using HTML pages

A JSP page is a file made up of HTML statements (to output the static part) and Java code (to output the dynamic part), with a suffix named.jsp

JSP is essentially a Servlet.

Second, JSP and servlet advantages and disadvantages

sevlet

Advantages: Java is extremely convenient to write

Disadvantages: Generating HTML pages using Response.getwriter ().print(” page “) is tedious and complicated

jsp

Advantages: JSP eliminates the complexity of writing HTML code in servlets

Disadvantages: All Java code written in JSP, poor maintainability

Three, JSP quick start

3.1 Implementation principle of JSP

Tomcat will translate the JSP file into a.java file, compile it into a.class file, and then run this.class file. The entire process is completed in the Work directory in the IDEA virtual directory.

The JSP we write is compiled into a.class that inherits HttpJspBase, and HttpJspBase inherits HttpServlet, so JSP is essentially a Servlet.

3.2 JSP basic syntax

3.2.1 script

Jsp defines Java code through scripting. Scripting is the part of Jsp used to write Java code. There are three main ways to write:

Script fragments

Syntax: <% Java code %> Purpose: Write logical Java code that, when compiled, appears directly in the servlet’s service() method

Grammar: < %! Java code %> declares global variables and methods. The code written in this declaration will appear in the member variable location of the servlet when compiled

Script expression

Syntax: <%= Java code %>

What it does: Outputs characters to the page. The code written in this output will appear directly in the out.print(“”) method of the servlet’s service()

3.2.2 annotation

<%– comment content –%> ‘comment HTML code and Java code scripts

Scope of comment:

3.2.3 Three instructions

page

include

taglib

3.3 Implicit Objects

Implicit objects: objects that are declared in JSP (variables defined by _jspService) and can be used directly on the page.

Implicit object characteristics

You can use it directly in JSP without declaring it

Instead of really not declaring, it looks like there is no declaration in the JSP

The first four objects can all be used to share data, and they can access data in descending order: pageContext < Request < session < Application

PageContext does three things:

  1. The current page shares data 2) obtains other eight built-in objects 3) can manipulate data from four domain objects

3.3.1 PageContext Page field

PageContext is javax.mail servlet. JSP. JspContext subclass, also in javax.mail. Servlet. JSP bag; It can get eight other implicit objects, and it is also a domain object, called a page domain.

Page domain scope: in the current page, out of the current page is not used.

PageContext can manipulate not only the page domain, but also the other three domains.

3.4 Expression Language

3.4.1 track EL overview

JSP nested Java code, using script elements, is now obsolete because it is so complex that Java code is now not allowed in JSPS.

Now we use Expression Language, which is primarily used to simplify operations on Java code in JSPS.

The EL can get data from the JSP’s domain objects and perform some simple calculations.

Syntax: ${expression}

${pagescope. key name} gets the value of the specified key name from the page field

${requestScope. Key name} retrieves the value of the specified key name from the request field

${sessionScope. Key name} retrieves the value of the specified key name from the session field

${applicationScope. Key name} retrieves the value of the specified key name from the application field

Simplified writing:

${key name}: search from the smallest field to the largest field, if found, immediately return and terminate the search; If not found, return”

Details:

No null pointer exception

No array subscript is out of bounds

No string concatenation

3.4.2 Storing Data

When putting data into a field, try not to use it in property names. And digital

3.4.3 Reading Data

  • Simple type: ${key name}

  • Object type: ${key name. Property name drop get lowercase}

  • Single-column collection type: ${key name [index]}

  • ${key name [” key “]}

3.4.4 Performing operations

  • Arithmetic operator

Syntax: + – * / %

  • Comparison operator

Syntax: > < >= <= =! =

  • Logical operator

Grammar: && | |!

  • Ternary operator

Syntax: ${conditional expressions? True: false}

  • Air transport operator

${not empty object}

Function:

Used for non-null judgments,

Check whether a string is null or “”

Determines whether the length of a collection or array is 0

Determines whether an object is null

3.4.5 Usage details

No null pointer exception

No array subscript is out of bounds

No string concatenation

3.5 Jsp standard tag library

3.5.1 track of JSTL overview

Jsp Standard Tag Library (Jsp Standard Tag Library), is provided by the Apache organization of open source Jsp Tag Library, the essence is to contain a certain logic Tag, the role is in the page to do judgment and circular operation.

The JSTL standard tag library has 5 sub-libraries, but with the development, the core library is often used

3.5.2 Common Labels in Core

  • c:if

The if tag is used for single-branch conditions. It is equivalent to the use of if(condition){} in Java.

<c:if test=“条件”>

The logic of the condition being true

</c:if>

  • c:foreach

ForEach is used for loop traversal. Common properties:

Begin: indicates the start of the loop. The default value is 0

End: The end of the loop. Default is the length of items

Var: a temporary variable in a loop

Step: Set the step. The default value is 1

Items: Specifies objects to loop through, supports EL expressions, and can fetch collections from four fields

VarStatus: traversal status

Count: indicates the number of digits being traversed, starting from 1

Index: indicates the current traversal index, starting from 0

3.5.3 Use of JSTL

  1. Import the jar package javax.mail. Servlet. JSP. JSTL. Jars and JSTL – impl. Jar package
  2. Import the tag library in the used page by <%@taglib prefix=” tag prefix “uri=” tag library URI “%>

The < % @ taglib prefix = “c” uri = “java.sun.com/jsp/jstl/co…” % >

MVC pattern

4.1 summary of MVC

MVC is a software architecture pattern in software engineering. It is a development idea that separates business logic and display interface.

  • M: Model. Other entity classes besides servlets are used to process business logic or encapsulate data

  • V: View, JSPS are views right now, for displaying data, good at displaying dynamic content

  • C: Controller, currently servlets are controllers, used to receive and respond to requests from clients

MVC advantages:

  • Reduce coupling (code correlation), convenient maintenance and expansion, conducive to division of labor and collaboration

  • Reusability is strong

MVC faults:

  • It makes the project structure become complex, enhances the difficulty of development, and requires the ability of developers to be relatively high

4.2 Three-Tier Architecture

The three-tier architecture is a refinement of THE MVC pattern, which further divides the Model into service layer and DAO layer. The future development will follow the three-tier architecture.

4.3 Package Directory Structure

The first layer of the base package for each project is the company domain name written backwards, such as com.baidu

Com. Baidu Company domain name backwards

Com.baidu. web Web layer content

Com.baidu. service Service layer code (business logic layer)

Com.baidu. dao database access layer

Com.baidu. domain holds the entity class

Com.baidu. utils stores utility classes: classes that can be used without new store static methods

Complex hierarchies:

Note: Package names are all lowercase.

Well, that’s all for today’s article, hoping to help those of you who are confused in front of the screen