【问题标题】:how to run web application with only doPost()?如何仅使用 doPost() 运行 Web 应用程序?
【发布时间】:2023-03-27 18:46:01
【问题描述】:
`enter code here`@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String x = req.getParameter("x");
    String y = req.getParameter("y");
    String op = req.getParameter("op");
    try (PrintWriter writer = resp.getWriter()) {
        writer.write("sd");
    }
}

我可以在 Servlet 中仅使用 doPost 方法()来运行 Web 应用程序吗?我做了并且它想要 doGet 方法

【问题讨论】:

    标签: java web web-applications


    【解决方案1】:

    当然,你可以做到。下面是一个例子:

    TestDoPost.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;
    
    @WebServlet("/TestDoPost")
    public class TestDoPost extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        public TestDoPost() {
            super();
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().write("Success!");
        }
    }
    

    NewFile.html:

    <html>
        <head>
            <title>Hello World</title>
        </head>
        <body>
            <form action="TestDoPost" method="post">
                <input type="submit">
            </form>
        </body>
    </html>
    

    部署应用程序并单击 HTML 文件中的 Submit 按钮。结果如下:

    【讨论】:

    • 天哪,你分享了我需要的所有信息,我还没有看到..非常感谢。其实,我并不希望得到答案,但你帮助我。非常感谢)@Arwind Kumar Avinash
    猜你喜欢
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多