Http 将参数传递到servlet时出错

Http 将参数传递到servlet时出错,http,servlets,Http,Servlets,我想实现一个servlet来从浏览器获取参数,并使用HTTPPOST而不是HTTPGET将其插入数据库 servlet将从这样的url接收参数,并将其插入数据库中,但好像我不能正确地执行 下面是我试图运行的代码 public class NewServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOE

我想实现一个servlet来从浏览器获取参数,并使用HTTPPOST而不是HTTPGET将其插入数据库

servlet将从这样的url接收参数,并将其插入数据库中,但好像我不能正确地执行

下面是我试图运行的代码

  public class NewServlet extends HttpServlet {

  public void doPost(HttpServletRequest request,

  HttpServletResponse response)

  throws IOException, ServletException{

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();  

  String firstName = request.getParameter("firstname");

  String middleName = request.getParameter("middlename");

  String lastName = request.getParameter("lastname"); 

  String location = request.getParameter("location");

  String result;

  java.sql.Connection connDB = null;

  try {

            Class.forName("org.postgresql.Driver");

        } catch (ClassNotFoundException ex) {

            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {

            connDB = DriverManager.getConnection("jdbc:postgresql://" + "localhost" + ":" + 5432 + "/" + "mydb", "username", "secret");

            connDB.setAutoCommit(false);

            System.out.println("Connection established : [" + connDB.toString() + "]");

            java.sql.Statement bankStmt = connDB.createStatement();

           java.sql.Statement stt = connDB.createStatement();

            bankStmt.execute("INSERT INTO full_names(firstname, secondname, lastname) VALUES('"+firstName+"', '"+middleName+"', '"+lastName+"' )");

            java.sql.Statement bnk =connDB.createStatement();

            bnk.execute("INSERT INTO employee_details(location) VALUES('"+location+"')");

           }

            connDB.commit();

        } catch (SQLException ex) {

            ex.printStackTrace();

            try {

                connDB.rollback();

            } catch (SQLException ex1) {

                ex1.printStackTrace();

                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex1);


            }
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);

        }




  out.println("<b><font color='blue'>Your FirstName is :</font></b>" 

  + "<b>"+ firstName +"</b>" + "<br>");

  out.println("<b><font color='blue'>Your Middle Name is :</font></b>" 

  + "<b>"+ middleName +"</b>" + "<br>");


  out.println("<b><font color='blue'>Your Last Name is :</font></b>"  

  + "<b>"+ lastName +"</b>");
  }
}
公共类NewServlet扩展了HttpServlet{
public void doPost(HttpServletRequest请求,
HttpServletResponse(响应)
抛出IOException、ServletException{
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
String firstName=request.getParameter(“firstName”);
字符串middleName=request.getParameter(“middleName”);
字符串lastName=request.getParameter(“lastName”);
字符串位置=request.getParameter(“位置”);
字符串结果;
java.sql.Connection connDB=null;
试一试{
Class.forName(“org.postgresql.Driver”);
}捕获(ClassNotFoundException ex){
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE,null,ex);
}
试一试{
connDB=DriverManager.getConnection(“jdbc:postgresql://“+”localhost“+”:“+5432+”/“+”mydb”、“用户名”、“密码”);
connDB.setAutoCommit(假);
System.out.println(“已建立连接:[“+connDB.toString()+”]);
java.sql.Statement banksmt=connDB.createStatement();
java.sql.Statement stt=connDB.createStatement();
bankStmt.execute(“插入全名(firstname、secondname、lastname)值(“+firstname+”、“+middleName+”、“+lastname+”))”);
java.sql.Statement bnk=connDB.createStatement();
bnk.execute(“插入员工详细信息(位置)值(“+”位置+”)”);
}
commit();
}catch(SQLException-ex){
例如printStackTrace();
试一试{
connDB.rollback();
}捕获(SQLException ex1){
ex1.printStackTrace();
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE,null,ex1);
}
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE,null,ex);
}
out.println(“您的名字是:”
+“+firstName+”+“+”
”; out.println(“您的中间名是:” +“+middleName++”
”; out.println(“您的姓是:” +“+lastName+”); } }
当我尝试使用url运行代码时

我得到以下错误:

HTTP状态405-此URL不支持HTTP方法GET

类型状态报告

此URL不支持消息HTTP方法GET

说明请求的资源不允许使用指定的HTTP方法(此URL不支持HTTP方法GET)

HTTP状态405-此URL不支持HTTP方法GET

这已经是全部答案了。您正在发送GET请求,但servlet实现不支持它。根据您编写的代码,它只支持POST请求。任何地方都没有
doGet()
实现,只有
doPost()

我不确定功能需求是什么,也不清楚为什么会出现此错误,但要让代码运行,您应该发送POST请求,或者在servlet中将
doPost
方法重命名为
doGet


与具体问题无关您的代码还有其他问题,包括SQL注入漏洞、DB资源泄漏以及在控制器中混合视图。要正确地学习servlet,您可能需要从开始

HTTP状态405-此URL不支持HTTP方法GET

这已经是全部答案了。您正在发送GET请求,但servlet实现不支持它。根据您编写的代码,它只支持POST请求。任何地方都没有
doGet()
实现,只有
doPost()

我不确定功能需求是什么,也不清楚为什么会出现此错误,但要让代码运行,您应该发送POST请求,或者在servlet中将
doPost
方法重命名为
doGet



与具体问题无关您的代码还有其他问题,包括SQL注入漏洞、DB资源泄漏以及在控制器中混合视图。要正确学习servlet,您可能需要从开始。

您只在servlet中定义了do Post()方法。但是,当您使用访问时,
将调用尚未定义的doGet()。只需将doPost()方法中的代码复制并粘贴到同一servlet中的doGet()中
像这样:

public void doGet{
 //your code
}

您只在servlet中定义了do Post()方法。但是,当您使用访问时,
将调用尚未定义的doGet()。只需将doPost()方法中的代码复制并粘贴到同一servlet中的doGet()中
像这样:

public void doGet{
 //your code
}

或者只需从doGet调用doPost而不是代码复制。或者只需从doGet调用doPost而不是代码复制。