新手请教:要实现购物车功能,目前只能显示表头,表里的数据显示不了(没有实现遍历功能),代码如下:获得商品信息的GetAllGoodsServlet:public class GetAllGoodsServlet extends HttpServlet {
public GetAllGoodsServlet(){
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
request.setCharacterEncoding("GB2312");
List<Goods> gList = new ArrayList<Goods>();
//到数据库查询所有的商品
try {
gList = GoodsDB.getAll();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置到属性中
session.setAttribute("gList", gList); //本来是request.setAttribute,但也是显示不了数据。
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}购物车页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="com.fendou.servlet.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>商场主页</title>
<script type="text/javascript">
function addToCar(gid)
{
window.location.href="AddToCarServlet?gid="+gid;
}
</script>
</head>
<body>
<table border="1" cellspacing="0">
<tr>
<td>商品ID</td>
<td>类别</td>
<td>名称</td>
<td>价格</td>
<td>详细参数</td>
<td>操作</td>
</tr>
<c:forEach items="${gList}" var="g"> <!--改成sessionScope.gList也不行-->
<tr>
<td>${g.gid}</td>
<td>${g.gtype}</td>
<td>${g.gname}</td>
<td>${g.price}</td>
<td>${g.ginfo}</td>
<td>
<input type="button" value="添加到购物车" onclick="addToCar(${g.gid})"/>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
连接数据库的代码:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;import com.fendou.vo.Goods;public class GoodsDB { //得到所有的商品
public static List<Goods> getAll() throws Exception{
List<Goods> list = new ArrayList<Goods>();
String sql = "select * from 商品表";
Connection conn = DBManager.getConn();
PreparedStatement pstmt = DBMethod.getPstmt(sql);
ResultSet rs = DBMethod.getRs(pstmt, sql);
while(rs.next()){
Goods good = new Goods();
good.setGid(rs.getInt(1));
good.setGtype(rs.getString(2));
good.setGname(rs.getString(3));
good.setPrice(rs.getDouble(4));
good.setGinfo(rs.getString(5));
list.add(good);
}
return list;
}
//根据ID得到商品
public static Goods findById(int id){
String sql = "select * from 商品表 where 商品ID=?";
Connection conn = DBManager.getConn();
PreparedStatement pstmt = DBMethod.getPstmt(sql);
ResultSet rs = null;
Goods good = new Goods();
try {
pstmt.setInt(1, id);
rs = DBMethod.getRs(pstmt,sql);
if(rs.next()){
good.setGid(rs.getInt(1));
good.setGtype(rs.getString(2));
good.setGname(rs.getString(3));
good.setPrice(rs.getDouble(4));
good.setGinfo(rs.getString(5));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return good;
}
}遍历JSP JAVA
public GetAllGoodsServlet(){
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
request.setCharacterEncoding("GB2312");
List<Goods> gList = new ArrayList<Goods>();
//到数据库查询所有的商品
try {
gList = GoodsDB.getAll();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置到属性中
session.setAttribute("gList", gList); //本来是request.setAttribute,但也是显示不了数据。
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}购物车页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="com.fendou.servlet.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>商场主页</title>
<script type="text/javascript">
function addToCar(gid)
{
window.location.href="AddToCarServlet?gid="+gid;
}
</script>
</head>
<body>
<table border="1" cellspacing="0">
<tr>
<td>商品ID</td>
<td>类别</td>
<td>名称</td>
<td>价格</td>
<td>详细参数</td>
<td>操作</td>
</tr>
<c:forEach items="${gList}" var="g"> <!--改成sessionScope.gList也不行-->
<tr>
<td>${g.gid}</td>
<td>${g.gtype}</td>
<td>${g.gname}</td>
<td>${g.price}</td>
<td>${g.ginfo}</td>
<td>
<input type="button" value="添加到购物车" onclick="addToCar(${g.gid})"/>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
连接数据库的代码:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;import com.fendou.vo.Goods;public class GoodsDB { //得到所有的商品
public static List<Goods> getAll() throws Exception{
List<Goods> list = new ArrayList<Goods>();
String sql = "select * from 商品表";
Connection conn = DBManager.getConn();
PreparedStatement pstmt = DBMethod.getPstmt(sql);
ResultSet rs = DBMethod.getRs(pstmt, sql);
while(rs.next()){
Goods good = new Goods();
good.setGid(rs.getInt(1));
good.setGtype(rs.getString(2));
good.setGname(rs.getString(3));
good.setPrice(rs.getDouble(4));
good.setGinfo(rs.getString(5));
list.add(good);
}
return list;
}
//根据ID得到商品
public static Goods findById(int id){
String sql = "select * from 商品表 where 商品ID=?";
Connection conn = DBManager.getConn();
PreparedStatement pstmt = DBMethod.getPstmt(sql);
ResultSet rs = null;
Goods good = new Goods();
try {
pstmt.setInt(1, id);
rs = DBMethod.getRs(pstmt,sql);
if(rs.next()){
good.setGid(rs.getInt(1));
good.setGtype(rs.getString(2));
good.setGname(rs.getString(3));
good.setPrice(rs.getDouble(4));
good.setGinfo(rs.getString(5));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return good;
}
}遍历JSP JAVA
解决方案 »
- ssh整合开启log4j查看日志内容怎么这么多啊?很多debug?
- 问一个关于Connection reset的问题???
- 超简单的问题,送分啦
- 一个javabean问题
- 请问怎样实现延迟 3秒后自动控制鼠标点击 页面上的某一坐标?
- 为什么在jsp中不能再次对cookie数据进行修改?(在线等候)
- 据说这是爱因斯坦在20世纪初出的这个问题,他说世界上有98%的人答不出来(注:要用JAVA实现)。
- 找不到文件“-Dsun.io.useCanonCaches=false"(或它得组件之一).请确定路径和文件是否正确,而且所需的库文件均可用
- 不好意思,只有10分,---------请教一个bean的小问题!
- websocket怎么创建多个聊天室
- tomcat怎么没有回应?
- 求助!js显示返回的请求数据中文乱码问题
你先看看你的gList里面有值么。
试试
<c:forEach var="gg" items="${gList}" varStatus="status">
${status.count}
</c:forEach>
然后再看看<td>${g.gid}</td>的大小写问题。
我用<%=session.getAttribute("gList") %>得到是null,说明gList里面根本是空的,为什么呢?
//设置到属性中
session.setAttribute("gList", gList); //本来是request.setAttribute,但也是显示不了数据。这里应该是:request.getSession().setAttribute("gList", gList);的吧?
//设置到属性中
session.setAttribute("gList", gList); //本来是request.setAttribute,但也是显示不了数据。这里应该是:request.getSession().setAttribute("gList", gList);的吧?你session就是的值就是request.getSession();我觉得你先打下断点,看下servlet中的gList是否有值。怎么看过程都没错啊
在这个设置之前,你应该把gList的打印一下,看看。有没有值
再其次就是以后,最好不要用gList这样的一个大写一个小写的形式,你可以:glist吧。