Tomcat Servlet:src/main/webapp/WEB-INF时为404状态,而root/WEB-INF时为无错误 [英] Tomcat Servlet: 404 status when src/main/webapp/WEB-INF and no error when root/WEB-INF
问题描述
我有一个 LoginServlet.java :
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.getWriter().println("HELLO THERE");
}
以及 web.xml 中的此代码(除其他外):
and this code in web.xml (among other things):
<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
错误.
I try to go to URL: http://localhost:8080/ProjectName/LoginResult.do
and get a HTTP Status 404 – Not Found
error.
为什么?似乎Tomcat无法找到Serlvet,否则它就可以输出文本,因为它相当容易.
Why? It seems like Tomcat can't find the serlvet, otherwise it would be able to output the text since its fairly easy.
我的项目结构:
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
中进行一些更改才能使其正常工作吗?
EDIT: I figured out a solution of sorts: if I place WEB-INF
folder with all its contents into root ProjectName
it works fine and I can access the servlet. However, I've seen this (like in my question) project structure recommended in tutorials. How do I make it work with a project structure when WEB-INF
is located inside src/main/webapp
? I use Maven a bit (download dependencies and specify target output directory). Do I need to make some changes in pom.xml
to make it work?
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
时,它将在root/target
中创建一个testproject-1.0-SNAPSHOT.war
和testproject-1.0-SNAPSHOT.war.original
.但是当我转到http://localhost:8080/ProjectName/LoginResult.do
时,仍然是404
错误.
When I run mvn package
it creates a testproject-1.0-SNAPSHOT.war
and testproject-1.0-SNAPSHOT.war.original
in root/target
. But then when I go to http://localhost:8080/ProjectName/LoginResult.do
it's still 404
error.
我尝试将testproject-1.0-SNAPSHOT.war
和testproject-1.0-SNAPSHOT.war.original
放入root
-无效,仍然出现404
错误,然后我尝试将其放入Tomcat 8.5/webapps
-仍然404
.
I tried putting testproject-1.0-SNAPSHOT.war
and testproject-1.0-SNAPSHOT.war.original
in root
- didn't work, still 404
error, then I tried putting it into Tomcat 8.5/webapps
- still 404
.
推荐答案
您面临的问题与放置错误的资源无关.我刚刚创建了一个Maven项目,其结构与您的结构相同,并且一切正常.您唯一做错的是使用URI:
The problem you are facing has nothing to do with the wrongly placed resources. I just created a Maven project with the same structure as yours and everything works fine. The only thing you're doing wrong is that you use the URI:
http://localhost:8080/ProjectName/LoginResult.do
正如您在上方所写的那样,所生成的 war 文件名为 testproject-1.0-SNAPSHOT.war .因此,如果将此文件放在Tomcat的 webapps 文件夹中,则正确的URL应为:
As you wrote above the generated war file is named testproject-1.0-SNAPSHOT.war. So if you drop this file in the webapps folder of Tomcat, your correct URL should be:
http://localhost:8080/testproject-1.0-SNAPSHOT/LoginResult.do
如果要使用URL,则应按以下步骤调整POM:
If you want to use your URL, you should adjust your POM as follows:
.............
<build>
<finalName>ProjectName</finalName>
..................
</build>
当您使用Maven进行构建时,这将产生 Projectname.war ,并且在部署后可以使用:
This will produce Projectname.war when you build with Maven, and after deploying you can use:
http://localhost:8080/ProjectName/LoginResult.do
最后,您不需要在POM中添加以下行并将其删除(我不确定您是否要将生成的工件放到源代码树下!):
Lastly, you don't need the following line in your POM and should remove it (I am not sure whether you want to put the generated artifacts under your source tree!):
<outputDirectory>${basedir}\src\main\webapp\WEB-INF\classes</outputDirectory>
这篇关于Tomcat Servlet:src/main/webapp/WEB-INF时为404状态,而root/WEB-INF时为无错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!