Java servlet - HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET/POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20831457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java servlets - HTTP Status 405 - HTTP method GET/POST is not supported by this URL
提问by user984621
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? Changing the method how the form is sent out didn't work out...
我是 Java 新手,但是在这个例子中我错过了什么?更改表单发送方式的方法没有奏效...
Thank you in advance.
先感谢您。
回答by Sotirios Delimanolis
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.
any 的入口点Servlet
是service(ServletRequest, ServletResponse)
方法。HttpServlet
实现此方法和代表其中的一个doGet
,doPost
等等方法基于HTTP方法。
You need to override either service()
or the approriate doXxx()
methods. Your processRequest
method serves no purpose right now.
您需要覆盖其中一个service()
或适当的doXxx()
方法。你的processRequest
方法现在没有任何意义。
回答by sasankad
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);
}