HTTP method POST is not supported by this URL解决方案
2017-10-26 15:31
573 查看
刚才在做一个简单的Session案例——实现用户登录的Demo,将Login.html,LoginServlet.java,IndexServlet.java以及web.xml等代码都写好后。打开登录页面,输入信息后,跳转到如下错误页面
HTTP method POST is not supported by this URL
以下是错误时候的代码
(1)form.html代码如下
<form action="/ServletTest/LoginServlet" method="post">
用户名:<input type="text" name="username" /> <br />
密 码:<input type="password" name="password" /> <br />
<input type="submit" value="提交" id="sub" />
</form>
这里是post方式提交的
(2)LoginServlet.java代码如下
package com.liuyanzhao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* @author LiuYanzhao
*/
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//防止乱码
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
PrintWriter out = resp.getWriter();
//假设正确的账号是admin,密码是123456
if("admin".equals(username)&&"123456".equals(password)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
req.getSession().setAttribute("user", user);
resp.sendRedirect("/ServletTest/IndexServlet");
} else {
out.print("用户名和密码错误,登录失败!");
}
}
}
很明显,错在这里,没有 doPost 方法
因为 post 的提交方式只能用 dopost 方法来处理,get 或者 url 的提交方式 只能用 doget 的方法来处理
解决方案,有两种。
第一种是把 Login.html 里的 post 提交方式改成 get 的
第二种就是在IndexServlet类里面加上 doGet 方法下面 加上 doPost 方法
代码可以如下
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
然后就解决了,其实这本不是啥问题,细心一点就不会有啦
HTTP method POST is not supported by this URL
以下是错误时候的代码
(1)form.html代码如下
<form action="/ServletTest/LoginServlet" method="post">
用户名:<input type="text" name="username" /> <br />
密 码:<input type="password" name="password" /> <br />
<input type="submit" value="提交" id="sub" />
</form>
这里是post方式提交的
(2)LoginServlet.java代码如下
package com.liuyanzhao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* @author LiuYanzhao
*/
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//防止乱码
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
PrintWriter out = resp.getWriter();
//假设正确的账号是admin,密码是123456
if("admin".equals(username)&&"123456".equals(password)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
req.getSession().setAttribute("user", user);
resp.sendRedirect("/ServletTest/IndexServlet");
} else {
out.print("用户名和密码错误,登录失败!");
}
}
}
很明显,错在这里,没有 doPost 方法
因为 post 的提交方式只能用 dopost 方法来处理,get 或者 url 的提交方式 只能用 doget 的方法来处理
解决方案,有两种。
第一种是把 Login.html 里的 post 提交方式改成 get 的
第二种就是在IndexServlet类里面加上 doGet 方法下面 加上 doPost 方法
代码可以如下
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
然后就解决了,其实这本不是啥问题,细心一点就不会有啦
相关文章推荐
- HTTP method POST is not supported by this URL错误的解决方案
- HTTP method POST is not supported by this URL错误的解决方案
- HTTP method POST is not supported by this URL解决方案
- HTTP method POST is not supported by this URL错误的解决方案
- servlet中使用service()方法出现错误:HTTP method POST is not supported by this URL
- (6) HTTP Status 405 - HTTP method POST is not supported by this URL
- HTTP Status 405 - HTTP method POST is not supported by this URL
- 关于解决HTTP Status 405 - HTTP method POST is not supported by this URL方法之一
- HTTP method POST is not supported by this URL
- HTTP method POST is not supported by this URL
- HTTP Status 405 - HTTP method POST is not supported by this URL
- HTTP Status 405 -- HTTP method POST is not supported by this URL
- 解决HTTP Status 405 - HTTP method POST is not supported by this URL 详解方案
- J2EE中HTTP method GET/Post is not supported by this URL
- HTTP Status 405 - HTTP method GET(POST) is not supported by this URL
- 解决HTTP Status 405 - HTTP method POST is not supported by this URL 详解方案
- HTTP method POST is not supported by this URL
- HTTP Status 405 - HTTP method POST is not supported by this URL
- HTTP method POST is not supported by this URL
- HTTP Status 405 - HTTP method POST is not supported by this URL