Eclipse creates a maven-based web project

A tomcat installation and deployment

1.  Install tomcat

To run the web program, a tomcat environment is required. Install tomcat in advance

2. Configure tomcat environment

Click windows ->preference, and select server -> Runtime environment on the left side of the pop-up window.

Click Add, select the type of tomcat according to the installed tomcat version, and click the finish button

 

 

 3. Configure tomcat server

Click on the link to create a tomcat server

 

 

Add the web program to the list on the right

 

  

 Three Create a simple maven-based java web project

1 Open Eclipse

2. Create a new maven-based web project

1) Click the menu File -> New -> Maven Project, and then click Next.

2) On the "Select Project" page, set the project path and click Next.

   3) On the "Select Archetype" page, select "maven-archetype-webapp" and click Next.

4.) On the "New Maven Project" page, enter the Group Id and Artifact Id and click Finish.

Group id input: cn.nfu.caohongxing

Aritifact id input: book-mananger-system

Enter in Version: 1.0.0

 Error handling: 

 Some students, when clicking finish, the following error will appear, just click the right-click menu run as -> Maven install

 The above error occurs

5) Under the "src/main" folder, create a folder named "java". This will be your Java source code directory.

 

6) Under the "java" folder, create a Java class file mvnServerlet.java, and write the following code in this class:

 

 enter code

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class mvnServerlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Hello, World!");
    }
}

At this time, javax.servlet will be red, because the package is not imported

7). Add the external dependency of javax.servlet in pom.xml

  <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
  </dependency>

 In order for the settings to take effect, you need to restart eclipse

If you still get an error, update maven

8) Edit the index.jsp under the "src/main/webapp" folder of the project, and write the following code in this file:

<html>
<body>
<h2>Hello World! I am  comming</h2>
</body>
</html>

9). Edit web.xml under the "src/main/webapp/WEB-INF folder of the project, such as the following code:

<?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_3_1.xsd"
    version="3.1">
    <display-name>MyWebApp</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>mvnServerlet</servlet-name>
        <servlet-class>mvnServerlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvnServerlet</servlet-name>
        <url-pattern>/mvnServerlet</url-pattern>
    </servlet-mapping>
</web-app>

10) Compile and package the project, right-click the "pom.xml" file, select "Run As -> Maven install", and wait for the compilation and packaging to complete.

Click windows -> show view, select "server" in the pop-up window

Right-click start to start tomcat 

 After waiting for Tomcat to start, visit in the browser: http://localhost:8166/book-manager-system/

"Hello, World! i am comming"。

The above is a simple and complete maven-based web project creation process.

Guess you like

Origin blog.csdn.net/caohongxing/article/details/129884552