![](/img/trans.png)
[英]HTTP Status 405 - HTTP method GET is not supported by this URL in Servlets
[英]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方法将其委托给其doGet
, doPost
等方法之一。
您需要重写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.