<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>商品名稱</td>
<td>商品單價</td>
<td>購買</td>
</tr>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>手機</td>
<td>2000</td>
<td><input type= "submit" name= "buy" value="https://bbs.csdn.net/topics/購買"/>
</tr>
<input type= "hidden" name="goodname" value="https://bbs.csdn.net/topics/手機"/>
<input type="hidden" name= "price" value=https://bbs.csdn.net/topics/ "2000"/>
</form>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>電視</td>
<td>5000</td>
<td><input type= "submit" name= "buy" value="https://bbs.csdn.net/topics/購買"/>
</tr>
<input type= "hidden" name="goodname" value="https://bbs.csdn.net/topics/電視"/>
<input type="hidden" name= "price" value=https://bbs.csdn.net/topics/ "5000"/>
</form>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>洗衣機</td>
<td>3000</td>
<td><input type= "submit" name= "buy" value="https://bbs.csdn.net/topics/購買"/>
</tr>
<input type= "hidden" name="goodname" value="https://bbs.csdn.net/topics/洗衣機"/>
<input type="hidden" name= "price" value=https://bbs.csdn.net/topics/ "3000"/>
</form>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>空調</td>
<td>4000</td>
<td><input type= "submit" name= "buy" value="https://bbs.csdn.net/topics/購買"/>
</tr>
<input type= "hidden" name="goodname" value="https://bbs.csdn.net/topics/空調"/>
<input type="hidden" name= "price" value=https://bbs.csdn.net/topics/ "4000"/>
</form>
<table/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="cc.Good"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8");
String goodname=request.getParameter("goodname");
String price=request.getParameter("price");
Good good =new Good();
good.setGoodname(goodname);
good.setPrice(price);
ArrayList <Good> a=null;
HttpSession sess = request.getSession();
if(session.getAttribute("good")==null){
a=new ArrayList<Good>();
}else{
a=(ArrayList<Good>)session.getAttribute("good");
}
a.add(good);
sess.setAttribute("good", a);
response.sendRedirect("NewFile2.jsp");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="cc.Good"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>商品名稱</td>
<td>商品單價</td>
<td>數量</td>
</tr>
<table/>
<%! ArrayList<Good> arry;
Good good[];
int price;%>
<%
HttpSession ses = request.getSession();
arry=(ArrayList<Good>)ses.getAttribute("good");
if(arry!=null){
for(int i=0;i<arry.size();i++){
good[i]=arry.get(i);
price+=Integer.parseInt(good[i].getPrice());
out.println(good[i].getGoodname());
out.println(good[i].getPrice());
out.println("<br/>");
}
out.println("商品總價"+price);
}
%>
<%=arry %>
</body>
</html>
求大神, good[i]=arry.get(i);為什么這里會報空指標例外啊,該怎么解決
uj5u.com熱心網友回復:
arry不存在,sesssion中沒這個物件。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/159535.html
標籤:Java相關
上一篇:如何在局域網中搭建多播網
下一篇:SpringBoot學習
