gpt4 book ai didi

java - servlet 中的 doPost 方法没有被调用

转载 作者:行者123 更新时间:2023-12-02 10:31:48 26 4
gpt4 key购买 nike

我试图覆盖 doGet 和 doPost 并尝试调用 doPost 但不起作用。下面是 JSP 和 Servlet 代码

    <%@ page language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form method="post" action="FirstServlet">

<input type="text" name="Name" value="Enter">
<input type="button" name="submit" value="Submit"/>
</form>
</body>
</html>

Servlet 代码如下并尝试调用 doPost 方法,但它没有被调用,也没有在控制台中打印消息。然而,当我尝试直接从 URL 访问 servlet 时,它的 doGet 方法被调用

import java.io.IOException;
import javax.servlet.ServletConfig;
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 FirstServlet
*/
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public FirstServlet() {
super();
System.out.println(" Inside Constructor");
// TODO Auto-generated constructor stub
}

/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println(" Inside init");
}


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(" Inside doGet");
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(" Inside doPost");
doGet(request, response);
}
}

最佳答案

您的 HTML 表单提交按钮不正确:修复 type 属性。尝试:

<input type="submit" name="submit" value="Submit"/>

关于java - servlet 中的 doPost 方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579803/

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