简体   繁体   English

Tomcat未运行Servlet

[英]Tomcat not running servlet

So I wrote a servlet that redirects all requests to /foo/* to an .jsf file saying the URL no longer exists. 因此,我写了一个servlet,将所有对/ foo / *的请求重定向到一个.jsf文件,并说URL不再存在。 I got it setup so that I can navigate to /newpath/error.faces just find. 我安装了它,以便可以找到/newpath/error.faces。 The problem when I start the server in eclipse and navigate to any URL matching that /foo/* mapping I get nothing. 当我在eclipse中启动服务器并导航到与/ foo / *映射匹配的任何URL时,问题什么都没得到。 No a 404 in the browser and jack squat in the console. 浏览器中没有404,控制台中没有杰克蹲。 No errors, no messages, nothing and I can figure out why. 没有错误,没有消息,什么也没有,我可以弄清楚为什么。

I checked to make sure I'm in the proper root by going to Window->Preferences->Server->Runtime Environment->Apache Tomcatv7.0->Edit-> and looked at the Tomcat Installation Directory Field. 我通过转到窗口->首选项->服务器->运行时环境-> Apache Tomcatv7.0->编辑->来确保自己在正确的根目录下,并查看了Tomcat安装目录字段。

which points to C:/Users/myName/Tomcat 7.0. 指向C:/ Users / myName / Tomcat 7.0。

The web.xml file in C:/Users/myName/Tomcat 7.0/webapps/ROOT/WEB-INF looks like: 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>

and errorServlet.java which is located in the same directory looks like 和位于同一目录中的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);
}

}

I have compiled the .class and .jar files from errorServlet.java (named errorServlet.class and errorServlet.jar respectively) and they're in the same locationas the .java file. 我已经从errorServlet.java编译了.class和.jar文件(分别命名为errorServlet.class和errorServlet.jar),它们与.java文件位于同一位置。 What am I missing or doing wrong? 我想念什么或做错什么? Why is my servlet not firing when I got to /foo/*? 为什么到/ foo / *时,我的servlet无法启动?

Edit: I've made the changes suggested by the first two answers while their suggestions are appreciated I'm still seeing literally nothing (at this point I would kill to for at least an error message). 编辑:我已经做出了前两个答案所建议的更改,而他们的建议得到了赞赏,但我仍然看不到任何内容(在这一点上,我至少会看到一条错误消息)。

One error that stands out to me in your WEB INF XML is 在您的WEB INF XML中对我而言最突出的一个错误是

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

You are opening the tag with "url-pattern" but closing it with "/url". 您使用“ url-pattern”打开标签,但使用“ / url”关闭标签。 You should instead close it with "/url-pattern". 您应该改为使用“ / url-pattern”将其关闭。 Also, you probably do not need the "*" wildcard, you could just do it so it looks like: 另外,您可能不需要“ *”通配符,只需执行一下就可以了:

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

Try that, it should work then, everything else looks OK. 尝试一下,它应该可以正常工作,其他所有内容都可以。

From your web.xml errorServlet.errorServlet - means that you have package errorServlet and class errorServlet inside it. 从您的web.xml中的errorServlet.errorServlet意味着您在其中具有package errorServlet和class errorServlet But your java code has no any package declaration. 但是您的Java代码没有任何包声明。 So Tomcat at least cannot find the class of servlet. 因此,Tomcat至少找不到Servlet的类。

My recommendation - take some simple example and pay attention to directory structure on it. 我的建议-举一些简单的例子,并注意上面的目录结构。

Have no idea why in your case no error messages were provided. 不知道为什么您没有提供错误消息。

Try to debug your servlet instantiation and request processing by adding some System.out.println() with custom messages in corresponding methods. 尝试通过在相应方法中添加一些带有自定义消息的System.out.println()来调试servlet实例化并请求处理。

For example: 例如:

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");
}

Redeploy your servlet and check tomcat log for your messages. 重新部署您的servlet,并检查tomcat日志中的消息。

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

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