gpt4 book ai didi

java - Tomcat 服务小程序 : 404 status when src/main/webapp/WEB-INF and no error when root/WEB-INF

转载 作者:行者123 更新时间:2023-11-28 22:47:18 25 4
gpt4 key购买 nike

我有一个LoginServlet.java:

public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.getWriter().println("HELLO THERE");
}

web.xml 中的这段代码(以及其他内容):

    <servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginResult.do</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.web.LoginServlet</servlet-class>
</servlet>

我尝试转到 URL:http://localhost:8080/ProjectName/LoginResult.do 并收到 HTTP Status 404 – Not Found 错误。

为什么? Tomcat 好像找不到serlvet,不然应该能输出文本,还算简单。

我的项目结构:

ProjectName
-----------index.jsp
-----------pom.xml
-----------src
--------------main
------------------java
----------------------com
-------------------------web
---------------------------------LoginServlet.java
------------------webapp
------------------------WEB-INF
-------------------------------classes
--------------------------------------com
------------------------------------------web
---------------------------------------------LoginServlet.class

编辑:我想出了一个解决方案:如果我将 WEB-INF 文件夹及其所有内容放入根 ProjectName 中,它就可以工作很好,我可以访问 servlet。但是,我已经看到教程中推荐的这个(就像我的问题一样)项目结构。当 WEB-INF 位于 src/main/webapp 中时,我如何让它与项目结构一起工作?我稍微使用了 Maven(下载依赖项并指定目标输出目录)。我是否需要对 pom.xml 进行一些更改才能使其正常工作?

EDIT2:这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.util</groupId>
<artifactId>testproject</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test Project</name>
<url>https://test.com</url>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-util -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-util</artifactId>
<version>8.0.0-RC1</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.3.0.RELEASE</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>

<!--Using logback-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-enforcer-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

<properties>
<java.version>1.7</java.version>
<start-class>com.web.LoginServlet</start-class>
</properties>

<build>
<outputDirectory>${basedir}\src\main\webapp\WEB-INF\classes</outputDirectory>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>

<artifactItems>

<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>8.0.30.2</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>

</artifactItems>

</configuration>
</execution>
</executions>
</plugin>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>

</plugin>


<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

</plugins>

<defaultGoal>install</defaultGoal>
</build>
</project>

当我运行 mvn package 时,它会创建一个 testproject-1.0-SNAPSHOT.wartestproject-1.0-SNAPSHOT.war.original 根/目标。但是当我转到 http://localhost:8080/ProjectName/LoginResult.do 时,它仍然是 404 错误。

我尝试将 testproject-1.0-SNAPSHOT.wartestproject-1.0-SNAPSHOT.war.original 放入 root - 没有工作,仍然是 404 错误,然后我尝试将其放入 Tomcat 8.5/webapps - 仍然是 404

最佳答案

您遇到的问题与资源放置错误无关。我刚刚创建了一个与您的结构相同的 Maven 项目,一切正常。您唯一做错的是您使用了 URI:

http://localhost:8080/ProjectName/LoginResult.do

正如您在上面所写,生成的 war 文件名为 testproject-1.0-SNAPSHOT.war。所以如果你把这个文件放在 Tomcat 的 webapps 文件夹中,你正确的 URL 应该是:

http://localhost:8080/testproject-1.0-SNAPSHOT/LoginResult.do

如果你想使用你的 URL,你应该按如下方式调整你的 POM:

.............
<build>
<finalName>ProjectName</finalName>
..................
</build>

这将在您使用 Maven 构建时生成 Projectname.war,部署后您可以使用:

http://localhost:8080/ProjectName/LoginResult.do

最后,您的 POM 中不需要以下行,应该将其删除(我不确定您是否要将生成的工件放在源代码树下!):

<outputDirectory>${basedir}\src\main\webapp\WEB-INF\classes</outputDirectory>

关于java - Tomcat 服务小程序 : 404 status when src/main/webapp/WEB-INF and no error when root/WEB-INF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54718941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com