gpt4 book ai didi

java - Jetty - 找不到 Servlet

转载 作者:搜寻专家 更新时间:2023-11-01 02:25:22 25 4
gpt4 key购买 nike

我是 Servlet 新手,想通过 Eclipse 的 Jetty 插件调用一个简单的 Servlet。我可以调用 index.html,但在尝试访问 Servlet 时,我得到:

HTTP ERROR: 404
Problem accessing /ProjectServlet. Reason:
NOT_FOUND

我认为我正确配置了所有文件,但无法解释 Jetty 返回此错误的原因。

谢谢你帮助我!


index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>TEST</title>
</head>
<body>
<h1>Servlet Call Test:</h1>
<form action="/ProjectServlet" method="GET">
<input type="text" name="name" maxlength="20" value="Name" onfocus="this.select()"/><br>
<input type="submit" name="callservlet" value="Call Servlet."/>
</form>
</body>
</html>

ProjectServlet.java

package servlets;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class ProjectServlet
*/
@WebServlet(description = "Servlet to return JSON response with project list", urlPatterns = { "/ProjectServlet" })
public class ProjectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public ProjectServlet() {
super();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//Call Servlet Button
if (request.getParameter("callservlet") != null) {

response.setContentType("application/json");

String name = request.getParameter("name");
String jsonexample = "hi " + name;

response.getWriter().write(jsonexample);
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>ProjectServlet</servlet-name>
<servlet-class>servlets.ProjectServlet</servlet-class>
</servlet>

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

最佳答案

这应该如下所示,在 Servlet url 之前添加上下文路径

<form action="${pageContext.servletContext.contextPath}/ProjectServlet" 
method="GET">

代替

<form action="/ProjectServlet" method="GET">

关于java - Jetty - 找不到 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24995773/

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