HTTP method POST is not supported by this URL解决方案
来源:互联网 发布:淘宝网店客服中心 编辑:程序博客网 时间:2024/06/05 19:50
刚才在做一个简单的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);
- }
然后就解决了,其实这本不是啥问题,细心一点就不会有啦
阅读全文
0 0
- 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
- 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错误的解决方案
- 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 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
- (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 Status 405 - HTTP method POST is not supported by this URL方法之一
- HTTP Status 405 - HTTP method POST is not supported by this URL
- Mysql日志--缓慢指令日志
- vue开发:vue-cli+axios解决跨域问题
- python常见变量的定义,引用,修改
- 微信授权登录,提示-2,用户取消
- PHP5.6版本安装redis扩展
- HTTP method POST is not supported by this URL解决方案
- PCB各层介绍和AltiumDesigner画PCB时的规则设置
- WINDOWS多线程学习笔记
- 第一章 Shiro简介——《跟我学Shiro》
- Unable to preventDefault inside passive event listener
- [BZOJ1079]迷之DP
- 如何从seo的维度来选择网站的关键词
- 背包模板
- CircularFloatingActionMenu