[JavaWeb] Overview of JSP

Table of contents

1. What is JSP

2. Why learn JSP

1. The operating principle of JSP

1.1 Create a JSP page

1.2 Running JSPs

1.3 Analysis of the operating principle of JSP

1.4 JSP script elements

3. MVC mode of JSP development mode

1. Development of dynamic web page development model

4. Path problems in development

1. When will you encounter path problems

2. Classification of paths

5. Introduction of Case Requirements

1. Description of case requirements

2. Case process analysis

3. How MVC is applied in the case

4. Case preparation - create database

1. Create table statement

5. Case preparation - project environment construction

1. Create a web project

2. Create the relevant package structure

 6. Case preparation - introduction of relevant resources

1. Introduce related jar packages:

2. Import C3P0 configuration file

3. Introduce tools developed by JDBC

7. Case Preparation - Create Landing Page

1. Login page implementation

2. Login page effect

8. Case code - login code implementation

1. Login code implementation process

 2. Login code implementation

 3. The business code of the logged-in Servlet

4. Process data part code

 5. Login code error message echo

 6. Record the number of people who log in successfully

6. Request forwarding and redirection to complete the page jump

1. Request forwarding

1.1 How to write request forwarding

 1.2 Code implementation of request forwarding

1.3 The effect of request forwarding

2. Redirection

2.1 How to write redirection

 2.2 Code implementation of redirection

 2.3 The effect of redirection

3. The difference between request forwarding and redirection

1. Principles of request forwarding and redirection

2. Summary of the difference between request forwarding and redirection

3. The code demonstrates the difference between request forwarding and redirection

Summarize


1. What is JSP

JSP: Java Server Pages (Java server-side pages), in fact, embed Java code in HTML.

2. Why learn JSP

SUN company provides dynamic web page development technology: Servlet. Servlet itself has some shortcomings, and SUN company discovered these problems, and introduced a new dynamic web page development technology JSP.

Disadvantages of Servlets:

  1. Servlet needs to be configured, which is inconvenient to maintain
  2. It is difficult for Servlet to output HTML page content to the web page

1. The operating principle of JSP

1.1 Create a JSP page

1.2 Running JSPs

JSP needs to be published to the server before it can run.

  1. Publish the project to Tomcat
  2. Visit the JSP page

1.3 Analysis of the operating principle of JSP

The JSP file is translated into a Java file, the Java file is compiled to generate a class file, and the class file is run.

1.4 JSP script elements

1.4.1 What is the JSP script element:
 

JSP = HTML + Java code + JSP itself

The script element of JSP is to embed Java code in JSP .

1.4.2 Classification of JSP script elements

The first one: declaration label

  • grammar:
  • ①<%! Variable or method declaration %>
  • ②The code written in this script is translated into member variables or member methods inside the Servlet.
  • usage:

 Second: expression label

grammar:

①<%= expression%>

②The code written in this script is translated into the content of out.print(); inside the method.

usage:

 The third: program code label

grammar:

①<% program code%>

②The code written in this script is translated into local variables inside the method or code fragments inside the method.

usage:

3. MVC mode of JSP development mode

1. Development of dynamic web page development model

4. Path problems in development

1. When will you encounter path problems

Some pages are provided, and links or forms are provided on the pages. When the links or forms are clicked, they need to be submitted and submitted to the Servlet. How should the address (path) to send a request from the page to the Servlet be written.

2. Classification of paths

2.1 Relative path:

How to write the relative path: The relative path does not start with /.

Use of relative paths:

①Access the Servlet on the page under the root path

The access path of demo2.jsp:

http://localhost:8080/web02/demo2.jsp

Access path of ServletDemo1:

http://localhost:8080/web02/ServletDemo1

 ② Access Servlet from a page in a certain directory

The access path of demo2.jsp:

http://localhost:8080/web02/demo2/demo2.jsp

Access path of ServletDemo1:

http://localhost:8080/web02/ServletDemo1

2.2 Absolute path (usually):

The writing method of absolute path: the path usually starts with /

Using an absolute path, you don't need to care about the relative position of the current file and the file to be requested! ! !

Notice:

  • ①Absolute path is divided into server-side path and client-side path
  • ②The client path   needs to have the project name
  • ③The server-side path does not need to have a project name

5. Introduction of Case Requirements

1. Description of case requirements

Provide a login page for user login (username and password need to query the database). If the login fails, you need to return to the login page (with a prompt message). If the login is successful, the page will jump, and the total number of successful logins will be displayed on the success page.

2. Case process analysis

3. How MVC is applied in the case

Let me explain here that in our mvc mode, the original Servlet becomes the Controller, the encapsulation and processing becomes the Modul layer, and the page display becomes the View layer. The Controller layer does scheduling, and the actual operation is still performed at the Modul layer.

4. Case preparation - create database

1. Create table statement

create database web02_login;
use web02_login;
create table user(
	uid int primary key auto_increment,
	username varchar(20),
	password varchar(20),
	nickname varchar(20)
);

insert into user values (null,'zs','123','张三');
insert into user values (null,'ls','123','李四');
insert into user values (null,'ww','123','王五');

5. Case preparation - project environment construction

1. Create a web project

Write the project name, and then choose version 2.5

 directly next

 Create a good directory here

Next we need to modify the compilation path of the Web project:

Create a classes folder under the WEB-INF directory.

Then right click on the project and select Build Path ---> Configure Build Path

Change the compiled output path to: project name/WebContent/WEB-INF/classes .

 Click Apply and Close , you will find that the created classes folder disappears, you can delete the build folder. So far, a standard dynamic Web project has been created!

 

2. Create the relevant package structure

The controller package stores the control layer code, corresponding to the original Servlet

The model package is mainly used to store the location of the javabean that processes the data

The meaning of the domain package entity, our usual data-encapsulated javabean class is placed here

The tool class for connecting to the database in the utils package is placed here

 6. Case preparation - introduction of relevant resources

1. Introduce related jar packages:

  1. MySQL database driver package
  2. The jar package required by the C3P0 connection pool
  3. jar package developed by DBUtils

2. Import C3P0 configuration file

Put the configuration file under the src of the project

3. Introduce tools developed by JDBC

7. Case Preparation - Create Landing Page

1. Login page implementation

2. Login page effect

8. Case code - login code implementation

1. Login code implementation process

Login page (login.jsp)--->>Login Servlet (LoginServlet), in this Servlet needs to receive data, encapsulate this data into a JavaBean, and call another JavaBean to process the data. Jump to the page according to the processing result.

 2. Login code implementation

Step 1: Create LoginServlet under the controller package

Step 2: Create a User class under domain

Step 3: Create a UserModel class under the model package

Provide a login method in this class

Step 4: Jump to the page according to the processing result

 3. The business code of the logged-in Servlet

4. Process data part code

 5. Login code error message echo

 Error message display effect:

 6. Record the number of people who log in successfully

  1. drawing analysis

1.1 Complete the initialization operation

Initialize a value to zero when the server starts, and store this value in the ServletContext domain.

Configure this servlet to load at startup:

 1.2 Record the number of people who have successfully logged in

1.3 Display the total number of people on the successful login page

6. Request forwarding and redirection to complete the page jump

1. Request forwarding

1.1 How to write request forwarding

Obtain the RequestDispatcher object through the ServletRequest object.

Then forward the request according to the method in RequestDispatcher.

 1.2 Code implementation of request forwarding

1.3 The effect of request forwarding

Note: Let me say here that when I use eclipse to build a dynamic web project, I build it directly according to the instructions, but after the build is completed, after I write the forwarded ServletDemo1.java code, enter the corresponding address in the browser, It just doesn’t forward to the corresponding jsp page, and the error report says that I can’t find my ServletDemo1.java file. I only know after Baidu. You need to create a classes folder under the created webapp folder, and compile the java code to the location Changed to this location, the location was not specified before, resulting in the java code not being compiled, so the class could not be found.

2. Redirection

2.1 How to write redirection

Redirection is achieved through the following methods in the HttpServletResponse object

 2.2 Code implementation of redirection

 2.3 The effect of redirection

3. The difference between request forwarding and redirection

1. Principles of request forwarding and redirection

Forwarding means that after the client sends a request, it finds the Servlet, and then the Servlet forwards it to the jsp, and then the jsp responds and returns it to the client.

And redirection means that after the client sends a request, it finds the Servlet, and then the Servlet returns the redirected url to the client, and then the client requests the url, finds the jsp page, and then returns it to the client accordingly.

2. Summary of the difference between request forwarding and redirection

  1. Request forwarding is one request and one response, while redirection is two requests and two responses.
  2. The request forwarding address bar will not change, but the redirection address bar will change.
  3. The request forwarding path does not include a project name, and the redirection requires a path with a project name.
  4. Request forwarding can only be within this website, and redirection can be directed to any website.

 3. The code demonstrates the difference between request forwarding and redirection

jsp page:

Notice:

If you need to use request for value transfer, you need to complete it through request forwarding. If the page needs to jump to another website, redirection must be used.

Guess you like

Origin blog.csdn.net/wang_qiu_hao/article/details/126514708