关于HTTP状态405-此URL不支持HTTP方法GET解决方案
public class LoginServlet extends HttpServlet {
@Override
//1.初始化登入次数
public void init() throws ServletException {
//2.获取全局管理者
ServletContext context=getServletContext();
//3.初始化次数
context.setAttribute("count", 0);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//0.设置编码
resp.setContentType("text/html;charset=utf-8");
//1.接受用户名和密码
String username= req.getParameter("username");
String password= req.getParameter("password");
//2.调用userservice里的login(username , password)返回值:User
User user = null;
try {
//携带表单数据的name,pwd去数据库查询;
user = new userserice().login(username , password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//3.判断user是否为空
if(user==null){
//3.1若为空写"用户名和密码不匹配'
resp.getWriter().print("用户名和密码不匹配,3秒之后跳转主页");
//定时跳转
resp.setHeader("refresh", "3;url=/Servlet-login/login.html");
}else{
//3.2若不为空写"xxx:欢迎回来”
resp.getWriter().print(user.getUsername()+"欢迎回来");
//4.获取全局管理者
ServletContext context = this. getServletContext( ) ;
//5.获取总次数
Integer cishu = (Integer) context . getAttribute("count");
//6.将次数+1
cishu++;
//7.将次数再次放入域对像中
context . setAttribute("count",cishu);
}
//super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
结束语:
一把泪,解决的办法:一是要把post提交的方法改为get;
改完你就认为问题就解决了?,nonono,真是当头一棒啊;
去度娘找了下终于找解决办法;
竟然只要去掉//super.doGet(req, resp);就可以了但具体原因不明;
解决注明处:用户对问题“此URL不支持HTTP状态405-HTTP方法GET”的回答 - 问答 - 云+社区 - 腾讯云