Tomcat Servlet路径

时间:2013-02-26 04:58:18

标签: java tomcat

我有这个文件夹目录:

的webapps / ROOT / META-INF /

...................... context.xml中

的webapps / ROOT / WEB-INF /

......................的web.xml

的webapps / ROOT / WEB-INF / lib中

......................... MySQL的连接器的Java-5.1.23-bin.jar

的webapps / ROOT / WEB-INF /类/ COM / myCompany的/服务器/ 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加载并找到类)。

enter image description here

然而,当我转到localhost:8080/LoginServlet时,它说资源未找到。一直在苦苦挣扎。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:3)

您需要添加

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

并从您拥有的地方删除<url-pattern>

答案 1 :(得分: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>   
相关问题