【问题标题】:Tomcat Servlet PathTomcat Servlet 路径
【发布时间】:2013-02-11 11:08:19
【问题描述】:

我有这个文件夹目录:

webapps/ROOT/META-INF/

......................context.xml

webapps/ROOT/WEB-INF/

......................web.xml

webapps/ROOT/WEB-INF/lib

......................mysql-connector-java-5.1.23-bin.jar

webapps/ROOT/WEB-INF/classes/com/mycompany/server/LoginServlet.class

我有这个 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
        <servlet-class>com.mycompany.server.LoginServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

LoginServlet.java 有这个init()

@Override
public void init(ServletConfig config) throws ServletException {
    System.out.println("Login Servlet Is Running");
    new Thread(guestRouter).start();
    System.out.println("path: '" + config.getServletName() + "'");
}

当我启动 Tomcat 时,这是输出(init 加载并找到类)。

但是,当我转到localhost:8080/LoginServlet 时,它说找不到资源。这几天一直在挣扎。任何帮助表示赞赏!

【问题讨论】:

  • 是否有可能是在 tomcat 配置中有问题,并且它正在将 ROOT 上下文绑定到其他路径?
  • LoginServlet 是做什么的?
  • @RyanStewart,处理登录。具有protected void processRequest(HttpServletRequest request, HttpServletResponse response) 功能。
  • @IanMcMahon,默认配置。
  • 这很重要,因为当您请求它时,您会收到错误消息。问题可能出在 servlet 中。完整的错误消息也可以提供一些启示。

标签: java tomcat


【解决方案1】:

已修复以下内容(WTF Tomcat)。

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.mycompany.server.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
</web-app>   

【讨论】:

    【解决方案2】:

    你需要添加

    &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;LoginServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/LoginServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

    并从您拥有的位置删除&lt;url-pattern&gt;

    【讨论】:

    • 我尝试了 10 种不同的方法,但都没有奏效。我认为这是您的答案和问题标题的结合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 2010-09-08
    • 2012-04-28
    • 2020-02-05
    • 1970-01-01
    • 2014-05-24
    相关资源
    最近更新 更多