servlet处理抛的异常
商品列表jsp主要代码:<%
List<String[]> vgoods=(List<String[]>)request.getAttribute("vgoods");
if(vgoods==null){
Pages pages=new Pages();
pages.setCurPage(1);
String gsql="select Gimgurl,Gname,Gintro,Gclass,"+"Gprice,Gid,Gdate from GoodsInfo";
int totalpage=DBUtil.getTotalPages("select count(*) from goodsinfo");
pages.setTotalPage(totalpage);
pages.setSql(gsql);
vgoods=DBUtil.getPageContent(1, gsql);
request.setAttribute("vgoods", vgoods);
session.setAttribute("pages", pages);
}
%>
</head>
<body>
<c:forEach var="goods" items="${vgoods}">
<div class="x3">
<div class="media padding-bottom clearfix border bg-back">
<img width="100%" src="${pageContext.request.contextPath}/${goods[0]}" class="radius img-responsive" alt="...">
<div class="media-body padding">
<a href="${pageContext.request.contextPath}/GoodsDetail?gid=${goods[5]}">
${goods[1]}
<h4>${fn:replace(goods[2],'|',' ')}</h4>
<h4>所属分类:${goods[3]}</h4>
<h4>定价:¥${goods[4]}</h4>
<!-- <h4>浏览次数:{goods[5]}</h4> -->
<h4>上架日期:${goods[6]}</h4>
<div class="btn_div">
<button class="button bg-sub padding float-right
margin-big-top button-block">修改/删除商品</button>
从这进去商品详情JSP页面:
<% List<String[]> vgoods=(List<String[]>)request.getAttribute("vgoods");
%>
</head>
<body>
<div class="layout">
<div class="line padding-big-top padding-big-bottom navbar bg-blue bg-inverse">
<div class="x2">
<button class="button icon-navicon float-right " data-target="#header-demo3"></button>
<img src="${pageContext.request.contextPath}/img/jclogo(2).png" width="150" class="padding" height="50"/>
</div>
<%@include file="../user/top.jsp" %>
</div>
</div>
<c:forEach var="goods" items="${vgoods}">
<div class="container-layout">
<div class="line border padding-big margin-big-top">
<div class="x3 padding-big">
<img width="100%" class="img-responsive" src="${goods[0]}">
</div>
<div class="x9 float-right padding-big">
<h1>${goods[1]}</h1>
<h4>
<span>${fn:replace(goods[2],'|','') }</span>
</h4>
<h4>所属分类:${goods[3]}</h4>
<h4>定价:¥${goods[4]}</h4>
<!-- <h4>浏览次数:${vgoods[5]}</h4>-->
<h3>商品简述:</h3>
<p>${goods[6]}</p>
<img src="${pageContext.request.contextPath}/img/other/buy.png" border="0"/>
,点击超链接进BuyGoods servlet去处理
BuyGoods.java:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session=request.getSession();
Pages pages=(Pages)session.getAttribute("pages");
Cart cart=(Cart)session.getAttribute("cart");
if(pages==null){
pages=new Pages();
}
if(cart==null){
cart=new Cart();
}
//得到要购买的东西的ID
String gid=request.getParameter("gid").trim();
//判断在哪里购买,0-在缩略图中购买,1-在详细信息中购买
String flag=request.getParameter("flag").trim();
cart.buy(gid);
//得到搜索当前内容的sql
String sql=pages.getSql();
int page=pages.getCurPage();
session.setAttribute("pages", pages);
session.setAttribute("cart", cart);
String url="";
if(flag.equals("0")){
url="/index.jsp";
}
else{
url="/jsp/good/gooddetail.jsp";
sql="select Gimgurl,Gname,Gintro,Gclass,Gprice,"+"Gid,Gbrief from GoodsInfo where Gid="+gid;
page=1;
}
//返回后,得到页面内容
List<String[]> vgoods=DBUtil.getPageContent(page, sql);
if(vgoods.size()==0){
String msg="对不起,没有搜索到你要的商品!!!";
request.setAttribute("msg", msg);
}
if(flag.equals("0")){
request.setAttribute("vgoods", vgoods);
}
else{
request.setAttribute("vgoods", vgoods.get(0));
}
//forward 到要去的页面
ServletContext sc=getServletContext();
RequestDispatcher rd=sc.getRequestDispatcher(url);
rd.forward(request, response);
}
}
结果抛出异常我查了一下说是转型问题,搞了半天还是没弄出来,望各位大神指点迷津
异常如下:message java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.wywydo.BuyGoods.doPost(BuyGoods.java:91)
com.wywydo.BuyGoods.doGet(BuyGoods.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.wywy.filter.CharsetFilter.doFilter(CharsetFilter.java:48)
root cause
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List
org.apache.jsp.jsp.good.gooddetail_jsp._jspService(gooddetail_jsp.java:113)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.wywydo.BuyGoods.doPost(BuyGoods.java:91)
com.wywydo.BuyGoods.doGet(BuyGoods.java:40)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,最后还是自己搞出来了,request.setAttribute("vgoods", vgoods.get(0))这行代码传进去的vgoods不是List类型,然后在detail.jsp中取出来的时候赋值给list了所有类转型错了,vgoods.get(0)改为vgoods就OK了