Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tomcat没有运行servlet_Java_Eclipse_Tomcat_Servlets - Fatal编程技术网

Java Tomcat没有运行servlet

Java Tomcat没有运行servlet,java,eclipse,tomcat,servlets,Java,Eclipse,Tomcat,Servlets,因此,我编写了一个servlet,它将所有到/foo/*的请求重定向到一个.jsf文件,表示URL不再存在。我将其设置为可以导航到/newpath/error.faces只需查找。当我在eclipse中启动服务器并导航到任何与/foo/*映射匹配的URL时,我什么也得不到。浏览器中没有404,控制台中没有jack Shuck。没有错误,没有消息,什么都没有,我能找出原因 我通过进入窗口->首选项->服务器->运行时环境->ApacheTomcatv7.0->编辑->并查看Tomcat安装目录字段

因此,我编写了一个servlet,它将所有到/foo/*的请求重定向到一个.jsf文件,表示URL不再存在。我将其设置为可以导航到/newpath/error.faces只需查找。当我在eclipse中启动服务器并导航到任何与/foo/*映射匹配的URL时,我什么也得不到。浏览器中没有404,控制台中没有jack Shuck。没有错误,没有消息,什么都没有,我能找出原因

我通过进入窗口->首选项->服务器->运行时环境->ApacheTomcatv7.0->编辑->并查看Tomcat安装目录字段来检查是否处于正确的根目录中

指向C:/Users/myName/tomcat7.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>
<url-pattern>/foo/</url-pattern>
我已经从errorServlet.java(分别命名为errorServlet.class和errorServlet.jar)编译了.class和.jar文件,它们与.java文件位于同一位置。我错过了什么或做错了什么?为什么我的servlet在到达/foo/*时没有启动


编辑:我已经做出了前两个答案所建议的更改,尽管他们的建议得到了赞赏,但我仍然没有看到任何东西(在这一点上,我会杀掉至少一条错误消息).xml
errorServlet.errorServlet
-意味着包
errorServlet
和类
errorServlet
。但是java代码没有任何包声明。所以Tomcat至少找不到servlet的类


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

在您的WEB INF XML中,有一个突出的错误是

<url-pattern>/foo/*</url>
/foo/*
您正在以“url模式”打开标记,但以“/url”关闭标记。您应该改为使用“/url模式”关闭它。此外,您可能不需要“*”通配符,您可以这样做:

<?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>
<url-pattern>/foo/</url-pattern>
/foo/

试试看,它应该可以工作,其他一切看起来都正常。

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

尝试通过添加一些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日志中的消息。

指出类包问题的要点。我改变了这一点,但仍然没有解决我的问题。我修复了url模式,并尝试使用和不使用通配符。没有什么。