簡體   English   中英

Java Servlet-HTTP狀態405-此URL不支持HTTP方法GET / POST

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

我創建了一個表格:

  <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>

發送此表格時,會出現以下錯誤:

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

我在表單post更改了get ,但是出現了類似的錯誤:

這是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();
    }
  }
}  

我是Java的新手,但是在此示例中我缺少什么? 更改發送表單的方式的方法無法解決...

先感謝您。

任何Servlet的入口點都是service(ServletRequest, ServletResponse)方法。 HttpServlet實現此方法,並基於HTTP方法將其委托給其doGetdoPost等方法之一。

您需要重寫service()或適當的doXxx()方法。 您的processRequest方法現在沒有任何作用。

您需要重寫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