因此,我編寫了一個servlet,將所有請求重定向到/ foo/*到一個.jsf文件,說明URL不再存在。我已經安裝好了,這樣我就可以導航到/newpath/error.faces找到。當我在eclipse中啓動服務器並導航到任何匹配該/ foo/*映射的URL時,我什麼也得不到。在瀏覽器中沒有404和插孔在控制檯中下蹲。沒有錯誤,沒有消息,沒有什麼,我可以找出原因。Tomcat沒有運行servlet
我檢查並確認我進入了Window-> Preferences-> Server-> Runtime Environment-> Apache Tomcatv7.0-> Edit->,並查看了Tomcat安裝目錄字段,以確保我處於正確的根目錄。
指向C:/ Users/myName/Tomcat 7.0。
在C中的web.xml文件:/用戶/ MYNAME/Tomcat的7.0/web應用/ ROOT/WEB-INF看起來像:
<?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.class和errorServlet.jar)彙編了errorServlet.java中的.class和.jar文件,他們是在同一個locationas的java文件。我錯過了什麼或做錯了什麼?爲什麼當我到達/ foo/*時,我的servlet沒有啓動?
編輯:我做了前兩個答案建議的變化,同時他們的建議表示讚賞我仍然沒有看到字面上什麼都沒有(在這一點上,我會殺了至少一個錯誤信息)。
指出該類包問題的事情。我改變了,但它仍然沒有解決我的問題。 –