经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
大项目之网上书城(五)——主页(End)
来源:cnblogs  作者:return_false  时间:2019/6/19 8:56:08  对本文有异议

大项目之网上书城(五)——主页(End)

不说了!日更啊!

主要改动

今天终于把主页完成啦!给书加了图片,虽然图片严重不符,不够这都是小问题,现在还是测试阶段,到时候整理下数据库就行了。

同时终于加了登录页面,并把验证码移到了登录界面,注册页面就不需要了。

最后,加了个用户页面,不过目前只有退出账号功能233333

1.主页(终于完成啦)

完整代码

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <style>
  7. li{
  8. float:left;
  9. width:100%;
  10. height:10%;
  11. font-size:16px;
  12. color:#8deeee;
  13. }
  14. </style>
  15. <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-3.3.1/jquery-3.3.1.min.js"></script>
  16. <script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
  17. <script type="text/javascript" src="${pageContext.request.contextPath}/client/js/index.js"></script>
  18. <title>主页</title>
  19. </head>
  20. <body style="background-color:#bbb">
  21. <!-- 调用头部页面 -->
  22. <div style="width:100%;height:100px;float:left">
  23. <jsp:include page="/client/head.jsp"></jsp:include>
  24. </div>
  25. <!-- 通用内容体大小 -->
  26. <div style="width:70%;height:886px;float:left;margin-left:15%;">
  27. <!-- 二级导航 -->
  28. <jsp:include page="/client/head2.jsp"></jsp:include>
  29. <!-- 图书分类and轮播图and文案and热门推荐and新书上架and新书榜 -->
  30. <div style="width:100%;height:800px;float:left;margin-top:2%;background-color:#bbb;">
  31. <!-- 图书分类 -->
  32. <div style="width:23%;height:100%;float:left;background-color:#a8f;">
  33. <div style="width:100%;height:6%;text-align:center;line-height:45px;background-color:#556B2F">
  34. <font color="#ddd" style="font-size:20px;">图书分类</font>
  35. </div>
  36. <div style="width:100%;height:94%;text-align:center;line-height:45px;background-color:#548B54">
  37. <ul style="width:100%;height:100%;text-align:left;">
  38. <li>
  39. <a href="${pageContext.request.contextPath }/client/pai/index.jsp">好书拍卖</a>
  40. </li>
  41. <li>
  42. <a href="${pageContext.request.contextPath }/client/net_literature/index.jsp">网络文学</a>
  43. </li>
  44. <li>
  45. <a href="${pageContext.request.contextPath }/client/clothing/index.jsp">服装</a>
  46. </li>
  47. <li>
  48. <a href="${pageContext.request.contextPath }/client/sport/index.jsp">运动户外</a>
  49. </li>
  50. <li>
  51. <a href="${pageContext.request.contextPath }/client/nursery/index.jsp">孕婴童</a>
  52. </li>
  53. <li>
  54. <a href="${pageContext.request.contextPath }/client/nursery/index.jsp">孕婴童</a>
  55. </li>
  56. <li>
  57. <a href="${pageContext.request.contextPath }/client/food/index.jsp">食品</a>
  58. </li>
  59. <li>
  60. <a href="#">暂无分类</a>
  61. </li>
  62. <li>
  63. <a href="#">暂无分类</a>
  64. </li>
  65. <li>
  66. <a href="#">暂无分类</a>
  67. </li>
  68. </ul>
  69. </div>
  70. </div>
  71. <!-- 轮播图 -->
  72. <div style="width:50%;height:35%;float:left;margin-left:2%;background-size:100% 100%;"id="lun">
  73. <!-- table按钮沉底大法! -->
  74. <table style="width:100%;height:100%">
  75. <tr>
  76. <td style="vertical-align:bottom;">
  77. <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:60%;" id="l1">1</button>
  78. <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:2%;" id="l2">2</button>
  79. <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:2%;" id="l3">3</button>
  80. <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:2%;" id="l4">4</button>
  81. </td>
  82. </tr>
  83. </table>
  84. </div>
  85. <!-- 文案and热门推荐 -->
  86. <div style="width:23%;height:35%;float:left;background-color:#bbb;margin-left:2%">
  87. <!-- 文案 -->
  88. <div style="width:100%;height:30%;float:left;background-color:#548B54;">
  89. <font style="font-size:20px;text-align:center;display:block;width:100%;color:#ee4000">618年中狂欢</font>
  90. <font style="display:block;color:#eead0e">十万童书,每满100减50</font>
  91. <font style="display:block;color:#eead0e">艺术绘画,每满100减50</font>
  92. </div>
  93. <!-- 热门推荐 -->
  94. <div style="width:100%;height:64%;float:left;background-color:#a8f;margin-top:8%">
  95. <div style="width:100%;height:25%;text-align:center;line-height:45px;background-color:#556B2F">
  96. <font color="#ddd" style="font-size:20px;">热门推荐</font>
  97. </div>
  98. <div style="width:100%;height:75%;text-align:center;line-height:45px;background-color:#548B54;margin-top:-5%;">
  99. <ul>
  100. <li style="text-align:left;color:black"><font id="a1"></font><font id="remen1" style="font-size:8px"></font></li>
  101. <li style="text-align:left;color:black"><font id="a2"></font><font id="remen2" style="font-size:8px"></font></li>
  102. </ul>
  103. <div style="width:100%;height:20%;float:left;margin-top:5%">
  104. <button class="btn btn-info" style="float:left;margin-left:20%;font-size:8px;"id="b1">1</button>
  105. <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b2">2</button>
  106. <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b3">3</button>
  107. <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b4">4</button>
  108. </div>
  109. </div>
  110. </div>
  111. </div>
  112. <!-- 新书上架 -->
  113. <div style="width:50%;height:63%;float:left;background-color:#a8f;margin-left:2%;margin-top:2%;"id="xinShu">
  114. <table border="1"style="width:100%;height:100%">
  115. <tr>
  116. <td style="width:33%">
  117. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  118. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=1" style="width:100%;"/>
  119. </div>
  120. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  121. <font style="font-size:16px;margin-left:3%;"id="shu1">图书</font>
  122. <font style="font-size:8px"id="re1"></font>
  123. </div>
  124. </td>
  125. <td style="width:33%">
  126. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  127. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=2" style="width:100%;"/>
  128. </div>
  129. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  130. <font style="font-size:16px;margin-left:3%;"id="shu2">图书</font>
  131. <font style="font-size:8px"id="re2"></font>
  132. </div>
  133. </td>
  134. <td style="width:33%">
  135. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  136. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=3" style="width:100%;"/>
  137. </div>
  138. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  139. <font style="font-size:16px;margin-left:3%;"id="shu3">图书</font>
  140. <font style="font-size:8px"id="re3"></font>
  141. </div>
  142. </td>
  143. </tr>
  144. <tr>
  145. <td style="width:33%">
  146. <div style="width:94%;height:80%;background-color:white;float:left;margin-left:3%">
  147. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=4" style="width:100%;"/>
  148. </div>
  149. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  150. <font style="font-size:16px;margin-left:3%;"id="shu4">图书</font>
  151. <font style="font-size:8px"id="re4"></font>
  152. </div>
  153. </td>
  154. <td style="width:33%">
  155. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  156. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=5" style="width:100%;"/>
  157. </div>
  158. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  159. <font style="font-size:16px;margin-left:3%;"id="shu5">图书</font>
  160. <font style="font-size:8px"id="re5"></font>
  161. </div>
  162. </td>
  163. <td style="width:33%">
  164. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  165. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=6" style="width:100%;"/>
  166. </div>
  167. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  168. <font style="font-size:16px;margin-left:3%;"id="shu6">图书</font>
  169. <font style="font-size:8px"id="re6"></font>
  170. </div>
  171. </td>
  172. </tr>
  173. <tr>
  174. <td style="width:33%">
  175. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  176. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=7" style="width:100%;"/>
  177. </div>
  178. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  179. <font style="font-size:16px;margin-left:3%;"id="shu7">图书</font>
  180. <font style="font-size:8px"id="re7"></font>
  181. </div>
  182. </td>
  183. <td style="width:33%">
  184. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  185. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=8" style="width:100%;"/>
  186. </div>
  187. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  188. <font style="font-size:16px;margin-left:3%;"id="shu8">图书</font>
  189. <font style="font-size:8px"id="re8"></font>
  190. </div>
  191. </td>
  192. <td style="width:33%">
  193. <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
  194. <img alt="图书" src="${pageContext.request.contextPath}/XinShuImg?shu=9" style="width:100%;"/>
  195. </div>
  196. <div style="width:94%;float:left;margin-left:3%;height:15%;">
  197. <font style="font-size:16px;margin-left:3%;"id="shu9">图书</font>
  198. <font style="font-size:8px"id="re9"></font>
  199. </div>
  200. </td>
  201. </tr>
  202. </table>
  203. </div>
  204. <!-- 新书榜 -->
  205. <div style="width:23%;height:63%;float:left;background-color:#a8f;margin-left:2%;margin-top:2%;">
  206. <div style="width:100%;height:10%;text-align:center;line-height:45px;background-color:#556B2F">
  207. <font color="#ddd" style="font-size:20px;">新书排行榜</font>
  208. </div>
  209. <div style="width:100%;height:90%;text-align:center;line-height:45px;background-color:#548B54;">
  210. <ul style="width:100%;height:90%;float:left;">
  211. <li style="text-align:left;color:black"><font id="x1"></font><font id="r1" style="font-size:8px"></font></li>
  212. <li style="text-align:left;color:black"><font id="x2"></font><font id="r2" style="font-size:8px"></font></li>
  213. <li style="text-align:left;color:black"><font id="x3"></font><font id="r3" style="font-size:8px"></font></li>
  214. <li style="text-align:left;color:black"><font id="x4"></font><font id="r4" style="font-size:8px"></font></li>
  215. <li style="text-align:left;color:black"><font id="x5"></font><font id="r5" style="font-size:8px"></font></li>
  216. <li style="text-align:left;color:black"><font id="x6"></font><font id="r6" style="font-size:8px"></font></li>
  217. <li style="text-align:left;color:black"><font id="x7"></font><font id="r7" style="font-size:8px"></font></li>
  218. <li style="text-align:left;color:black"><font id="x8"></font><font id="r8" style="font-size:8px"></font></li>
  219. <li style="text-align:left;color:black"><font id="x9"></font><font id="r9" style="font-size:8px"></font></li>
  220. </ul>
  221. <div style="width:100%;height:10%;float:left;">
  222. <button class="btn btn-info" style="float:left;margin-left:20%;font-size:8px;"id="b5">1</button>
  223. <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b6">2</button>
  224. <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b7">3</button>
  225. <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b8">4</button>
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. </div>
  231. <!-- 调用底部页面 -->
  232. <div style="width:100%;height:60px;float:left">
  233. <jsp:include page="/client/foot.jsp"></jsp:include>
  234. </div>
  235. </body>
  236. </html>

效果图

2.head.jsp的小改动

给我的账号加了个判断,如果已经登录了,就显示为你的用户名,可跳转到你的个人主页。否则显示“我的账号”,可跳转到登录界面。恩,悄悄给BookStore字样做了个到主页的跳转~

代码

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <title>Insert title here</title>
  8. <!-- 这里的href请写自己的bootstrap的css的链接。如果没有下载,可以用这个 -->
  9. <!-- https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css -->
  10. <link rel="stylesheet" href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css">
  11. <style type="text/css">
  12. @font-face{
  13. font-family: myFont;
  14. src: url("${pageContext.request.contextPath }/bootstrap-3.3.7-dist/fonts/Adventure-Normal.ttf");
  15. }
  16. a:link {text-decoration:none;color: black} /* 未访问的链接 */
  17. a:visited {text-decoration:none;color: black} /* 已访问的链接 */
  18. a:hover {text-decoration:none;color: #068} /* 鼠标移动到链接上 */
  19. a:active {text-decoration:none;color: #068} /* 选定的链接,即鼠标按下去的时候不松开显示的状态 */
  20. a{
  21. font-size:18px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div style="height:100px;width:100%;float:left">
  27. <!-- BookStore字样 -->
  28. <div style="line-height:120px;height:100px;width:45%;margin-left:15%;float:left">
  29. <a href="${pageContext.request.contextPath }/client/index.jsp"><font face="myFont" color = "black" style="font-size:60px">BookStore</font></a>
  30. </div>
  31. <!-- 导航栏 -->
  32. <div style="height:100px;width:25%;float:left;margin-right:15%">
  33. <div style="height:100px;width:25%;float:left;padding-top:40px">
  34. <a href="#"><span class="glyphicon glyphicon-shopping-cart"></span>购物车</a>
  35. </div>
  36. <div style="height:100px;width:25%;float:left;padding-top:40px">
  37. <a href="#">帮助中心</a>
  38. </div>
  39. <div style="height:100px;width:25%;float:left;padding-top:40px">
  40. <c:if test="${sessionScope.user==null}" var="f">
  41. <a href="${pageContext.request.contextPath }/client/login.jsp">我的账户</a>
  42. </c:if>
  43. <c:if test="${!f }">
  44. <a href="${pageContext.request.contextPath }/client/user.jsp">${sessionScope.user.username }</a>
  45. </c:if>
  46. </div>
  47. <div style="height:100px;width:25%;float:left;padding-top:40px">
  48. <a href="${pageContext.request.contextPath }/client/register.jsp">用户注册</a>
  49. </div>
  50. </div>
  51. </div>
  52. </body>
  53. </html>

3.login.jsp

###代码

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <title>登录</title>
  7. <!-- 这里的href请写自己的bootstrap的css的链接。如果没有下载,可以用这个 -->
  8. <!-- https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css -->
  9. <link rel="stylesheet" href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css">
  10. <!-- jquery和bootstrap的链接可以用下面的 -->
  11. <!-- https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js -->
  12. <!-- https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js -->
  13. <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-3.3.1/jquery-3.3.1.min.js"></script>
  14. <script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
  15. <!-- login.js是我自己写的,里面放了判断输入是否合理,以及验证码的正确与否的。之后放上了。 -->
  16. <script type="text/javascript" src="${pageContext.request.contextPath}/client/js/login.js"></script>
  17. </head>
  18. <body style="background-color:#bbb">
  19. <font size="7" style="color:red;">${errorMsg }</font>
  20. <!-- 调用头部页面 -->
  21. <div style="width:100%;height:100px;float:left">
  22. <jsp:include page="head.jsp"></jsp:include>
  23. </div>
  24. <!-- 通用内容体大小 -->
  25. <div style="width:70%;height:720px;float:left;margin-left:15%;">
  26. <!-- 好看的图 -->
  27. <div style="width:55%;height:100%;float:left;margin-top:10%;">
  28. <img alt="拿书男孩" src="${pageContext.request.contextPath }/client/img/boy.jpg" style="width:90%;">
  29. </div>
  30. <!-- 登录界面 -->
  31. <div style="width:45%;height:80%;float:left;margin-top:7%">
  32. <h1 style="color:#8b6914;text-align:center">用户登录</h1>
  33. <hr style="height:2px;border:none;border-top:5px ridge green;" />
  34. <form action="${pageContext.request.contextPath }/Login" method="post" class="form-horizontal" role="form">
  35. <div class="form-group">
  36. <label for="lastname" class="col-sm-3 control-label input-lg">昵称</label>
  37. <div class="col-sm-9">
  38. <input type="text" name="username" id="username" class="form-control input-lg"
  39. placeholder="请输入昵称" style="float:left"/>
  40. </div>
  41. </div>
  42. <div class="form-group">
  43. <label for="firstname" class="col-sm-3 control-label input-lg">密码</label>
  44. <div class="col-sm-9">
  45. <input type="password" name="password" id="password"
  46. class="form-control input-lg" placeholder="请输入密码" style="float:left"/>
  47. </div>
  48. </div>
  49. <div class="form-group">
  50. <label for="firstname" class="col-sm-3 control-label input-lg">验证码</label>
  51. <div class="col-sm-9">
  52. <input type="text" id="code"
  53. class="form-control input-lg" placeholder="请输入验证码" style="width:45%;float:left"/>
  54. <img src="${pageContext.request.contextPath}/imageCode"
  55. width="180"height="30"class="textinput"
  56. style="height:42px;width:55%;"id="img"/>
  57. <a href="javascript:void(0);"id="a1" >换一张</a>
  58. </div>
  59. </div>
  60. <div class="form-group">
  61. <label for="firstname" class="col-sm-1 control-label input-lg"></label>
  62. <div class="col-sm-5">
  63. <input type="submit" name="submit" value="提交"
  64. class="form-control input-lg btn btn-primary"style="width:100%;float:left"/>
  65. </div>
  66. <div class="col-sm-5">
  67. <input type="reset" name="reset" value="重置" id="re"
  68. class="form-control input-lg btn btn-warning"style="width:100%;float:left"/>
  69. </div>
  70. </div>
  71. </form>
  72. <span style="margin-left:10%">还没有帐号?请先<a href="${pageContext.request.contextPath }/client/register.jsp">注册</a></span>
  73. </div>
  74. </div>
  75. <!-- 调用底部页面 -->
  76. <div style="width:100%;height:60px;float:left">
  77. <jsp:include page="foot.jsp"></jsp:include>
  78. </div>
  79. </body>
  80. </html>

效果图

4.login.js

代码

因为貌似jquery的post提交是在其他代码结束之后才运行的,于是我取巧把二维码的验证和表单提交分开写了,个人认为也算是个不错的思路吧~

  1. $(function(){
  2. var code;
  3. $.post("../CheckCode",function(data){
  4. code=data;
  5. });
  6. $("#a1").click(function(){
  7. $("#img").attr("src","/bookstore/imageCode?time="+new Date().getTime());
  8. });
  9. var flag;
  10. $("#code").keyup(function(){
  11. $.post("/bookstore/CheckCode",function(data){
  12. code=data;
  13. if(code==$("#code").val()){
  14. flag=true;
  15. }
  16. else{
  17. flag=false;
  18. }
  19. });
  20. });
  21. $("form").submit(function(){
  22. if(flag){
  23. return true;
  24. }else{
  25. alert("验证码错误");
  26. return false;
  27. }
  28. });
  29. });

5.userServiceImpl的login方法

代码

个人感觉设计的还是挺精妙的,可以用一个函数就检测用户名是否存在,密码是否正确,同时也能在正确的时候返回user对象。

  1. @Override
  2. public Object login(String username, String password) throws SQLException {
  3. // TODO Auto-generated method stub
  4. User user = dao.findUserByName(username);
  5. if(user==null) {
  6. return "user";
  7. }else {
  8. if(user.getPassword().equals(password)) {
  9. return user;
  10. }else {
  11. return "pass";
  12. }
  13. }
  14. }

6.LoginServlet

代码

接收用户名密码,然后调用userService的login方法,并对返回值做出相应处理。

  1. package cn.edu.bdu.mc.servlets;
  2. import java.io.IOException;
  3. import java.sql.SQLException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import cn.edu.bdu.mc.beans.User;
  10. import cn.edu.bdu.mc.services.UserService;
  11. import cn.edu.bdu.mc.services.impls.UserServiceImpl;
  12. /**
  13. * Servlet implementation class LoginServlet
  14. */
  15. @WebServlet("/Login")
  16. public class LoginServlet extends HttpServlet {
  17. private static final long serialVersionUID = 1L;
  18. /**
  19. * @see HttpServlet#HttpServlet()
  20. */
  21. public LoginServlet() {
  22. super();
  23. // TODO Auto-generated constructor stub
  24. }
  25. /**
  26. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  27. */
  28. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. try {
  30. // TODO Auto-generated method stub
  31. String username = request.getParameter("username");
  32. String password = request.getParameter("password");
  33. UserService userService = new UserServiceImpl();
  34. Object login = userService.login(username, password);
  35. if(login.equals("user")) {
  36. request.setAttribute("errorMsg", "用户名不存在");
  37. request.getRequestDispatcher("client/login.jsp").forward(request, response);
  38. }else if(login.equals("pass")) {
  39. request.setAttribute("errorMsg", "密码错误");
  40. request.getRequestDispatcher("client/login.jsp").forward(request, response);
  41. }else {
  42. User user = (User) login;
  43. request.getSession().setAttribute("user", user);
  44. response.sendRedirect("client/index.jsp");
  45. }
  46. } catch (SQLException e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50. }
  51. /**
  52. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  53. */
  54. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  55. // TODO Auto-generated method stub
  56. doGet(request, response);
  57. }
  58. }

7.user.jsp

代码

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <title>空白</title>
  7. </head>
  8. <body style="background-color:#bbb">
  9. <!-- 调用头部页面 -->
  10. <div style="width:100%;height:100px;float:left">
  11. <jsp:include page="/client/head.jsp"></jsp:include>
  12. </div>
  13. <!-- 通用内容体大小 -->
  14. <div style="width:70%;height:720px;float:left;margin-left:15%;">
  15. <a href="${pageContext.request.contextPath }/Logout">退出登录</a>
  16. </div>
  17. <!-- 调用底部页面 -->
  18. <div style="width:100%;height:60px;float:left">
  19. <jsp:include page="/client/foot.jsp"></jsp:include>
  20. </div>
  21. </body>
  22. </html>

效果图

8.LogoutServlet

移除session域中的user属性,然后跳转回主页。

  1. package cn.edu.bdu.mc.servlets;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. /**
  9. * Servlet implementation class LogoutServlet
  10. */
  11. @WebServlet("/Logout")
  12. public class LogoutServlet extends HttpServlet {
  13. private static final long serialVersionUID = 1L;
  14. /**
  15. * @see HttpServlet#HttpServlet()
  16. */
  17. public LogoutServlet() {
  18. super();
  19. // TODO Auto-generated constructor stub
  20. }
  21. /**
  22. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  23. */
  24. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. // TODO Auto-generated method stub
  26. request.getSession().removeAttribute("user");
  27. response.sendRedirect("client/index.jsp");
  28. }
  29. /**
  30. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  31. */
  32. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  33. // TODO Auto-generated method stub
  34. doGet(request, response);
  35. }
  36. }

9.BookService的改动——添加图片

代码

差点忘了往上放这部分。。。

  1. @Override
  2. public void addImg(int book_id, String path) throws SQLException, IOException {
  3. // TODO Auto-generated method stub
  4. dao.addImg(book_id, path);
  5. }
  6. @Override
  7. public InputStream getImgById(int book_id) throws SQLException {
  8. // TODO Auto-generated method stub
  9. return dao.getImgById(book_id);
  10. }
  11. //好像应该看dao里的,,
  12. /*
  13. @Override
  14. public void addImg(int book_id, String path) throws SQLException, IOException {
  15. // TODO Auto-generated method stub
  16. Connection conn = null;
  17. PreparedStatement ps = null;
  18. FileInputStream in = null;
  19. in = new FileInputStream(new File(path));
  20. conn = JDBCUtil.getConn();
  21. String sql = "update book set book_img = ? where book_id = ?";
  22. ps = conn.prepareStatement(sql);
  23. ps.setBinaryStream(1, in, in.available());
  24. ps.setInt(2, book_id);
  25. int count = ps.executeUpdate();
  26. if (count > 0) {
  27. System.out.println("插入成功!");
  28. } else {
  29. System.out.println("插入失败!");
  30. }
  31. JDBCUtil.release(conn, ps);
  32. }
  33. @Override
  34. public InputStream getImgById(int book_id) throws SQLException {
  35. // TODO Auto-generated method stub
  36. Connection conn = null;
  37. PreparedStatement ps = null;
  38. ResultSet rs = null;
  39. InputStream in = null;
  40. try {
  41. conn = JDBCUtil.getConn();
  42. String sql = "select book_img from book where book_id = ?";
  43. ps = conn.prepareStatement(sql);
  44. ps.setInt(1, book_id);
  45. rs = ps.executeQuery();
  46. while (rs.next()) {
  47. in = rs.getBinaryStream("book_img");
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. JDBCUtil.release(conn, ps, rs);
  53. return in;
  54. }
  55. */
  56. //注意,一定不要忘记释放连接,不然你网页卡半天也加载不出来图片。。。

总结

今天遇到的错误还挺多的,好在都解决得差不多了,主页终于完工了,然后今天其实打代码的时间比较少,啊,做不完啦做不完啦!好烦啊!

原文链接:http://www.cnblogs.com/zhangA/p/11048470.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号