[英]HTTP status 405 - HTTP method POST is not supported by this URL
I am getting this "HTTP method POST is not supported by this URL" error when I run my project.当我运行我的项目时,出现“此 URL 不支持 HTTP 方法 POST”错误。 The funny thing is, it was running perfectly fine two days ago.
有趣的是,两天前它运行得非常好。 After I made a few changes to my code but then restored my original code and its giving me this error.
在我对我的代码进行了一些更改但随后恢复了我的原始代码并且它给了我这个错误之后。 Could you please help me?
请你帮助我好吗?
Here is my index.html:这是我的 index.html:
<form method="post" action="login.do">
<div>
<table>
<tr><td>Username: </td><td><input type="text" name="e_name"/>
</td> </tr>
<tr><td> Password: </td><td><input type="password" name="e_pass"/>
</td> </tr>
<tr><td></td><td><input type="submit" name ="e_submit" value="Submit"/>
Here is my Login servlet:这是我的登录 servlet:
public class Login extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
int status;
String submit = request.getParameter("e_submit");
String submit2 = request.getParameter("a_submit");
out.println("Here1");
String e_name = request.getParameter("e_name");
String e_password = request.getParameter("e_pass");
String a_name = request.getParameter("a_name");
String a_password = request.getParameter("a_pass");
out.println(e_name+e_password+a_name+a_password);
Author author = new Author(a_name,a_password);
Editor editor = new Editor(e_name,e_password);
// If it is an AUTHOR login:
if(submit==null){
status = author.login(author);
out.println("Author Login");
//Incorrect login details
if(status==0) {
out.println("Incorrect");
RequestDispatcher view = request.getRequestDispatcher("index_F.html");
view.forward(request, response);
}
//Correct login details --- AUTHOR
else {
out.println("Correct login details");
HttpSession session = request.getSession();
session.setAttribute(a_name, "a_name");
RequestDispatcher view = request.getRequestDispatcher("index_S.jsp");
view.forward(request, response);
}
}
//If it is an EDITOR login
else if (submit2==null){
status = editor.login(editor);
//Incorrect login details
if(status==0) {
RequestDispatcher view = request.getRequestDispatcher("index_F.html");
view.forward(request, response);
}
//Correct login details --- EDITOR
else {
out.println("correct");
HttpSession session = request.getSession();
session.setAttribute(e_name, "e_name");
session.setAttribute(e_password, "e_pass");
RequestDispatcher view = request.getRequestDispatcher("index_S_1.html");
view.forward(request, response);
} }
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}}
And my web.xml looks like this:我的 web.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>controller.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
I use Glassfish v3 server - let me know anything else you need to know我使用 Glassfish v3 服务器 - 让我知道您需要知道的任何其他信息
That's because on your doGet()
and doPost()
method, you're calling it's super
methods. 这是因为在您的
doGet()
和doPost()
方法上,您将其称为super
方法。 Rather, call the processRequest()
inside the respective methods mentioned above. 而是在上述各个方法中调用
processRequest()
。
The super.doGet()
and super.doPost()
method, returns an HTTP 405, by default, so you don't call your superclass doGet()
and doPost()
. 默认情况下,
super.doGet()
和super.doPost()
方法返回HTTP 405,因此您不必调用超类doGet()
和doPost()
。
Why is there a processRequest
method in your code? 为什么您的代码中有一个
processRequest
方法? Who will call that method? 谁将调用该方法?
You can't get up to that method by calling super.doGet()
or super.doPost()
you need to call that method explicitly. 您无法通过调用
super.doGet()
或super.doPost()
来使用该方法,而需要显式调用该方法。
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req,resp)
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req,resp)
}
EDIT 编辑
Do this 做这个
response.sendRedirect("index_F.html");
return;
instead of 代替
RequestDispatcher view = request.getRequestDispatcher("index_F.html");
view.forward(request, response);
在doPost()
内部,您必须调用processRequest()
。
You're calling doGet()
and doPost()
method without actually implementing (using super
). 您在调用
doGet()
和doPost()
方法时并未实际实现(使用super
)。
The HttpServlet basically follows the template method pattern where all non-overridden HTTP methods returns a HTTP 405
error "Method not supported". HttpServlet基本上遵循模板方法模式,其中所有未覆盖的HTTP方法都返回
HTTP 405
错误“ Method not support”。 When you override such a method, you should not call super
method, because you would otherwise still get the HTTP 405
error. 覆盖此类方法时,不应调用
super
方法,因为否则您仍然会收到HTTP 405
错误。 The same story goes on for your doPost() method. 您的doPost()方法也是如此。
Call the processRequest(req,resp)
inside the respective methods mentioned above. 在上述各个方法中调用
processRequest(req,resp)
。
EDIT: 编辑:
Second, 第二,
Do not use dispatcher to forward request to HTML. 不要使用调度程序将请求转发到HTML。 Use redirect anyway if you want to show html only.
如果只想显示html,请使用重定向。
response.sendRedirect("index_F.html");
return;
Also, It is good practice to use redirect
when you do Logout or send back for invalid credentials. 另外,优良作法是在注销或发送无效凭据时使用
redirect
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.