jsp大作业,题目:TOP粉丝俱乐部,实现功能:登陆,注册,主页信息查看,购买周边,留言板,喜爱程度测试,对粉丝俱乐部的意见调查。
login.jsp
登录页面:
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登陆页面</title>
</head>
<script>
function check1(){
if(form2.custname.value==""||form2.password.value==""){ //null 和 ""是有区别的
alert("内容不为空");
return false;
}
}
</script>
<body >
<h1><font color="gray">欢迎光临T.O.P粉丝俱乐部!</font></h1><br>
${sessionScope.message}
<form action="Loginservlet" method="post" name="form2" οnsubmit="return check1()">
<table>
<tr><td>请输入用户名和密码:</td></tr>
<tr><td>用户名:</td>
<td><input type="text" name="custname"/></td>
</tr>
<tr><td> 密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr><td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
还没注册?<a href="register.jsp">注册</a>
</form>
</body>
</html>
主页index.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>信息展示</title>
<style>
html, body{
width:100%;
height:100%;
background: rgb(236, 227, 212);
position: relative;
text-decoration: none;
padding:0;
margin:0;
}
.header{
position: fixed;
top:0;
width:100%;
height:60px;
border:1px solid black;
}
.list{
position: absolute;
width:100%;
height:100%;
padding:0;
margin:0;
}
.list li{
list-style: none;
float:left;
width:448px;
height:60px;
text-align: center;
line-height: 60px;
border-right:1px solid black;
}
.list li a{
height:60px;
text-align: center;
line-height: 60px;
color:#007979;
text-decoration: none;
}
.intro{
position: relative;
width:60%;
left:50px;
top:10%;
height:45%;
}
.intro img{
width:200px;
height:100px;
}
.intro p{
font-size:20px;
/*color:aqua;颜色*/
}
/*图片的大小自己定*/
.photo2{
position: relative;
float:right;
top:-200px;
right:120px;
width:300px;
height:450px;
background-repeat: no-repeat;
background-size: 100% 100%;
}
#banner
{position:relative; width:400px; height:400px; border:1px solid #666; overflow:hidden;}
#banner_list img {border:0px; width:400px; height:400px;}
#banner_bg {position:absolute; bottom:0;background-color:#000;height:30px;filter: Alpha(Opacity=30);opacity:0.3;z-index:1000;
cursor:pointer; width:400px;}
#banner_info{position:absolute; bottom:0; left:5px;height:22px;color:#fff;z-index:1001;cursor:pointer}
#banner_text {position:absolute;width:120px;z-index:1002; right:3px; bottom:3px;}
#banner ul {position:absolute;list-style-type:none;filter: Alpha(Opacity=80);opacity:0.8; border:1px solid #fff;z-index:1002;
margin:0; padding:0; bottom:3px; right:5px;}
#banner ul li { padding:0px 8px;float:left;display:block;color:#FFF;border:#e5eaff 1px solid;background:#6f4f67;cursor:pointer}
#banner ul li.on { background:#900}
#banner_list a{position:absolute;}
#photoShow
{
border:solid 1px #C5E88E;
overflow:hidden;
width:640px;
height:550px;
right:0px;
top:500px;
background:C5E88E;
position:absolute;
}
.photo
{
position:absolute;
top:0px;
width:640px;
height:550px;
}
.photo img
{
width:640px;
height:550px;
}
.photo span
{
padding:5px 0px 0px 5px;
width:1280px;
height:70px;
position:absolute;
left:0px;
bottom:-32px;
background:black;
color:#FFFFFF;
}
.movie
{
position:relative;
width:45%;
left:50px;
top:17%;
height:45%;
}
.lastdance{
position:relative;
left:150px;
top:45%;
}
.zutter{
position:relative;
left:150px;
top:47%;
}
.DoomDaDa{
position:relative;
left:150px;
top:49%;
}
.song{
position:relative;
left:50px;
top:14%;
}
</style>
<script src="Scripts/jquery-1.4.1.min.js"type="text/javascript"></script>
</head>
<body>
<!-- 导航栏 -->
<div class="header">
<ul class="list">
<li><a href="">Top</a></li>
<li><a href="showproductservlet">购买周边</a></li>
<li><a href="member.jsp">迷妹寄语</a></li>
</ul>
</div>
<!--人物介绍-->
<div class="intro">
<img src="./images/intro.png" alt="TOP文字">
<h3>人物介绍:</h3>
<p>
<!--人物信息-->
崔胜铉(艺名T.O.P),1987年11月4日出生于韩国首尔市,韩国男歌手、演员,男子演唱组合BigBang成员之一。
2006年签约韩国YG娱乐公司;同年8月19日以演唱组合BigBang成员身份正式出道 。2007年参演韩国KBS电视台电视剧《我是老师》 。2010年主演战争电影《走进炮火中》。2011年与权志龙组成限定组合"GD&TOP"。2013年推出个人单曲《DOOM DADA》。同年主演的电影《同窗生》。2014年参演电影《老千2》。2016年参演电影《失控·幽灵飞车》。
</p>
<br>
</div>
<!--这是一个轮播图,5秒自动换一张,或者点击下方数字可以换图片-->
<div class="photo2">
<div id="banner">
<div id="banner_bg"></div><!--标题背景-->
<div id="banner_info"></div><!--标题-->
<ul>
<li class="on">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div id="banner_list">
<a href="#" target="_blank"><img src="images/11.jpg" title="TOP" alt="TOP"/></a>
<a href="#" target="_blank"><img src="images/12.jpg" title="TOP" alt="TOP"/></a>
<a href="#" target="_blank"><img src="images/13.jpg" title="TOP" alt="TOP"/></a>
<a href="#" target="_blank"><img src="images/14.jpg" title="TOP" alt="TOP"/></a>
</div>
</div>
<!--这里是鼠标指示在图片上时会显示电影的海报,图片下方会显示电影的简介-->
<script type="text/javascript" src="./Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="./Scripts/JScript.js"></script>
</div>
<div id = "photoShow">
<div class="photo">
<img src="images/19.jpg" alt="我的19岁" />
<span>《我的19岁》是张勇宇执导,许怡才、崔胜贤、李胜贤、申珉熙等主演的悬疑片。影片讲<br>述在汉江中发现了一具女性尸体,有三个同龄的年轻人嫌疑最大。 三人均坚信自己的清白,<br>他们决定联起手来调查案件的真相</li>
</span>
</div>
<div class="photo">
<img src="images/iris.jpg" alt="IRIS" />
<span>《特工IRIS》为2010年韩国谍战题材电影,金奎泰、梁允浩执导,李秉宪、金泰熙<br>等主演。剧情以国家安全局(NSS)为舞台,讲述国家安全局要员与犯罪组织进行周旋、<br>斗争的故事。</li>
</span>
</div>
<div class="photo">
<img src="images/zoujinpaohuozhong.jpg" alt="向着炮火" />
<span>到炮火中去》是由李载汉执导,崔胜贤、车胜元、权相佑、金承佑主演的战争电影。<br>影片根据朝鲜战争期间发生的真实故事改编,讲述了在前线与朝鲜正规军作战的71名学生兵<br>战死沙场的悲壮故事。该片于2010年6月16日在韩国上映</li>
</span>
</div>
<div class ="photo">
<img src="images/tongchuangsheng.jpg" alt = "同窗生" />
<span>《同窗生》是由朴信宇执导,崔胜贤、金裕贞等主演的一部韩国动作片,于2013年11月6日在韩国上映。<br>该片主要讲述了男主人公因妹妹被挟持而被迫成为一名<br>间谍,伪装成高中生潜伏在韩国的一所中学而发生的故事</li>
</span>
</div>
<div class="photo">
<img src="images/laoqian2.jpg" alt="老千2" />
<span>《老千2 - 神之手》是2006年崔东勋执导的影片《老千》的续集,<br>改编自漫画家许英万的同名漫画,是由姜炯哲执导,崔胜贤、申世景、李哈妮主演的<br>韩国犯罪电影。影片讲述了心性像叔叔高尼、从小就展现不同于常人的巧手和好胜心的大吉<br>进入老千世界后,经历多次命悬一线的赌博的故事。该片于2014年9月3日在韩国上映。</li>
</span>
</div>
</div>
<div class="movie">
<h3>我们崔演员:</h3>
<p>
<!--人物信息-->
以Bigbang的Rapper出道的我们TOP不只是会念rap会唱歌哦,在专辑的制作中也会常常和GD一起写歌词,
一开始的TOP并不是以演员身份出道的,而是以Bigbang的rapper身份出道,但再一次mv演出中被杨社长发觉了TOP的演员天分,于是坑蒙拐骗的把TOP带到了《我是老师》的片场中,从此开启了演员的生涯并一发不可收拾,TOP主演的电影《到炮火中去》得到了多方大奖的认证,足以证明我们崔演员的演技实力
看看下面那些我们崔演员获得过的奖项,怎么样,有没有很棒?
<h3>获奖信息:</h3>
<ul>
<li>2010 第31届 韩国青龙电影奖 最佳新人男演员奖 《走进炮火中》
<li>2010 第31届 韩国青龙电影奖 人气奖 《走进炮火中》
<li>2011 第47届 百想艺术大赏 最佳新人男演员
<li>2011 第47届 韩国大钟奖 SONATA潮流人气奖 《走进炮火中》
<li>2010 电影演员赏 STYLE LCON AWARDS 《走进炮火中》
<li>2011 第5届 亚洲电影大奖 最佳新人演员 《走进炮火中》提名
<li>2013 第18届 釜山国际电影节 最佳新人奖 《同窗生》
</ul>
</p>
</div>
<div class="lastdance">
<audio controls="controls" preload="auto" src="song/lastdance.mp3" type="audio/mpeg" />
</div>
<div class="zutter">
<audio controls="controls" preload="auto" src="song/zutter.mp3" type="audio/mpeg" />
</div>
<div class="DoomDaDa">
<audio controls="controls" preload="auto" src="song/doomdada.mp3" type="audio/mpeg" />
</div>
<div class="song">
<p>
<h3>歌曲欣赏</h3>
</p>
</div>
<script type="text/javascript">
$(document).ready(function () {
var imgDivs = $("#photoShow>div");
var imgNums = imgDivs.length; //图片数量
var divWidth = parseInt($("#photoShow").css("width")); //显示宽度
var imgWidth = parseInt($(".photo>img").css("width")); //图片宽度
var minWidth = (divWidth - imgWidth) / (imgNums - 1); //显示其中一张图片时其他图片的显示宽度
var spanHeight = parseInt($("#photoShow>.photo:first>span").css("height")); //图片介绍信息的高度
imgDivs.each(function (i) {
$(imgDivs[i]).css({ "z-index": i, "left": i * (divWidth / imgNums) });
$(imgDivs[i]).hover(function () {
$(this).find("span").stop().animate({ bottom: 0 }, "slow");
imgDivs.each(function (j) {
if (j <= i) {
$(imgDivs[j]).stop().animate({ left: j * minWidth }, "slow");
} else {
$(imgDivs[j]).stop().animate({ left: (j - 1) * minWidth + imgWidth }, "slow");
}
});
}, function () {
imgDivs.each(function (k) {
$(this).find("span").stop().animate({ bottom: -spanHeight }, "slow");
$(imgDivs[k]).stop().animate({ left: k * (divWidth / imgNums) }, "slow");
});
});
});
});
</script>
</body>
</html>
header.jsp
用于提供各种返回到上一级页面,显示在所有jsp页面的上方
<%@page language= "java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<a href = "index.jsp">返回首页>></a><a href ="member.jsp">返回留言板>></a><a href = "showproductservlet">返回周边购买>></a>
footer.jsp
用于提供返回主页和提供喜爱程度测试,对网站的评价,以及作者联系方式,显示在所有jsp页面的下方
<%@page language= "java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<hr>
<p align="center"><font color = "hotpink"> <a href = "question.jsp">TOP喜爱程度测试</a></font></p>
<p align="center"><font color = "hotpink"> <a href = "question2.jsp">TOP粉丝俱乐部问卷调查</a></font></p>
<p align="center"><font color = "hotpink"><a href="index.jsp">返回首页</a></font></p>
<p align="center"><font color = "hotpink">联系作者|<a href = "http://weibo.com/u/5500182421/home">微博</a>|邮箱|2643275194@qq.com</font></p>
注册页:register.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.Cart.com/sample" prefix="demo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>新用户注册</title>
</head>
<body style="background: #ECE3D4">
${sessionScope.message1}
<form action="Registerservlet" method="post" >
<img src="./images/reg.jpg"/>
<table style="margin:-45% 30 0 45%">
<tr><td><h1><font color="red" >结婚申请表</font></h1><br>带星部分必须填写</td></tr>
<tr><td>用户名:</td>
<td><input type="text" name="custname"/> <demo:star/></td>
</tr>
<tr><td> 密码:</td>
<td><input type="password" name="password"/> <demo:star/></td>
</tr>
<tr><td> 地址:</td>
<td><input type="text" name="custaddress"/> <demo:star/></td>
</tr>
<tr><td> 电话:</td>
<td><input type="text" name="custphone"/> <demo:star/></td>
</tr>
<tr><td><input type="submit" value="立即领证"></td>
<td><input type="reset" value="重新填写"></td>
</tr>
</table>
</form>
</body>
</html>
对TOP喜爱程度测试:question1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>测试页面</title>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
</head>
<body style="background: #ECE3D4">
<h1>T.O.P喜爱程度测试</h1> <div id="header"><%@ include file="header.jsp" %></div>
<hr>
<p>请回答下面的问题:</p>
<form action="simpletest.do" method="post">
<p>1.T.O.P的本名是崔胜铉
<input type="radio" name="quest1" value="1">正确
<input type="radio" name="quest1" value="2">错误<br></p>
<p>2.T.O.P的星座是
<input type="radio" name="quest2" value="1">天蝎座
<input type="radio" name="quest2" value="2">摩羯座
<input type="radio" name="quest2" value="3">狮子座<br></p>
<p>3.T.O.P的身高是
<input type="radio" name="quest3" value="1">180
<input type="radio" name="quest3" value="2">181
<input type="radio" name="quest3" value="3">182<br></p>
<p>4.T.O.P这个名字是谁给取的
<input type="radio" name = "quest4" value = "1">se7en
<input type="radio" name = "quest4" value = "2">杨菊花<br></p>
<p>5.TOPTOPTOP是什么梗
<input type="radio"name = "quest5" value = "1">重要的事情说三次
<input type="radio"name = "quest5" value = "2">胜利生日会事件 <br></p>
<p>交卷请点击:<input type="submit" value="交卷">
重答请点击:<input type="reset" value="重答">
</form>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
显示对TOP喜爱的分数页面:score.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
<title>测试结果</title>
</head>
<body style="background: #ECE3D4">
<h1>测试结果</h1>
<div id="header"><%@ include file="header.jsp" %></div>
<hr>
<h2 align="center">你对T.O.P的爱有<font color="hotpink">${score}</font>分~以后也要继续爱T.O.P哦~</h2>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
对粉丝俱乐部的调查问卷页面question2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>T.O.P粉丝俱乐部问卷调查</title>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
</head>
<body style="background: #ECE3D4">
<h1>T.O.P粉丝俱乐部问卷调查</h1> <div id="header"><%@ include file="header.jsp" %></div>
<hr>
<p>请回答下面的问题:</p>
<form action="simpletest2.do" method="post">
<p>1.你喜欢这个T.O.P粉丝俱乐部吗?</p>
<input type="radio" name="quest1" value="1">好喜欢哦~<br>
<input type="radio" name="quest1" value="2">什么鬼,垃圾<br>
<p>2.你觉得页面布局以及设计怎么样?</p>
<input type="radio" name="quest2" value="1">非常棒哦,奖励你一朵小红花<br>
<input type="radio" name="quest2" value="2">还可以吧,还有上升的空间哦<br>
<input type="radio" name="quest2" value="3">丑死了好吧,哪里有设计感了,开玩笑。。。<br>
<p>3.这个粉丝俱乐部能满足你对向T.O.P表白的各方面的需求吗?</p>
<input type="radio" name="quest3" value="1">嗯。满足了我所有的需求哦~<br>
<input type="radio" name="quest3" value="2">一般吧,还有上升的空间哦<br>
<input type="radio" name="quest3" value="3">什么垃圾,基本需求都满足不了好吗?你是来搞笑的吗。。。<br>
<p>4.你愿意对T.O.P粉丝俱乐部提出什么意见或建议吗?</p>
<input type="text" name="quest4" size="30"><br>
<p>我写完了:<input type="submit" value="交卷">
重新填写:<input type="reset" value="重答"><br>
</form>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
对粉丝俱乐部的喜爱程度分数显示页面score2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
<title>测试结果</title>
</head>
<body style="background: #ECE3D4">
<h1>测试结果</h1> <div id="header"><%@ include file="header.jsp" %></div>
<hr>
<h2 align="center">你对T.O.P粉丝俱乐部的喜爱程度是<font color="hotpink">${score}</font>分,我们已经收到了亲的评价,每天会继续努力的哦~</h2>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
购买周边的显示商品页面showproducts.jsp
<%@page language= "java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.*,m.Product,m.Orderitem" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示所有商品信息:</title>
<style type="text/css">
hr{
border-color:#007979;
}
div{
float:left;
margin: 10px;
}
div dd{
margin:0px;
font-size:10pt;
}
div dd.dd_name{
color:blue;
}
div dd.dd_city{
color:#000;
}
</style>
<script type="text/javascript">
function add() {
var num = parseInt(document.getElementById("number").value);
if (num < 100) {
document.getElementById("number").value = ++num;
}
}
//商品数量减少
function sub() {
var num = parseInt(document.getElementById("number").value);
if (num > 1) {
document.getElementById("number").value = --num;
}
}
</script>
</head>
<body style="background: #ECE3D4">
<h1>T.O.P周边</h1>
<a href = "index.jsp">返回首页>></a><a href ="member.jsp">返回留言板>></a><a href = "showproductservlet">返回周边购买>></a><br>
<a href="showorderitem.jsp">查看我的购物车</a>
<hr>
<table align = "center" border="1" >
<%
ArrayList <Product> productlist=(ArrayList<Product>)session.getAttribute("productlist");
for(Product product:productlist){ //遍历Produstlist里面的每一个成员赋值给product,再把product输出,Product是product的类型
%>
<%
int prodid=0;
prodid=product.getProdid();
System.out.println(prodid);
double price=0;
price=product.getPrice();
System.out.println(price);
%>
<div>
<dl>
<dt>
<img src="./images/<%=product.getProdid()%>.jpg" width="300" height="260" border="1"/>
</dt>
<dd class="dd_name"><%=product.getPname() %></dd>
<dd class="dd_city">库存:<%=product.getStock() %> 价格:¥ <%=product.getPrice() %><form action="orderitemservlet1?prodid=<%=prodid %>&price=<%=price %> " method="post">
<!-- <input type="text" name="quantity"/> -->
<form action="orderitemservlet1?prodid=<%=prodid %>&price=<%=price %> " method="post">
<span id="sub" οnclick="sub();">-</span><input type="text" id="number" name="quantity" value="1" size="2" /><span id="add" οnclick="add();">+</span>
<input type="submit" value="提交"/>
</form></dd>
</dl>
</div>
</td>
</tr>
<%} %>
</table>
<%!
ArrayList<Orderitem> orderitemlist1=new ArrayList<Orderitem>();
%>
<%
session.setAttribute("orderitemlist1", orderitemlist1);
%>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
购物车页面:showorderitem.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="Dao.*" %>
<%@page import="m.*" %>
<%@page import="java.util.List" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>我的购物车</title>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
</head>
<body style="background: #ECE3D4">
<h1>购物车</h1> <div id="header"><%@ include file="header.jsp" %></div>
<hr>
<table width="951" border="1" align="center"
bordercolor="#007979" cellpadding="0" cellspacing="0">
<tr><td>商品图</td><td>序号</td><td>商品号</td><td>数量</td><td>价格</td><td></td></tr>
<c:set value="0" var="totalprice"></c:set>
<c:forEach items="${sessionScope.orderitemlist1}" var="item" varStatus="status">
<tbody>
<tr>
<c:set value="${status.count}" var="id"></c:set>
<td width="120" height="90"><img src = "./images/${item.prodid}.jpg"width="120" height="90"/></td>
<td >
${status.count}
</td>
<td >
${item.prodid}
</td>
<td>
${item.quantity}
</td>
<td>
${item.itemprice}
</td>
<td>
<a href="orderitemservlet1?id=${id-1}">删除</a>
</td>
</tr>
<c:set value="${item.itemprice}" var="itemprice"></c:set>
<c:set value="${totalprice+itemprice}" var="totalprice"></c:set>
</tbody>
</c:forEach>
</table>
<form action="orderservlet?totalprice=${totalprice}" method="post">
<table align="center"><tr><td> <a href="orderservlet">清空购物车</a></td></tr><tr><td><input type="submit" value="提交订单"/></td></tr></table>
</form>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
生成订单页面:showorder.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.*,m.*" %>
<%@page import="Dao.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>订单信息:</title>
</head>
<body style="background: #ECE3D4">
<h1>订单详情</h1> <div id="header"><%@ include file="header.jsp" %></div>
<hr>
${sessionScope.message2}<br>
<table width="951" border="1" align="center"
bordercolor="#007979" cellpadding="0" cellspacing="0">
<tr><td>订单号</td><td>日期号</td><td>消费者id</td><td>总价格</td></tr>
<c:forEach items="${sessionScope.orderlist}" var="order" varStatus="status">
<tbody>
<tr>
<td bgcolor=>
${status.count}
</td>
<td bgcolor="">
${order.orderdate}
</td>
<td bgcolor="">
${order.custid}
</td>
<td bgcolor="">
${order.totalprice}
</td>
</tr>
</tbody>
</c:forEach>
</table>
<table align="center"><tr><td><a href="showproducts.jsp">继续购物</a></td></tr></table>
<%
ArrayList <Orderitem> orderitemlist1=(ArrayList<Orderitem>)session.getAttribute("orderitemlist1");
ArrayList <Order> orderlist=(ArrayList<Order>)session.getAttribute("orderlist"); int orderid=orderlist.get(0).getOrderid();
for(Orderitem orderitem:orderitemlist1){
orderitem.setOrderid(orderid);
IOrderitemdao dao=new Orderitemdaoimpl();
dao.addorderitem(orderitem);
}
orderitemlist1.removeAll(orderitemlist1);
session.setAttribute("orderitemlist1", orderitemlist1);
%>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
发表留言页面:member.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import = "java.util.*,m.Message"%>
<html>
<head>
<title>迷妹寄语</title>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
</head>
<body style="background: #ECE3D4">
<h1>迷妹寄语</h1>
<div id="header"><%@ include file="header.jsp" %></div>
<hr>
<font color = red>${result }</font>
<form action = "addMessage.do" method = post>
<h2>有什么悄悄话对TOP说吗?</h2>
<input type = "text" name = "mes">
<input type = "submit" value = "提交">
</form>
<a href = "addMessage.do">查看留言</a>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
查看留言页面:message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import = "java.util.*,m.Message"%>
<html>
<head>
<title>迷妹寄语</title>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
</head>
<body style="background: #ECE3D4">
<audio autoplay="autoplay" src="2.mp3" id="son"> </audio>
<h1>留言板内容</h1><a href = "index.jsp">返回首页>></a><a href ="member.jsp">返回留言板>></a><a href = "showproductservlet">返回周边购买>></a>
<hr>
<table border = "1">
<tr><td>编号</td><td>id</td><td>内容</td><td>时间</td>
<% ArrayList<Message> mesList = (ArrayList<Message>)session.getAttribute("mesList");
for(Message mes:mesList){
%>
<tr>
<td><%= mes.getMesid() %></td>
<td><%= mes.getCustid() %>
<td><%= mes.getMes() %></td>
<td><%= mes.getTime() %></td>
</tr>
<%} %>
</table>
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html> 错误页:wrong.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>错误</title>
<style type="text/css">
hr{
border-color:#007979;
}
</style>
</head>
<body>
出现错误
<div id="footer"><%@ include file="footer.jsp" %></div>
</body>
</html>
在webroot下建一个叫image的文件夹,存放所有需要用到的图片,商品以1.2.3.4这种数字命名,方便循环遍历的时候显示图片,电影图片随意命名。
在webroot下建一个叫song的文件夹,存放所有音乐的MP3格式
在webroot下建一个叫Scripts的文件夹,存放JScript,jquery-1.4.1.min文件。网上随便一搜就有下载的地址,
在src里面建一个叫c的包,存所有的servlet文件
loginservlet:
package c;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import m.Customer;
import Dao.Customerdaoimpl;
import Dao.ICustomerdao;
@WebServlet("/Loginservlet")
public class Loginservlet extends HttpServlet{
private ICustomerdao dao=new Customerdaoimpl();
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
String message1=null;
String custname=request.getParameter("custname");
String password=request.getParameter("password");
Customer customer=new Customer();
customer.setCustname(custname);
customer.setPassword(password);
Customer customer1=dao.viewCustomer(customer);
if(customer1==null){
message1="没有该用户,先注册吧^_^ ";
session.setAttribute("message1", message1);
response.sendRedirect("register.jsp");
}else{
String message0="登陆成功";
session.setAttribute("message0", message0);
session.setAttribute("customerid", customer1.getCustid());
response.sendRedirect("index.jsp");
}
}
}
memberservlet:
package c;
import java.io.*;
import java.util.ArrayList;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import m.Message;
import Dao.MemberDao;
@WebServlet("/addMessage.do")
public class memberServlet extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
MemberDao dao = new MemberDao();
Message message = new Message();
String mes = null;
try{
message.setMes (request.getParameter("mes"));
boolean success = dao.addMessage(message);
if(success){
mes = "<li>TOP会知道你的心情的哦~</li>";
}else{
mes = "<li>插入失败</li>";
}
}catch(Exception e){
mes = "<li>插入失败啦</li>";
e.printStackTrace();
}
request.setAttribute("result", mes);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/member.jsp");
rd.forward(request, response);
}
public void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
ArrayList<Message> mesList = null;
response.setContentType("text/html;charset=utf-8");
MemberDao dao = new MemberDao();
mesList = dao.findAllMes();
if(!mesList.isEmpty()){
request.getSession().setAttribute("mesList", mesList);
response.sendRedirect("/Cart/message.jsp");
}else{
response.sendRedirect("/Cart/wrong.jsp");
}
}
}
orderitemservlet1:
package c;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import m.Orderitem;
@WebServlet("/orderitemservlet1")
public class orderitemservlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
ArrayList<Orderitem> orderitemlist1=(ArrayList<Orderitem>)session.getAttribute("orderitemlist1");
Orderitem orderitem=new Orderitem();
String quantity=request.getParameter("quantity");
String prodid=request.getParameter("prodid");
String price=request.getParameter("price");
int q=Integer.parseInt(quantity);
float p=Float.parseFloat(price);
orderitem.setProdid(Integer.parseInt(prodid));
orderitem.setQuantity(q);
orderitem.setItemprice(p*q);
orderitemlist1.add(orderitem);
session.setAttribute("orderitemlist1", orderitemlist1);
response.sendRedirect("showproducts.jsp");
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
int id=Integer.parseInt(request.getParameter("id"));
ArrayList<Orderitem> orderitemlist1=(ArrayList<Orderitem>)session.getAttribute("orderitemlist1");
orderitemlist1.remove(id);
session.setAttribute("orderitemlist1", orderitemlist1);
response.sendRedirect("showorderitem.jsp");
}
}
orderservlet:
package c;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import m.Order;
import m.Orderitem;
import Dao.IOrderdao;
import Dao.Orderdaoimpl;
@WebServlet("/orderservlet")
public class orderservlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
if(session.getAttribute("customerid")==null){
response.sendRedirect("login.jsp");
}else{
String custid=session.getAttribute("customerid").toString();
String totalprice=request.getParameter("totalprice");
Order order=new Order();
order.setCustid(Integer.parseInt(custid));
order.setTotalprice(Float.parseFloat(totalprice));
IOrderdao dao=new Orderdaoimpl();
if(dao.addorder(order)){
String message2="订单提交成功";
session.setAttribute("message2", message2);
ArrayList<Order> orderlist=new ArrayList<Order>();
orderlist=dao.showorder();
session.setAttribute("orderlist", orderlist);
response.sendRedirect("showorder.jsp");
}else{
response.sendRedirect("wrong.jsp");
}
}
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
ArrayList<Orderitem> orderitemlist1=(ArrayList<Orderitem>)session.getAttribute("orderitemlist1");
boolean flag=false;
flag=orderitemlist1.removeAll(orderitemlist1);
session.setAttribute("orderitemlist1", orderitemlist1);
if(flag){
response.sendRedirect("showproducts.jsp");
}
}
}
registerservlet:
package c;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import m.Customer;
import Dao.Customerdaoimpl;
import Dao.ICustomerdao;
@WebServlet("/Registerservlet")
public class Registerservlet extends HttpServlet{
private static final long serialVersionUID=1L;
private ICustomerdao dao=new Customerdaoimpl();
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html; charset=UTF-8");
String message=null;
HttpSession session=request.getSession();
String custname=request.getParameter("custname");
String password=request.getParameter("password");
String custaddress=request.getParameter("custaddress");
String custphone=request.getParameter("custphone");
if(custname!=null&&password!=null&&custaddress!=null&&custphone!=null){
Customer customer=new Customer();
customer.setPassword(password);
customer.setCustname(custname);
customer.setCustaddress(custaddress);
customer.setCustphone(custphone);
if(dao.addCustomer(customer)){
session.setAttribute("customer", customer);
message="注册成功,请登录";
session.setAttribute("message", message);
response.sendRedirect("login.jsp");
}else{
response.sendRedirect("wrong.jsp");
}
}else{
message="注册失败,重新注册";
session.setAttribute("message", message);
response.sendRedirect("register.jsp");
}
}
}
显示商品的servlet:
showproductservlet:
package c;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import m.Product;
import Dao.IProductdao;
import Dao.Productdaoimpl;
@WebServlet("/showproductservlet")
public class showproductservlet extends HttpServlet {
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=utf-8");
IProductdao dao=new Productdaoimpl();
ArrayList<Product> productlist=dao.showproducts();
if(!productlist.isEmpty()){
request.getSession().setAttribute("productlist", productlist);
response.sendRedirect("showproducts.jsp");
}else{
response.sendRedirect("wrong.jsp");
}
}
}
对TOP喜爱程度的servlet
SimpleTextServlet
package c;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="SimpleTextServlet",urlPatterns={"/simpletest.do"})
public class SimpleTextServlet extends HttpServlet{
private static final long serialVersionUID=1L;
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
String quest1=request.getParameter("quest1");
String quest2=request.getParameter("quest2");
String quest3=request.getParameter("quest3");
String quest4=request.getParameter("quest4");
String quest5=request.getParameter("quest5");
int score=0;
if(quest1!=null&&quest1.equals("1")){
score=score+20;
}
if(quest2!=null&&quest2.equals("1")){
score=score+20;
}
if(quest3!=null&&quest3.equals("2")){
score=score+20;
}
if(quest4!=null&&quest4.equals("1")){
score=score+20;
}
if(quest5!=null&&quest5.equals("2")){
score=score+20;
}
request.setAttribute("score", score);
RequestDispatcher rd=request.getRequestDispatcher("/score.jsp");
rd.forward(request, response);
}
}
对网站的喜爱程度的调查问卷的servlet
simpleTextServlet2:
package c;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="SimpleTextServlet2",urlPatterns={"/simpletest2.do"})
public class SimpleTextServlet2 extends HttpServlet{
private static final long serialVersionUID=1L;
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
String quest1=request.getParameter("quest1");
String quest2=request.getParameter("quest2");
String quest3=request.getParameter("quest3");
String quest4=request.getParameter("quest4").trim();
int score=0;
if(quest1!=null&&quest1.equals("1")){
score=score+25;
}
if(quest1!=null&&quest1.equals("2")){
score=score-10;
}
if(quest2!=null&&quest2.equals("1")){
score=score+25;
}
if(quest2!=null&&quest2.equals("2")){
score=score+15;
}
if(quest2!=null&&quest2.equals("3")){
score=score-10;
}
if(quest3!=null&&quest3.equals("3")){
score=score-10;
}
if(quest3!=null&&quest3.equals("1")){
score=score+25;
}
if(quest3!=null&&quest3.equals("2")){
score=score+15;
}
if(quest4!=null&&quest4.equals("HttpServlet")||quest4.equals("javax.servlet.http.HttpServlet")){
score=score+10;
}
request.setAttribute("score", score);
RequestDispatcher rd=request.getRequestDispatcher("/score2.jsp");
rd.forward(request, response);
}
}
在src里建一个叫Dao的包,存所有的Dao文件
BaseDao:
package Dao;
import javax.sql.DataSource;
import java.sql.*;
import javax.naming.*;
public class BaseDao {
DataSource dataSource;
public BaseDao(){
try{
Context context = new InitialContext();
dataSource = (DataSource)context.lookup("java:comp/env/jdbc/mysql");
}
catch(NamingException ne){
System.out.println("Exception:"+ne);
}
}
public Connection getConnection()throws Exception{
return dataSource.getConnection();
}
protected static final String DELETE_SQL = "DELETE FROM customer WHERE custName = ?";
protected static final String UPDATE_SQL = "UPDATE customer SET email=?,balance=? WHERE custName=?";
}
Customerdaoimpl:
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import m.Customer;
import m.Product;
import DB.DB;
public class Customerdaoimpl implements ICustomerdao{
private static Connection conn=DB.getConnection();
public boolean addCustomer(Customer customer){
String sql="insert into customer (custname,password,custaddress,custphone) "
+ "values(?,?,?,?)";
PreparedStatement pstmt=null;
boolean flag=false;
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, customer.getCustname());
pstmt.setString(2, customer.getPassword());
pstmt.setString(3, customer.getCustaddress());
pstmt.setString(4, customer.getCustphone());
flag=pstmt.executeUpdate()>0?true:false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
public Customer viewCustomer(Customer customer){
String sql="select * from customer where custname=? and password=?";
PreparedStatement pstmt=null;
ResultSet rs=null;
Customer a=null;
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, customer.getCustname());
pstmt.setString(2, customer.getPassword());
rs=pstmt.executeQuery();
while(rs.next()){
a=new Customer();
a.setCustid(rs.getInt("custid"));
a.setCustname(rs.getString("custname"));
a.setPassword(rs.getString("password"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return a;
}
public Product getProductsById(int id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DB.getConnection();
String sql = "select * from items where id=?;"; // SQL查询语句
stmt = conn.prepareStatement(sql);
//把id的值赋给SQL查询语句中第一个问号
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
Product product = new Product();
product.setPname(rs.getString("pname"));
product.setPrice(rs.getInt("price"));
product.setPic(rs.getString("pic"));
product.setStock(rs.getInt("stock"));
product.setProdid(rs.getInt("id"));
return product;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
// 释放数据集对象
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception e) {
e.printStackTrace();
}
}
// 释放语句对象
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
ICustomerdao:
package Dao;
import m.Customer;
public interface ICustomerdao {
public boolean addCustomer(Customer customer);
public Customer viewCustomer(Customer customer);
}
IOrderdao
package Dao;
import java.util.ArrayList;
import m.Order;
public interface IOrderdao {
public boolean addorder(Order order);
public ArrayList<Order> showorder();
}
IOrderitemdao
package Dao;
import java.util.ArrayList;
import m.Orderitem;
public interface IOrderitemdao {
public boolean addorderitem(Orderitem orderitem);
public ArrayList<Orderitem> showorderitem();
public boolean deleteorderitem(int orderitem);
public boolean delallorderitem();
}
IProductdao
package Dao;
import java.util.ArrayList;
import m.Product;
public interface IProductdao {
public ArrayList<Product> showproducts();
}
MemberDao
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import m.Message;
public class MemberDao extends BaseDao{
public boolean addMessage(Message message) {
// TODO Auto-generated method stub
boolean flag = false;
String sql = "INSERT INTO message "+"(custid,mes,time)VALUES(?,?,now())";
try{
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, message.getCustid());
pstmt.setString(2, message.getMes());
flag=pstmt.executeUpdate()>0?true:false;
}
catch(SQLException se){
se.printStackTrace();
}
return flag;
}
public ArrayList<Message> findAllMes(){
Message message = new Message();
ArrayList<Message> mesList = new ArrayList<Message>();
String sql = "SELECT * FROM message";
try(Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rst = pstmt.executeQuery()){
while(rst.next()){
Message m = new Message();
m.setCustid(rst.getInt("custid"));
m.setMesid(rst.getInt("mesid"));
m.setMes(rst.getString("mes"));
m.setTime(rst.getString("time"));
mesList.add(m);
}
return mesList;
}
catch(SQLException e){
e.printStackTrace();
return null;
}
}
public boolean delMessage(int id){
String sql = "delete from message where mesid=?";
PreparedStatement pstmt = null;
boolean flag = false;
try{
Connection conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
flag=pstmt.executeUpdate()>0?true:false;
}catch(Exception e)
{
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
}
orderitemdaoimpl
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import m.Order;
import DB.DB;
public class Orderdaoimpl implements IOrderdao {
private static Connection conn=DB.getConnection();
@Override
public boolean addorder(Order order) {
// TODO Auto-generated method stub
String sql="insert into orders (orderdate,custid,totalprice) "
+ "values (now(),?,?)";
PreparedStatement pstmt=null;
boolean flag=false;
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, order.getCustid());
pstmt.setFloat(2, order.getTotalprice());
flag=pstmt.executeUpdate()>0?true:false;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return flag;
}
@Override
public ArrayList<Order> showorder() {
// TODO Auto-generated method stub
ArrayList<Order> orderlist=new ArrayList<Order>();
try {
String sql="select * from orders order by orderid desc";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet result=pstmt.executeQuery();
while(result.next()){
Order order=new Order();
order.setOrderid(Integer.parseInt(result.getString("orderid")));
order.setOrderdate(result.getString("orderdate"));
order.setCustid(Integer.parseInt(result.getString("custid")));
order.setTotalprice(Float.parseFloat(result.getString("totalprice")));
orderlist.add(order);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return orderlist;
}
public ArrayList<Integer> showorderid(){
ArrayList<Integer> orderidlist=new ArrayList<Integer>();
try {
String sql="select orderid from orders order by orderid desc";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet result=pstmt.executeQuery();
while(result.next()){
orderidlist.add(Integer.parseInt(result.getString("orderid")));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return orderidlist;
}
}
orderitemdaoimpl
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import m.Orderitem;
import DB.DB;
public class Orderitemdaoimpl implements IOrderitemdao {
private static Connection conn=DB.getConnection();
@Override
public boolean addorderitem(Orderitem orderitem) {
// TODO Auto-generated method stub
ArrayList<Orderitem> orderitemlist=new ArrayList<Orderitem>();
String sql="insert into orderitems (orderid,prodid,quantity,itemprice) "
+ "values (?,?,?,?)";
PreparedStatement pstmt=null;
boolean flag=false;
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, orderitem.getOrderid());
pstmt.setInt(2, orderitem.getProdid());
pstmt.setInt(3, orderitem.getQuantity());
pstmt.setFloat(4, orderitem.getItemprice());
flag=pstmt.executeUpdate()>0?true:false;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return flag;
}
@Override
public ArrayList<Orderitem> showorderitem() {
// TODO Auto-generated method stub
ArrayList<Orderitem> orderitemlist=new ArrayList<Orderitem>();
try {
String sql="select * from orderitems";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet result=pstmt.executeQuery();
while(result.next()){
Orderitem orderitem=new Orderitem();
orderitem.setOrderitem(Integer.parseInt(result.getString("orderitem")));
orderitem.setProdid(Integer.parseInt(result.getString("prodid")));
orderitem.setQuantity(Integer.parseInt(result.getString("quantity")));
orderitem.setItemprice(Float.parseFloat(result.getString("itemprice")));
orderitemlist.add(orderitem);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return orderitemlist;
}
@Override
public boolean deleteorderitem(int orderitem) {
// TODO Auto-generated method stub
String sql="delete from orderitems where orderitem=?";
PreparedStatement pstmt=null;
boolean flag=false;
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,orderitem);
flag=pstmt.executeUpdate()>0?true:false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
@Override
public boolean delallorderitem() {
// TODO Auto-generated method stub
String sql="delete from orderitems";
PreparedStatement pstmt=null;
boolean flag=false;
try {
pstmt=conn.prepareStatement(sql);
flag=pstmt.executeUpdate()>0?true:false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
}
Productdaoimpl
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import m.Product;
import DB.DB;
public class Productdaoimpl implements IProductdao {
@Override
public ArrayList<Product> showproducts() {
// TODO Auto-generated method stub
ArrayList<Product> productlist=null;
productlist=new ArrayList<Product>();
try {
String sql="select * from products";
Connection dbconn=DB.getConnection();
PreparedStatement pstmt=dbconn.prepareStatement(sql);
ResultSet result=pstmt.executeQuery();
while(result.next()){
Product product=new Product();
product.setProdid(Integer.parseInt(result.getString("prodid")));
product.setPname(result.getString("pname"));
product.setPrice(Double.parseDouble(result.getString("price")));
product.setStock(Integer.parseInt(result.getString("stock")));
productlist.add(product);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return productlist;
}
}
建一个叫DB的包在src里面,在DB包里建一个叫DB的Java文件
package DB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DB{
private static DataSource dataSource;
private static Connection conn;
public static Connection getConnection(){
try {
Context context=new InitialContext();
dataSource=(DataSource)context.lookup("java:comp/env/jdbc/mysql");
conn=dataSource.getConnection();
} catch (NamingException e) {
// TODO: handle exception
System.out.println("Exception"+e);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return conn;
}
/*
* private static Connection conn;
public static Connection getConnection(){
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection("jdbc:mysql://172.20.10.8:3306/lv?useUnicode=true&characterEncoding=utf-8", "root", "root");
//数据库字符集问题。
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public static void close(){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
}
建一个叫Filter的包在src里面,在Filter的里面建一个叫EncodingFilter的Java文件
package Filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
@WebFilter(filterName = "EncodingFilter",urlPatterns = "*",
initParams = @WebInitParam(name="encoder",value="utf-8"))
public class EncodingFilter implements Filter {
private String encoder="";
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
req.setCharacterEncoding(encoder);
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
encoder=config.getInitParameter("encoder");
}
}
建一个叫m的包,里面存所有封装的类文件
Customer:
package m;
public class Customer {
private int custid;
private String custname;
private String password;
private String custaddress;
private String custphone;
public int getCustid() {
return custid;
}
public void setCustid(int custid) {
this.custid = custid;
}
public String getCustname() {
return custname;
}
public void setCustname(String custname) {
this.custname = custname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCustaddress() {
return custaddress;
}
public void setCustaddress(String custaddress) {
this.custaddress = custaddress;
}
public String getCustphone() {
return custphone;
}
public void setCustphone(String custphone) {
this.custphone = custphone;
}
public Customer(String custname, String password) {
super();
this.custname = custname;
this.password = password;
}
public Customer(int custid, String custname, String password) {
super();
this.custid = custid;
this.custname = custname;
this.password = password;
}
public Customer(String custname, String password, String custaddress,
String custphone) {
super();
this.custname = custname;
this.password = password;
this.custaddress = custaddress;
this.custphone = custphone;
}
public Customer(){
}
}
Message:
package m;
public class Message {
private int mesid;
private String mes;
private int custid;
private String time;
private Customer cust;
public Customer getCust() {
return cust;
}
public void setCust(Customer cust) {
this.cust = cust;
}
public Message(){}
public Message(int mesid,String mes,int custid){
super();
this.mesid = mesid;
this.mes = mes;
this.custid = custid;
}
public int getCustid() {
return custid;
}
public void setCustid(int custid) {
this.custid = custid;
}
public int getMesid() {
return mesid;
}
public void setMesid(int mesid) {
this.mesid = mesid;
}
public String getMes() {
return mes;
}
public void setMes(String mes) {
this.mes = mes;
}
public String getTime() {
// TODO Auto-generated method stub
return time;
}
public void setTime(String time){
this.time = time;
}
}
Order:
package m;
import java.io.Serializable;
public class Order implements Serializable {
private int orderid;
private String orderdate;
private int custid;
private float totalprice;
public Order(int orderid, String orderdate, int custid, float totalprice) {
super();
this.orderid = orderid;
this.orderdate = orderdate;
this.custid = custid;
this.totalprice = totalprice;
}
public Order(){
}
public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public String getOrderdate() {
return orderdate;
}
public void setOrderdate(String orderdate) {
this.orderdate = orderdate;
}
public int getCustid() {
return custid;
}
public void setCustid(int custid) {
this.custid = custid;
}
public float getTotalprice() {
return totalprice;
}
public void setTotalprice(float totalprice) {
this.totalprice = totalprice;
}
}
Orderitem:
package m;
import java.io.Serializable;
public class Orderitem implements Serializable {
private int orderid;
private int orderitem;
private int prodid;
private int quantity;
private float itemprice;
public Orderitem(){
}
public Orderitem(int orderitem, int prodid, int quantity, float itemprice) {
super();
this.orderitem = orderitem;
this.prodid = prodid;
this.quantity = quantity;
this.itemprice = itemprice;
}
public Orderitem(int orderid, int orderitem, int prodid, int quantity,
float itemprice) {
super();
this.orderid = orderid;
this.orderitem = orderitem;
this.prodid = prodid;
this.quantity = quantity;
this.itemprice = itemprice;
}
public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public int getOrderitem() {
return orderitem;
}
public void setOrderitem(int orderitem) {
this.orderitem = orderitem;
}
public int getProdid() {
return prodid;
}
public void setProdid(int prodid) {
this.prodid = prodid;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getItemprice() {
return itemprice;
}
public void setItemprice(float itemprice) {
this.itemprice = itemprice;
}
}
Product
package m;
import java.io.Serializable;
public class Product implements Serializable{
private int prodid;
private String pname;
private double price;
private int stock;
private String pic;
public Product(int prodid, String pname, double price, int stock) {
super();
this.prodid = prodid;
this.pname = pname;
this.price = price;
this.stock = stock;
}
public Product(){
}
public int getProdid() {
return prodid;
}
public void setProdid(int prodid) {
this.prodid = prodid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getPic(){
return pic;
}
public void setPic(String pic){
this.pic = pic;
}
}
自定义标签RedStarTag:
package m;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class RedStarTag extends SimpleTagSupport{
public void doTag()throws JspException,IOException{
JspWriter out = getJspContext().getOut();
out.print("<font color = '#color=#FF0000'>*</font>");
}
}