简体   繁体   中英

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. I got it setup so that I can navigate to /newpath/error.faces just find. The problem when I start the server in eclipse and navigate to any URL matching that /foo/* mapping I get nothing. No a 404 in the browser and jack squat in the console. 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.

which points to C:/Users/myName/Tomcat 7.0.

The web.xml file in C:/Users/myName/Tomcat 7.0/webapps/ROOT/WEB-INF looks like:

<?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

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. What am I missing or doing wrong? Why is my servlet not firing when I got to /foo/*?

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

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

You are opening the tag with "url-pattern" but closing it with "/url". You should instead close it with "/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. But your java code has no any package declaration. So Tomcat at least cannot find the class of 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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