【发布时间】:2013-11-22 15:59:23
【问题描述】:
所以我编写了一个 servlet,它将所有对 /foo/* 的请求重定向到一个 .jsf 文件,说明 URL 不再存在。我设置好了,这样我就可以导航到 /newpath/error.faces 了。当我在 eclipse 中启动服务器并导航到与 /foo/* 映射匹配的任何 URL 时,我什么也得不到。浏览器中没有 404 和控制台中的千斤顶蹲下。没有错误,没有消息,什么都没有,我可以找出原因。
我通过转到 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(分别命名为 errorServlet.class 和 errorServlet.jar)编译了 .class 和 .jar 文件,它们与 .java 文件位于相同的位置。我错过了什么或做错了什么?为什么当我到达 /foo/* 时我的 servlet 没有触发?
编辑:我已经按照前两个答案的建议进行了更改,同时感谢他们的建议我仍然什么都没有看到(此时我会杀死至少一条错误消息)。
【问题讨论】:
标签: java eclipse tomcat servlets