JavaWEB(EL+JSTL实现购物车)

一.首页

 
<%@page import="com.zking.web.entity.Goods"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.web.dao.GoodsDao"%>
<%@page import="com.zking.web.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 通过session默认保存一个用户 -->
 
	<h3>zz购物商城首页</h3>
 
	<p>欢迎您!${username }</p>
	<p align="center">
		<from action="doLoad.jsp" method="post">名称:<input type="text"
			name="strName">
		<button type="submit" value="搜索">搜索</button>
		</from>
	</p>
	<table border="1" width="70%" align="center">
		<tr>
			<th>编号</th>
			<th>名称</th>
			<th>价格</th>
			<th>描述</th>
			<th>图片</th>
			<th>操作</th>
		</tr>
		<c:if test="${empty requestScope.listGoods }">
			<jsp:forward page="doLoad.jsp"></jsp:forward>
		</c:if>
		<c:forEach items="${requestScope.listGoods }" var="goods">
			<tr>
				<td>${goods.gid }</td>
				<td>${goods.gname }</td>
				<td>${goods.gprice }</td>
				<td>${goods.ginfo }</td>
				<td><img style="width: 80px; height: 50px;"
					src="${goods.gpath }" /></td>
				<td>
					<button onclick="addCart(${goods.gid})">加入购物车</button>
				</td>
			</tr>
		</c:forEach>
	</table>
 
	<p align="right" style="font-size: 20px; font-weight: bold">
 
		当前页数:[${pageIndex }/${pageMax }]&nbsp; <a href="index.jsp?pageIndex=1">首页</a>
		<a href="index.jsp?pageIndex=${ pageIndex-1<0?1:pageIndex-1}">上一页</a>
		<a
			href="index.jsp?pageIndex=${ pageIndex+1>pageMax?pageMax:pageIndex+1}">下一页</a>
		<a href="index.jsp?pageIndex=${ pageMax }">末页</a>
	</p>
	<script type="text/javascript">
	//加入购物车的点击事件
	function addCart(cid){
		//alert(cid)
		location.href = "doShopping.jsp?cid="+cid;
	}
 
</script>
 
</body>
</html>

二. 处理页面

//1.设置编码
    	request.setCharacterEncoding("utf-8");
    	//2.获取信息
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	//3.数据库交互
    	 //加载驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//建立连接
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		Connection conn = DriverManager.getConnection(url, "scott", "123");
    	//sql
    	String sql = "select * from tb_news_users where username = ? and password = ?";
    	PreparedStatement ps = conn.prepareStatement(sql);
    	ps.setString(1,username);
    	ps.setString(2,password);
    	ResultSet rs = ps.executeQuery();
    	
    	//4.判断页面跳转
		if(rs.next()){
			//跳转到前端的首页
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else{
			out.println("<script>alert('账户或密码错误');location.href='login.jsp'</script>");
		}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值