gpt4 book ai didi

java - 从 JSP 向 Servlet 发送数据时出现错误

转载 作者:行者123 更新时间:2023-12-01 13:30:28 25 4
gpt4 key购买 nike

我正在使用动态 Web 项目。这是我的 JSP 代码。我正在尝试向 servlet 发送 Hello

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-
8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="/servlet/ServletCode" flush="true" >
<jsp:param name="username" value="Hello" />
</jsp:include>
</body>
</html>

这是我的 Servlet 文件。

package pack.exp;

public class ServletCode extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String output= request.getParameter("username");
System.out.println(output);
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
}
}

在我的 JSP 文件中,我在这一行收到此编译时错误。

在预期路径/JSpServletCode/WebContent/servlet/ServletCode 处找不到片段“/servlet/ServletCode”

请帮我解决这个问题。

最佳答案

我认为您必须在 web.xml 中映射 servlet,并且必须在页面中提供 servlet-url。像下面这样的东西可以工作。

<jsp:include page="/ServletCode" flush="true" > 

<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>pack.exp.ServletCode</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/ServletCode</url-pattern>
</servlet-mapping>

更新

这对我有用

SERVLET

 @WebServlet("/ServletCode")
public class ServletCode extends HttpServlet {
private static final long serialVersionUID = 1L;

public ServletCode() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String output= request.getParameter("username");
System.out.println(output);
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
}
}

JSP

<body>
<jsp:include page="/ServletCode" flush="true">
<jsp:param name="username" value="Hello" />
</jsp:include>
</body>

关于java - 从 JSP 向 Servlet 发送数据时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21598778/

25 4 0
文章推荐: java - 如何在 java 中将 list 转换为 list
文章推荐: java - 并发HashMap操作