简体   繁体   English

Java Servlet-HTTP状态405-此URL不支持HTTP方法GET / POST

[英]Java servlets - HTTP Status 405 - HTTP method GET/POST is not supported by this URL

I have created a form: 我创建了一个表格:

  <form method="post" action="new">
      <input type="text" name="title" />
      <input type="text" name="description" />
      <input type="text" name="released" />

      <input type="submit" value="Send" />
    </form>

When I send this form, I will get following error: 发送此表格时,会出现以下错误:

HTTP Status 405 - HTTP method POST is not supported by this URL

I changed in the form post to get , but I got a similar error: 我在表单post更改了get ,但是出现了类似的错误:

Here's how look like the servlet: 这是servlet的外观:

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

public class MyServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

    String title = request.getParameter("title");
    String description = request.getParameter("description");
    String released_string = request.getParameter("released");
    int released = Integer.parseInt(released_string);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:8889/app_name", "username", "password");
        PreparedStatement ps=con.prepareStatement("insert into movies values(?, ?, ?)");

        ps.setString(1, title);
        ps.setString(2, description);
        ps.setString(3, released_string);
        int i=ps.executeUpdate();    
    } catch(Exception se) {
        se.printStackTrace();
    }
  }
}  

I am newbie in Java, but what am I missing in this example? 我是Java的新手,但是在此示例中我缺少什么? Changing the method how the form is sent out didn't work out... 更改发送表单的方式的方法无法解决...

Thank you in advance. 先感谢您。

The entry point of any Servlet is the service(ServletRequest, ServletResponse) method. 任何Servlet的入口点都是service(ServletRequest, ServletResponse)方法。 HttpServlet implements this method and delegates to one of its doGet , doPost , etc. method based on the HTTP method. HttpServlet实现此方法,并基于HTTP方法将其委托给其doGetdoPost等方法之一。

You need to override either service() or the approriate doXxx() methods. 您需要重写service()或适当的doXxx()方法。 Your processRequest method serves no purpose right now. 您的processRequest方法现在没有任何作用。

you need to override the doPost() method and call your processRequest() inside it. 您需要重写doPost()方法并在其中调用processRequest()

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

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

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