HTTP状态 405 - 方法不允许

HTTP状态 405 - 方法不允许

package com.imooc.gim;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class SearchGoodsByIdServlet
 */
@WebServlet("/sg")
public class SearchGoodsByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchGoodsByIdServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
 * @param good 
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response, Object good) throws ServletException, IOException {
  String id = request.getParameter("id");
  Integer goodsId = Integer.parseInt(id);
  ServletContext context=request.getServletContext();
  context.getAttribute("goods");
  List goodsList=(List)context.getAttribute("goods");
  
  Boolean flag=false;
  Goods gooda=null;
  for(int i=0; i<goodsList.size(); i++) {
  gooda=(Goods) goodsList.get(i);
  Integer id1 = gooda.getGoodsId();
  if(id1.equals(goodsId) ) {
   flag=true;
   
  }
      }
  int flag1 =0;  
  
  if(flag) {
  flag1 = 1;
  }else {
  flag1 = 2;
  }
  context.setAttribute("good1",gooda);
  context.setAttribute("flag",flag1);
  
  
    
     request.getRequestDispatcher("/goods.jsp").forward(request, response);
  
         
  
}
}
package com.imooc.gim;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class GoodsAddServlet
 */
@WebServlet("/create")
public class GoodsAddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GoodsAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String goodsId=request.getParameter("goodsId");
    String goodsName=request.getParameter("goodsName");
    String goodsType=request.getParameter("goodsType");
    String price=request.getParameter("price");
    String goodsRemark=request.getParameter("description");
    System.out.println(goodsId);
    Goods good = new Goods(Integer.parseInt(goodsId),goodsName,goodsType,Float.parseFloat(price),goodsRemark);
    ServletContext context=request.getServletContext();
    context.getAttribute("goods");
    List goodsList=(List)context.getAttribute("goods");
goodsList.add(good);
    context.setAttribute("goods", goodsList);
    request.getRequestDispatcher("/gl").forward(request, response);
}
}
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
 
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>商品列表</title>
    <link href="css/bootstrap.css" type="text/css" rel="stylesheet">
    </link>
    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <style type="text/css">
    .pagination {
        margin: 0px
    }
    .pagination>li>a,
    .pagination>li>span {
        margin: 0 5px;
        border: 1px solid #dddddd;
    }
    .glyphicon {
        margin-right: 3px;
    }
    .form-control[readonly] {
        cursor: pointer;
        background-color: white;
    }
    #dlgPhoto .modal-body {
        text-align: center;
    }
    .preview {
        max-width: 500px;
    }
    </style>
    <script>
        $(function () {
            
            $("#btnAdd").click(function () {
                $('#dlgForm').modal()
            });
        })
    </script>
</head>
<body>
    <div class="container">
        <div class="row">
            <h1 style="text-align: center">IMOOC商品信息表</h1>
            <div class="panel panel-default">
                <div class="clearfix panel-heading ">
                    <div>
                        <label class="form-inline">
                            <div class="form-group" style="width: 850px;">
                                <button class="btn btn-primary" id="btnAdd">新增
                                </button>
                            </div>
                            <div class="form-group pull-right">
                            <form action="/Gim/sg" method="post">
                                <input type="text" class="form-control" id="searchById" name="id" placeholder="根据商品id进行查询">
                                <button type="submit" class="btn btn-primary">查询</button>
                                </form>
                            </div>
                        </label>
                    </div>
                    <table class="table table-bordered table-hover">
                     <thead>
                    <c:choose>
                     <c:when test="${applicationScope.flag == 2}">
                                 <h1 style='color:red'>商品id不存在</h1>  
                                 <tr>
                               <th>序号</th>
                                <th>商品编号</th>
                                <th>商品名称</th>
                                <th>商品类别</th>
                                <th>价格</th>
                                <th>备注</th>
                                <th>&nbsp;</th>
                              </tr>                                             
                                                                  
                         </c:when>                       
                         <c:when test="${applicationScope.flag == 1}">
                              <tr>
                                <th>序号</th>
                                <th>商品编号</th>
                                <th>商品名称</th>
                                <th>商品类别</th>
                                <th>价格</th>
                                <th>备注</th>
                                <th>&nbsp;</th>
                         </tr>       
                              <tr>
                                   <th>序号</th>
                                <th>${good1.goodsId}</th>
                                    <th>${good1.goodsName}</th>
                                    <th>${good1.goodsType}</th>
                                    <th>¥<fmt:formatNumber value = "${good1.price}" pattern="0,000.00"></fmt:formatNumber></th>
                                    <th>${good1.goodsRemark}</th>
                                    <th>&nbsp;</th>
                              </tr>                         
                         </c:when>
                         <c:when test="${applicationScope.flag == 0}">  
                               <tr>
                                <th>序号</th>
                                <th>商品编号</th>
                                <th>商品名称</th>
                                <th>商品类别</th>
                                <th>价格</th>
                                <th>备注</th>
                                <th>&nbsp;</th>
                         </tr>                                 
                        <c:forEach items="${applicationScope.goods}" var ="good" varStatus="idx">
                            <tr>
                                <th>${idx.index+1}</th>
                                <th>${good.goodsId}</th>
                                <th>${good.goodsName}</th>
                                <th>${good.goodsType}</th>
                                <th>¥<fmt:formatNumber value = "${good.price}" pattern="0,000.00"></fmt:formatNumber></th>
                                <th>${good.goodsRemark}</th>
                                <th>&nbsp;</th>
                            </tr>
                            </c:forEach>                        
                        </c:when>  
                         </c:choose>
                        
                        </thead>
                    </table>
                </div>
            </div>
        </div>
        <!-- 表单 -->
        <div class="modal fade" tabindex="-1" role="dialog" id="dlgForm">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">新增商品</h4>
                    </div>
                    <div class="modal-body">
                        <form action="/Gim/create" method="post">
                            <div class="form-group">
                                <label>商品编号</label>
                                <input type="text" name="goodsId" class="form-control" id="empno" placeholder="请输入商品编号">
                            </div>
                            <div class="form-group">
                                <label>商品名称</label>
                                <input type="text" name="goodsName" class="form-control" id="ename" placeholder="请输入商品名称">
                            </div>
                            <div class="form-group">
                                <label>商品类别</label>
                                <select id="gname" name="goodsType" class="form-control">
                                    <option selected="selected">服装</option>
                                    <option value="家用">家用电器</option>
                                    <option value="生活">生活用品</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>价格</label>
                                <input type="text" name="price" class="form-control" id="sal" placeholder="请输入价格">
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input type="text" name="description" class="form-control" id="sal" placeholder="请输入备注">
                            </div>
                            <div class="form-group" style="text-align: center;">
                                <button type="submit" class="btn btn-primary">保存</button>
                            </div>
                        </form>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
</body>
</html>
package com.imooc.gim;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class GoodsAddServlet
 */
@WebServlet("/gl")
public class GoodsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
    /**
     * Default constructor. 
     */
    public GoodsListServlet() {
        // TODO Auto-generated constructor stgaub
    }
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request, response);
}
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    ServletContext context=request.getServletContext();
    if(context.getAttribute("goods")==null) {   
        
    List<Goods> goodsList=new ArrayList<>();
    goodsList.add(new Goods(1001,"格力空调","家用电器",3000.00f,"铂金品质"));
    goodsList.add(new Goods(1002,"LV","服装",18888.00f,"生活一场旅行"));
    context.setAttribute("goods", goodsList);
    
    }    
    
    
    request.getRequestDispatcher("/goods.jsp").forward(request, response);
    
   }
    
}
package com.imooc.gim;
public class Goods {
 private Integer goodsId;
 private String goodsName;
 private String goodsType;
 private float price;
 private String goodsRemark;
 public Goods() {
 
 }
public Goods(Integer goodsId, String goodsName, String goodsType, float price, String goodsRemark) {
super();
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goodsType = goodsType;
this.price = price;
this.goodsRemark = goodsRemark;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getgoodsType() {
return goodsType;
}
public void setgoodsType(String goodsType) {
this.goodsType = goodsType;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getGoodsRemark() {
return goodsRemark;
}
public void setGoodsRemark(String goodsRemark) {
this.goodsRemark = goodsRemark;
}
@Override
public String toString() {
return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName + ", goodsType=" + goodsType
+ ", price=" + price + ", goodsRemark=" + goodsRemark + "]";
}
 
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小蓝 2021-12-06 10:22:19

同学你好,同学的SearchGoodsByIdServlet类中的doPost方法中,参数列表错误导致该doPost方法并不满足方法重写语法要求,服务器无法读取doPost方法。

修改方法:将doPost方法参数列表中的Object good参数删除即可。

祝学习愉快~


  • 提问者 晓舟 #1

    为什么默认没有循环遍历list呢?毕竟在SearchGoodsByIdServlet中我设置的flag1=0,却没有执行

                                  
                            <c:forEach items="${applicationScope.goods}" var ="good" varStatus="idx">
                                <tr>
                                    <th>${idx.index+1}</th>
                                    <th>${good.goodsId}</th>
                                    <th>${good.goodsName}</th>
                                    <th>${good.goodsType}</th>
                                    <th>¥<fmt:formatNumber value = "${good.price}" pattern="0,000.00"></fmt:formatNumber></th>
                                    <th>${good.goodsRemark}</th>
                                    <th>&nbsp;</th>
                                </tr>
                                </c:forEach>                        
                            </c:when>  
                             </c:choose>


    2021-12-06 10:41:19
  • 好帮手慕小蓝 回复 提问者 晓舟 #2

    同学你好,同学的遍历代码分别位于<c:choose>的三个<c:when>标签中,而GoodsListServlet中并没有对flag的值进行处理,所以三个<c:when>标签的test判断都是false,都不会执行。

    修改方法,在GoodsListServlet中使用context.setAttribute("flag", 0);语句对flag进行处理之后,就可以正常展示遍历的数据了。

    祝学习愉快~


    2021-12-06 11:04:44
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师