gpt4 book ai didi

java - Tomcat 没有运行 servlet

转载 作者:行者123 更新时间:2023-11-28 23:00:16 24 4
gpt4 key购买 nike

因此我编写了一个 servlet,将所有对/foo/* 的请求重定向到一个 .jsf 文件,表示该 URL 不再存在。我设置了它,以便我可以导航到/newpath/error.faces 只是找到。当我在 eclipse 中启动服务器并导航到与/foo/* 映射匹配的任何 URL 时出现的问题我什么也得不到。浏览器中没有 404,控制台中没有 jack squat。没有错误,没有消息,什么都没有,我可以找出原因。

我通过转到 Window->Preferences->Server->Runtime Environment->Apache Tomcatv7.0->Edit-> 检查并查看 Tomcat 安装目录字段以确保我处于正确的根目录中。

它指向 C:/Users/myName/Tomcat 7.0。

C:/Users/myName/Tomcat 7.0/webapps/ROOT/WEB-INF 中的 web.xml 文件如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<servlet>
<servlet-name>errorServlet</servlet-name>
<servlet-class>errorServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>errorServlet</servlet-name>
<url-pattern>/foo/*</url-pattern>
</servlet-mapping>
</web-app>
</web-app>

和位于同一目录下的errorServlet.java 看起来像

import java.io.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class errorServlet extends HttpServlet{


public errorServlet(){
super();
}

public void init(ServletConfig config) throws ServletException{
super.init(config);
}


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
processRequest(request, response);
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String redirectString = response.encodeRedirectURL("/newpath/error.faces");
response.sendRedirect(redirectString);
}

}

我已经从 errorServlet.java 编译了 .class 和 .jar 文件(分别命名为 errorServlet.class 和 errorServlet.jar),它们与 .java 文件位于相同的位置。我错过了什么或做错了什么?为什么当我到达/foo/* 时我的 servlet 没有触发?

编辑:我已经按照前两个答案的建议进行了更改,虽然他们的建议很受欢迎,但我仍然什么也没看到(此时我会杀死至少一条错误消息)。

最佳答案

在您的 WEB INF XML 中我注意到的一个错误是

<url-pattern>/foo/*</url>

您正在使用“url-pattern”打开标签,但使用“/url”关闭它。您应该改为使用“/url-pattern”关闭它。此外,您可能不需要“*”通配符,您可以这样做,看起来像:

<url-pattern>/foo/</url-pattern>

尝试一下,它应该可以工作,其他一切看起来都正常。

关于java - Tomcat 没有运行 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20149179/

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