【问题标题】:Tomcat not running servletTomcat没有运行servlet
【发布时间】: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


    【解决方案1】:

    在您的 WEB INF XML 中对我来说突出的一个错误是

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

    您使用“url-pattern”打开标签,但使用“/url”关闭它。您应该使用“/url-pattern”关闭它。此外,您可能不需要“*”通配符,您可以这样做,如下所示:

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

    试试吧,它应该可以工作,其他一切看起来都很好。

    【讨论】:

    • 我修复了 url 模式并尝试了使用和不使用通配符。什么都没有。
    【解决方案2】:

    来自您的 web.xml errorServlet.errorServlet - 表示您在其中包含包 errorServlet 和类 errorServlet。但是您的 java 代码没有任何包声明。所以Tomcat至少找不到servlet的类。

    我的建议——举个简单的例子,注意上面的目录结构。

    【讨论】:

    • 指向那个类包问题的东西。我改变了它,但它仍然没有解决我的问题。
    【解决方案3】:

    不知道为什么在您的情况下没有提供错误消息。

    尝试通过在相应方法中添加一些带有自定义消息的 System.out.println() 来调试您的 servlet 实例化和请求处理。

    例如:

    public void init(ServletConfig config) throws ServletException{
      super(config);
      System.out.println("my servlet init() call");
    }
    
    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
      System.out.println("my servlet doGet() call");
    }
    

    重新部署您的 servlet 并检查您的消息的 tomcat 日志。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 2017-12-20
      • 2014-06-27
      • 2013-08-20
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多