繁体   English   中英

Tomcat未运行Servlet

[英]Tomcat not running servlet

因此,我写了一个servlet,将所有对/ foo / *的请求重定向到一个.jsf文件,并说URL不再存在。 我安装了它,以便可以找到/newpath/error.faces。 当我在eclipse中启动服务器并导航到与/ foo / *映射匹配的任何URL时,问题什么都没得到。 浏览器中没有404,控制台中没有杰克蹲。 没有错误,没有消息,什么也没有,我可以弄清楚为什么。

我通过转到窗口->首选项->服务器->运行时环境-> Apache Tomcatv7.0->编辑->来确保自己在正确的根目录下,并查看了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>

尝试一下,它应该可以正常工作,其他所有内容都可以。

从您的web.xml中的errorServlet.errorServlet意味着您在其中具有package errorServlet和class errorServlet 但是您的Java代码没有任何包声明。 因此,Tomcat至少找不到Servlet的类。

我的建议-举一些简单的例子,并注意上面的目录结构。

不知道为什么您没有提供错误消息。

尝试通过在相应方法中添加一些带有自定义消息的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日志中的消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM