无法在浏览器中打开servlet

时间:2017-09-28 20:16:30

标签: jsp tomcat servlets http-status-code-404

我有一个名为DiceRollServlet的servlet,我尝试在浏览器中运行。但是,我无法使其发挥作用,也无法弄清楚原因。我在web.xml文件中有名称映射并尝试了各种路径:

http://localhost:8080/AppName/Lottery
http://localhost:8080/AppName/Lottery.do
http://localhost:8080/AppName/src/servlets/Lottery
http://localhost:8080/AppName/src/servlets/Lottery.do
http://localhost:8080/AppName/target/classes/Lottery
http://localhost:8080/AppName/target/classes/Lottery.do

但不起作用。

的web.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>HelloWorld Application</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>DiceRoll</servlet-name>
        <servlet-class>servlets.DiceRollServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DiceRoll</servlet-name>
        <url-pattern>/Lottery</url-pattern>
    </servlet-mapping>

</web-app>

DiceRollServlet.java:

package servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;


public class DiceRollServlet extends HttpServlet  {
            public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException  {
      PrintWriter out = response.getWriter();

      Random rand = new Random();

      out.println("<p>Your random numer is: " + rand.nextInt(100) + "/p>");
            }

}

文件夹结构:

tomcat
    webapps
        AppName
            src
                servlets
                      DiceRollServlet.java
            pom.xml
            web.xml

修改

我将文件夹结构更改为

tomcat
    webapps
        AppName
            pom.xml
            WEB-INF
                web.xml
                classes
                    servlets
                        DiceRollServlet.java

和web.xml到

<?xml version="1.0" encoding="UTF-8" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>AppName</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>DiceRoll</servlet-name>
        <servlet-class>servlets.DiceRollServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DiceRoll</servlet-name>
        <url-pattern>/Lottery</url-pattern>
    </servlet-mapping>

</web-app>

但它仍然不起作用......

我应该将已编译的.class文件移到某处吗?现在它位于tomcat/webapps/AppName/target/classes/servlets文件夹中。

1 个答案:

答案 0 :(得分:0)

所有servlet文件必须位于此路径中.---&gt; (YOUR-DRIVE-PATH)\ Apache的Tomcat的7.0.34(YOUR-VERSION)\ web应用\ AppName的\ WEB-INF \类

web.xml路径--------&gt; (YOUR-DRIVE-PATH)\ Apache的Tomcat的7.0.34(YOUR-VERSION)\ web应用\ AppName的\ WEB-INF

你的web.xml应该是这样的

<servlet>
    <servlet-name>DiceRoll</servlet-name>
    <servlet-class>DiceRollServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DiceRoll</servlet-name>
    <url-pattern>/Lottery</url-pattern>
</servlet-mapping>