• 那是从何处传来的钟声呢?偶尔听到那钟声,平添一份喜悦与向往之情。

HTTP method POST is not supported by this URL解决方案

后端 Nanait 12年前 (2012-09-03) 1159次浏览 已收录 0个评论 扫描二维码

刚才在做一个简单的 Session 案例——实现用户登录的 Demo,将 Login.html,LoginServlet.java,IndexServlet.java 以及 web.xml 等代码都写好后。打开登录页面,输入信息后,跳转到如下错误页面

HTTP method POST is not supported by this URL 解决方案

HTTP method POST is not supported by this URL

以下是错误时候的代码

(1)form.html 代码如下

  1.  <form action=“/ServletTest/LoginServlet” method=“post”>
  2.         用户名:<input type=“text” name=“username” /> <br />
  3.         密 码:<input type=“password” name=“password” /> <br />
  4.         <input type=“submit” value=“提交” id=“sub” />
  5. </form>

这里是 post 方式提交的

(2)LoginServlet.java 代码如下

  1. package com.liuyanzhao;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. /*
  9.  * @author LiuYanzhao
  10.  */
  11. public class LoginServlet extends HttpServlet{
  12.     @Override
  13.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  14.         //防止乱码
  15.         resp.setContentType(“text/html;charset=utf-8”);
  16.         String username = req.getParameter(“username”);
  17.         String password = req.getParameter(“password”);
  18.         PrintWriter out = resp.getWriter();
  19.         //假设正确的账号是 admin,密码是 123456
  20.         if(“admin”.equals(username)&&“123456”.equals(password)) {
  21.             User user = new User();
  22.             user.setUsername(username);
  23.             user.setPassword(password);
  24.             req.getSession().setAttribute(“user”, user);
  25.             resp.sendRedirect(“/ServletTest/IndexServlet”);
  26.         } else {
  27.             out.print(“用户名和密码错误,登录失败!”);
  28.         }
  29.     }
  30. }

很明显,错在这里,没有 doPost 方法

因为 post 的提交方式只能用 dopost 方法来处理,get 或者 url 的提交方式 只能用 doget 的方法来处理

 

解决方案,有两种。

第一种是把 Login.html 里的 post 提交方式改成 get 的

第二种就是在 IndexServlet 类里面加上 doGet 方法下面 加上 doPost 方法

代码可以如下

  1. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  2.         doGet(req, resp);
  3. }

然后就解决了,其实这本不是啥问题,细心一点就不会有啦

 


何处钟 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:HTTP method POST is not supported by this URL 解决方案
喜欢 (1)
[15211539367@163.com]
分享 (0)

您必须 登录 才能发表评论!