IDEA creates a Servlet project (tomcat10)

 1. Create a maven project

org.apache.maven.archetypes:maven-archetype-webapp

 2. Increase Servlet dependency

Tomcat9 and previous dependencies

<!--加入servlet依赖(servlet的jar)-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
    <!--jsp的依赖(jsp相关的jar加进来)-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
      <scope>provided</scope>
    </dependency>

tomcat10 (the case uses tomcat10)

   <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>jakarta.servlet.jsp</groupId>
      <artifactId>jakarta.servlet.jsp-api</artifactId>
      <version>3.1.1</version>
      <scope>provided</scope>
    </dependency>

3. Create a java directory and write code

create java directory

 create package

 Create Servlet01 class and implement Servlet 

 

package com.minos.servlet;


import jakarta.servlet.*;

import java.io.IOException;

public class Servlet01 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("hello servlet");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

3. Configure web.xml

 

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0"
         metadata-complete="false">

<!--<web-app>-->
    <display-name>Archetype Created Web Application</display-name>
    <!--  配置servlet-->
    <servlet>
        <servlet-name>demo01</servlet-name>
        <servlet-class>com.minos.servlet.Servlet01</servlet-class>
    </servlet>
    <!--  配置路径-->
    <servlet-mapping>
        <servlet-name>demo01</servlet-name>
        <url-pattern>/demo01</url-pattern>
    </servlet-mapping>
</web-app>

Fourth, configure the startup environment

 If tomcat is not installed locally, you can install it

https://tomcat.apache.org/

 

 

 download on demand

https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.10/bin/apache-tomcat-10.1.10-windows-x64.zip

Unzip to a specific directory

D:\ProgramFiles\apache-tomcat-10.1.10\bin

 Configure environment variables

 IDEA deploy tomcat

 Configure default browser, port

 

 app context config/

 5. Start the application and test

 

 access

http://localhost:8080/demo01

 View console output

 

Guess you like

Origin blog.csdn.net/qq_29752857/article/details/131448707