Front end: Tomcat server deployment Web project

1.1 C/S Architecture

Client/ServerClient/Server

As a stand-alone program, the client has a better graphics effect but is difficult to maintain.

1.2 B/S Architecture

Browser / Server Browser / Server

Enter the URL directly into the browser to visit

After the server is upgraded, the browser can directly access and obtain

However, the browser cannot carry too large resources, so the page effect and other experience are not as good as those of the client.

2.1 server

A web server is a container for running and publishing web applications. Only by placing the developed web project in this container can all users in the network access it through a browser.

2.2 Common servers
  • Tomcat (one of the mainstream web servers, suitable for beginners)
  • jetty
  • resin (very fast)
3.1 Tomcat installation

Official website: Apache Tomcat® - Apache Tomcat 10 Software Downloads

After the download is complete, decompress it directly. Note that the file path is best not to have a Chinese name.

insert image description here

There are server management files under the bin folder

insert image description here

The webapps project deployment file initially holds some example files

insert image description here

3.2 Using Tomcat

Double click the bat file

insert image description here

The command line port appears and remains open

If the interface closes directly after running the command line here, you need to check whether the JAVA_HOME configuration is correct

Checking method: Enter java on the command line to see if it is an executable command

insert image description here

Garbled characters appear here because the encoding method of Tomcat needs to be modified

Enter the browser to visit Apache Tomcat/9.0.31

Observe the following interface, Tomcat starts successfully
insert image description here

Double-click the bat file to terminate Tomcat operation

insert image description here

At this time, accessing Apache Tomcat/9.0.31 will not be able to observe the content

3.3 Tomcat configuration

The port number is the unique identification of our access program, the default is 8080

Can be modified in server.xml under the conf folder

insert image description here

insert image description here

Use Notepad to open and modify the port value after restarting Tomcat

You can successfully modify the default port number.

insert image description here

Similarly, the configuration of other attributes can also be completed by modifying the text content.

3.4 Tomcat project deployment

Create a project file under the webapps folder

insert image description here

The WEB-INF folder and the html file you will access are required under the project file

insert image description here

Under the WEB-INF folder

insert image description here

web.xml can be copied from root and others are empty folders

  • Visit: http://localhost:8080/firstweb/first.html
  • protocol: http
  • Domain name or hostname: locallhost
  • Port number: 8080
  • Project path: firstweb/first.html

So we can see the first.html page we wrote in toncat

insert image description here

If the project path is selected incorrectly, a "resource does not exist" error will appear.

4.1 Servlet Technology
  • The server-side program (code, function implementation) can interactively process the request sent from the client to the server and complete the operation response.
  • A dynamic web technology
  • Basics of Java Web Program Development
4.2 Servlet configuration

Find the servlet-api.jar package under the lib file path

insert image description here

save its path

Append to the environment variable Classpath of the system

insert image description here

4.3 Configuration test

Create the MyServlet.java file

//简单Servlet示例
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

public class MyServlet implements Servlet{
    
    
    public void init(ServletConfig config) throws ServletException{
    
    
    }
public void service(ServletRequest request,ServletResponse response) throws ServletException,IOException{
    
    
		System.out.println("My First Servlet");
}

public void destroy(){
    
    
}

public ServletConfig getServletConfig(){
    
    
		return null;
}	

public String getServletInfo(){
    
    
		return null;
}
}

First of all, make sure that the java javac version is consistent

insert image description here

My versions are both 1.8.0_312

If you find that the two are inconsistent, it is because different versions of JDK are installed in the computer.

Delete different JDKs or adjust the path to put the bin path of a certain version of JDK in the front

Compile the above JAVA file on the command line

insert image description here

insert image description here

Compiled files with .class suffix appear in the same folder

It indicates that the Servlet environment configuration is successful.

4.4 Servlet Deployment

Place the class file in the previously created classes folder

insert image description here

Configure the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

<!-- 1.添加servlet节点 -->
<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>MyServlet</servlet-class>
</servlet>

<!-- 2.添加servlet-mapping节点 -->
<!-- 2.对Servlet做映射 name保持一致 -->
<!-- 2.url-pattern是访问Servlet的路径 -->
    
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/myservlet</url-pattern>
</servlet-mapping>
    
</web-app>

Although there is currently no content, no 404 or 500 errors are reported when accessing

500 means that there is an error in the Servlet code, including version incompatibility, etc.

Indicates that the Servlet was deployed successfully

insert image description here

The System.out.println statement prints output on the console

insert image description here

If you modify the Java file, you need to recompile and restart the Tomcat server

5.1 IDEA Department

We found that after rewriting the code, it is necessary to recompile, move files, restart services, etc., and the time cost is relatively high.

We deploy the Servlet environment in the IDEA development tool to facilitate our subsequent development.

IDEA creates a new project

insert image description here

Select the Enterprise project Name can be modified independently

insert image description here

insert image description here

Store the following files in the Web-INF folder

insert image description here

Add lib as dependency as our project's library

insert image description here

Configure Tomcat

insert image description here

deployment project

insert image description here

insert image description here

Click Execute IDEA in the lower menu to run the Tomcat server autonomously

insert image description here

Auto-open browser interface

insert image description here

Guess you like

Origin blog.csdn.net/yt266666/article/details/127401802