簡體   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