Java Get方法在Servlet中工作,POST方法不工作

Java Get方法在Servlet中工作,POST方法不工作,java,servlets,post,Java,Servlets,Post,我是一个编码新手,正在尝试建立一个web表单,可以使用post方法收集信息 我使用了一个在线教程来创建以下servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloForm extends HttpServlet { public void doGet(HttpServletRequest req

我是一个编码新手,正在尝试建立一个web表单,可以使用post方法收集信息

我使用了一个在线教程来创建以下servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloForm extends HttpServlet {


  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");

      PrintWriter out = response.getWriter();
      String title = "Using GET Method to Read Form Data";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +
      "transitional//en\">\n";
      out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>First Name</b>: "
                + request.getParameter("first_name") + "\n" +
                "  <li><b>Last Name</b>: "
                + request.getParameter("last_name") + "\n" +
                "</ul>\n" +
                "</body></html>");
  }


//Method to handle POST method request.
 public void doPost(HttpServletRequest request,
                    HttpServletResponse response)
     throws ServletException, IOException {
    doGet(request, response);
 }

}
import java.io.*;
导入javax.servlet.*;
导入javax.servlet.http.*;
//扩展HttpServlet类
公共类HelloForm扩展了HttpServlet{
公共无效数据集(HttpServletRequest请求,
HttpServletResponse(响应)
抛出ServletException、IOException
{
//设置响应内容类型
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
String title=“使用GET方法读取表单数据”;
字符串docType=
“\n”;
out.println(docType+
“\n”+
“”+标题+“\n”+
“\n”+
“”+标题+“\n”+
“
    \n”+ “
  • 名字:” +request.getParameter(“名字”)+“\n”+ “
  • 姓氏:” +request.getParameter(“姓氏”)+“\n”+ “
\n”+ ""); } //方法来处理POST方法请求。 public void doPost(HttpServletRequest请求, HttpServletResponse(响应) 抛出ServletException、IOException{ doGet(请求、响应); } }
我的html表单如下。当我更改获取的方法时,表单工作。但是,当我将方法更改为POST时,我得到HTTP状态405-此URL不支持HTTP方法POST

<html>
<body>
<form action="HelloForm" method="POST">
First Name: <input type="text" name="first_name">
<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

名字:

姓氏:
我在post方法上尝试了@Override,正如在其他一些线程中所建议的那样,但没有成功


有人能建议出什么问题吗?谢谢

您的代码包含一些错误,因为该代码给出了错误

public void doPost(HttpServletRequest请求, HttpServletResponse(响应) 抛出ServletException、IOException{ doGet(请求,响应);//方法调用不正确
}

如果您使用的是tomcat,您可以试试这个

<servlet-mapping>
     ...
    <http-method>POST</http-method>

</servlet-mapping>

...
邮递

除了web.xml servlet配置中的servlet名称和url映射之外。

还需要在POST函数中编写代码

此外,为什么不使用JSP呢?在处理servlet时,使用MVC方法具有良好的倾向性。 即模型-视图-控制器

将代码传递到JSP(视图)并从那里生成必要的代码


祝你好运

谢谢-应该是什么?