简体   繁体   English

servlet中的doPost方法没有被调用

[英]doPost method in servlet not getting called

I am trying to override the doGet and doPost and trying to call the doPost but not working. 我试图覆盖doGet和doPost并尝试调用doPost但不能正常工作。 Below is ths JSP and Servlet code 以下是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>

And Servlet code is below and trying to invoke the doPost method but it's not getting called and not printing the message in console. Servlet代码在下面,并尝试调用doPost方法,但未调用它,也不在控制台中打印消息。 However when i try to access the servlet directly from URL, it's doGet method is getting called 但是,当我尝试直接从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);
    }
}

Your HTML form submit button is incorrect: fix type attribute. 您的HTML表单提交按钮不正确:修复类型属性。 Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM