gpt4 book ai didi

jsp - 无法在浏览器中打开 servlet

转载 作者:行者123 更新时间:2023-11-28 23:58:52 25 4
gpt4 key购买 nike

<分区>

我有一个名为 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 文件夹中。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com