简体   繁体   中英

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:

Here's how look like the 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? 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. HttpServlet implements this method and delegates to one of its doGet , doPost , etc. method based on the HTTP method.

You need to override either service() or the approriate doXxx() methods. Your processRequest method serves no purpose right now.

you need to override the doPost() method and call your processRequest() inside it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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