0

I had some problems when I first wrote the Spring MVC project enter image description here My pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>springmvc01-base</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springmvc01-base Maven Webapp</name>
  <url>http://maven.apache.org</url>

    <dependencies>
        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- 日志 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>


        <!-- Spring5和Thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
    </dependencies>
  <build>
    <finalName>springmvc01-base</finalName>
  </build>

</project>

I've added servlets in my dependencies and built successfully with Maven enter image description here

However, servlet-api does not appear in the target/project/WEB-INF/lib directory enter image description here

Can you help me?

I tried clear and package maven and it didn't work

2
  • With scope=provided, it's normal that the servlet jar doesn't appear in your WEB-INF/lib. It's expected to be part of your server environment - Tomcat - rather than your app. Commented Aug 7, 2023 at 22:01
  • I'd normally say this looks like a case of mismatched dependency and Tomcat versions (see, for instance, stackoverflow.com/q/54122724/796761). But It looks like Tomcat 9 is what you're running, which does support Servlet 4 that you've specified, which does have the method specified. Commented Aug 7, 2023 at 22:07

1 Answer 1

-2

Please try changing

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

to

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0</version>
</dependency>

see Maven Dependency Scope

If you want to deploy with a Servlet container, and the container already provides an appropriate version of the Servlet API, you can use <scope>provided</scope>; if you don't want to rely on an external Servlet container, you'd better delete <scope> provided</scope>.

This is my understanding, I hope it can help you.

Sign up to request clarification or add additional context in comments.

3 Comments

Didn't solve my problem, but thank you very much for your help 🌹
This is not correct, the reason provided must be specified is that Tomcat already provided this library for you.
Thank you, I understand now. I want servlet-api.jar to appear in the packaged directory to ignore this question, I feel guilty for my wrong answer.@MarkRotteveel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.