大項目之網上書城(五)——主頁(End)

大項目之網上書城(五)——主頁(End)

不說了!日更啊!css

主要改動

今天終於把主頁完成啦!給書加了圖片,雖然圖片嚴重不符,不夠這都是小問題,如今仍是測試階段,到時候整理下數據庫就好了。html

同時終於加了登陸頁面,並把驗證碼移到了登陸界面,註冊頁面就不須要了。java

最後,加了個用戶頁面,不過目前只有退出帳號功能233333jquery

1.主頁(終於完成啦)

完整代碼

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<style>
li{
    float:left;
    width:100%;
    height:10%;
    font-size:16px;
    color:#8deeee;
}
</style>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-3.3.1/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/client/js/index.js"></script>
<title>主頁</title>
</head>
<body style="background-color:#bbb">
<!-- 調用頭部頁面 -->
<div style="width:100%;height:100px;float:left">
<jsp:include page="/client/head.jsp"></jsp:include>
</div>
<!-- 通用內容體大小 -->
<div style="width:70%;height:886px;float:left;margin-left:15%;">
<!-- 二級導航 -->
<jsp:include page="/client/head2.jsp"></jsp:include>    
<!-- 圖書分類and輪播圖and文案and熱門推薦and新書上架and新書榜 -->
<div style="width:100%;height:800px;float:left;margin-top:2%;background-color:#bbb;">
<!-- 圖書分類 -->
<div style="width:23%;height:100%;float:left;background-color:#a8f;">
    <div style="width:100%;height:6%;text-align:center;line-height:45px;background-color:#556B2F">
        <font color="#ddd" style="font-size:20px;">圖書分類</font>
    </div>
    <div style="width:100%;height:94%;text-align:center;line-height:45px;background-color:#548B54">
        <ul style="width:100%;height:100%;text-align:left;">
            <li>
                <a href="${pageContext.request.contextPath }/client/pai/index.jsp">好書拍賣</a>
            </li>
            <li>
                <a href="${pageContext.request.contextPath }/client/net_literature/index.jsp">網絡文學</a>
            </li>
            <li>
                <a href="${pageContext.request.contextPath }/client/clothing/index.jsp">服裝</a>
            </li>
            <li>
                <a href="${pageContext.request.contextPath }/client/sport/index.jsp">運動戶外</a>
            </li>
            <li>
                <a href="${pageContext.request.contextPath }/client/nursery/index.jsp">孕嬰童</a>
            </li>
            <li>
                <a href="${pageContext.request.contextPath }/client/nursery/index.jsp">孕嬰童</a>
            </li>
            <li>
                <a href="${pageContext.request.contextPath }/client/food/index.jsp">食品</a>
            </li>
            <li>
                <a href="#">暫無分類</a>
            </li>
            <li>
                <a href="#">暫無分類</a>
            </li>
            <li>
                <a href="#">暫無分類</a>
            </li>
        </ul>
    </div>  
</div>
<!-- 輪播圖 -->
<div style="width:50%;height:35%;float:left;margin-left:2%;background-size:100% 100%;"id="lun">
    <!-- table按鈕沉底大法! -->
    <table style="width:100%;height:100%">
        <tr>
            <td style="vertical-align:bottom;">
                <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:60%;" id="l1">1</button>
                <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:2%;" id="l2">2</button>
                <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:2%;" id="l3">3</button>
                <button class="btn btn-warning" style="width:30px;height:30px;float:left;margin-left:2%;" id="l4">4</button>
            </td>
        </tr>
    </table>
</div>
<!-- 文案and熱門推薦 -->
<div style="width:23%;height:35%;float:left;background-color:#bbb;margin-left:2%">
<!-- 文案 -->
<div style="width:100%;height:30%;float:left;background-color:#548B54;">
    <font style="font-size:20px;text-align:center;display:block;width:100%;color:#ee4000">618年中狂歡</font>
    <font style="display:block;color:#eead0e">十萬童書,每滿100減50</font>
    <font style="display:block;color:#eead0e">藝術繪畫,每滿100減50</font>
</div>
<!-- 熱門推薦 -->
<div style="width:100%;height:64%;float:left;background-color:#a8f;margin-top:8%">
    <div style="width:100%;height:25%;text-align:center;line-height:45px;background-color:#556B2F">
        <font color="#ddd" style="font-size:20px;">熱門推薦</font>
    </div>
    <div style="width:100%;height:75%;text-align:center;line-height:45px;background-color:#548B54;margin-top:-5%;">
        <ul>
            <li style="text-align:left;color:black"><font id="a1"></font><font id="remen1" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="a2"></font><font id="remen2" style="font-size:8px"></font></li>
        </ul>
        <div style="width:100%;height:20%;float:left;margin-top:5%">
            <button class="btn btn-info" style="float:left;margin-left:20%;font-size:8px;"id="b1">1</button>
            <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b2">2</button>
            <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b3">3</button>
            <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b4">4</button>
        </div>
    </div>
</div>
</div>
<!-- 新書上架 -->
<div style="width:50%;height:63%;float:left;background-color:#a8f;margin-left:2%;margin-top:2%;"id="xinShu">
<table border="1"style="width:100%;height:100%">
    <tr>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=1" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu1">圖書</font>
                <font style="font-size:8px"id="re1"></font>
            </div>
        </td>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=2" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu2">圖書</font>
                <font style="font-size:8px"id="re2"></font>
            </div>
        </td>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=3" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu3">圖書</font>
                <font style="font-size:8px"id="re3"></font>
            </div>
        </td>
    </tr>
    <tr>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;float:left;margin-left:3%">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=4" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu4">圖書</font>
                <font style="font-size:8px"id="re4"></font>
            </div>
        </td>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=5" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu5">圖書</font>
                <font style="font-size:8px"id="re5"></font>
            </div>
        </td>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=6" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu6">圖書</font>
                <font style="font-size:8px"id="re6"></font>
            </div>
        </td>
    </tr>
    <tr>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=7" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu7">圖書</font>
                <font style="font-size:8px"id="re7"></font>
            </div>
        </td>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=8" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu8">圖書</font>
                <font style="font-size:8px"id="re8"></font>
            </div>
            
        </td>
        <td style="width:33%">
            <div style="width:94%;height:80%;background-color:white;margin-left:3%;float:left">
                <img alt="圖書" src="${pageContext.request.contextPath}/XinShuImg?shu=9" style="width:100%;"/>
            </div>
            <div style="width:94%;float:left;margin-left:3%;height:15%;">
                <font style="font-size:16px;margin-left:3%;"id="shu9">圖書</font>
                <font style="font-size:8px"id="re9"></font>
            </div>
        </td>
    </tr>
</table>
</div>
<!-- 新書榜 -->
<div style="width:23%;height:63%;float:left;background-color:#a8f;margin-left:2%;margin-top:2%;">
    <div style="width:100%;height:10%;text-align:center;line-height:45px;background-color:#556B2F">
        <font color="#ddd" style="font-size:20px;">新書排行榜</font>
    </div>
    <div style="width:100%;height:90%;text-align:center;line-height:45px;background-color:#548B54;">
        <ul style="width:100%;height:90%;float:left;">
            <li style="text-align:left;color:black"><font id="x1"></font><font id="r1" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x2"></font><font id="r2" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x3"></font><font id="r3" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x4"></font><font id="r4" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x5"></font><font id="r5" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x6"></font><font id="r6" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x7"></font><font id="r7" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x8"></font><font id="r8" style="font-size:8px"></font></li>
            <li style="text-align:left;color:black"><font id="x9"></font><font id="r9" style="font-size:8px"></font></li>
        </ul>
            <div style="width:100%;height:10%;float:left;">
                <button class="btn btn-info" style="float:left;margin-left:20%;font-size:8px;"id="b5">1</button>
                <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b6">2</button>
                <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b7">3</button>
                <button class="btn btn-info" style="float:left;margin-left:4%;font-size:8px;"id="b8">4</button>
            </div>
    </div>
</div>
</div>
</div>
<!-- 調用底部頁面 -->
<div style="width:100%;height:60px;float:left">
<jsp:include page="/client/foot.jsp"></jsp:include>
</div>
</body>
</html>

效果圖

2.head.jsp的小改動

給個人帳號加了個判斷,若是已經登陸了,就顯示爲你的用戶名,可跳轉到你的我的主頁。不然顯示「個人帳號」,可跳轉到登陸界面。恩,悄悄給BookStore字樣作了個到主頁的跳轉~sql

代碼

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
<!-- 這裏的href請寫本身的bootstrap的css的連接。若是沒有下載,能夠用這個 -->
<!-- https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css -->
<link rel="stylesheet" href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<style type="text/css">
@font-face{
  font-family: myFont;
  src: url("${pageContext.request.contextPath }/bootstrap-3.3.7-dist/fonts/Adventure-Normal.ttf");
}
a:link {text-decoration:none;color: black} /* 未訪問的連接 */
a:visited {text-decoration:none;color: black} /* 已訪問的連接 */
a:hover {text-decoration:none;color: #068} /* 鼠標移動到連接上 */
a:active {text-decoration:none;color: #068} /* 選定的連接,即鼠標按下去的時候不鬆開顯示的狀態 */
a{
    font-size:18px;
}
</style>
</head>
<body>
<div style="height:100px;width:100%;float:left">
    <!-- BookStore字樣 -->
    <div style="line-height:120px;height:100px;width:45%;margin-left:15%;float:left">
        <a href="${pageContext.request.contextPath }/client/index.jsp"><font face="myFont" color = "black" style="font-size:60px">BookStore</font></a>
    </div>
    <!-- 導航欄 -->
    <div style="height:100px;width:25%;float:left;margin-right:15%">
        <div style="height:100px;width:25%;float:left;padding-top:40px">
            <a href="#"><span class="glyphicon glyphicon-shopping-cart"></span>購物車</a>
        </div>
        <div style="height:100px;width:25%;float:left;padding-top:40px">
        <a href="#">幫助中心</a>
        </div>
        <div style="height:100px;width:25%;float:left;padding-top:40px">
            <c:if test="${sessionScope.user==null}" var="f">
                <a href="${pageContext.request.contextPath }/client/login.jsp">個人帳戶</a>
            </c:if>
            <c:if test="${!f }">
                <a href="${pageContext.request.contextPath }/client/user.jsp">${sessionScope.user.username }</a>
            </c:if>
        </div>
        <div style="height:100px;width:25%;float:left;padding-top:40px">
            <a href="${pageContext.request.contextPath }/client/register.jsp">用戶註冊</a>
        </div>
    </div>
</div>
</body>
</html>

3.login.jsp

###代碼

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>登陸</title>
<!-- 這裏的href請寫本身的bootstrap的css的連接。若是沒有下載,能夠用這個 -->
<!-- https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css -->
<link rel="stylesheet" href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<!-- jquery和bootstrap的連接能夠用下面的 -->
<!-- https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js -->
<!-- https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js -->
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-3.3.1/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<!-- login.js是我本身寫的,裏面放了判斷輸入是否合理,以及驗證碼的正確與否的。以後放上了。 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/client/js/login.js"></script>
</head>
<body style="background-color:#bbb">
<font size="7" style="color:red;">${errorMsg }</font>
<!-- 調用頭部頁面 -->
<div style="width:100%;height:100px;float:left">
<jsp:include page="head.jsp"></jsp:include>
</div>
<!-- 通用內容體大小 -->
<div style="width:70%;height:720px;float:left;margin-left:15%;">
    <!-- 好看的圖 -->
    <div style="width:55%;height:100%;float:left;margin-top:10%;">
        <img alt="拿書男孩" src="${pageContext.request.contextPath }/client/img/boy.jpg" style="width:90%;">
    </div>
    <!-- 登陸界面 -->
    <div style="width:45%;height:80%;float:left;margin-top:7%">
        <h1 style="color:#8b6914;text-align:center">用戶登陸</h1>
        <hr style="height:2px;border:none;border-top:5px ridge green;" />
        <form action="${pageContext.request.contextPath }/Login" method="post" class="form-horizontal" role="form">
            <div class="form-group">
                <label for="lastname" class="col-sm-3 control-label input-lg">暱稱</label>
                <div class="col-sm-9">
                    <input type="text" name="username" id="username" class="form-control input-lg"
                           placeholder="請輸入暱稱" style="float:left"/>
                </div>
            </div>
            <div class="form-group">
                <label for="firstname" class="col-sm-3 control-label input-lg">密碼</label>
                <div class="col-sm-9">
                    <input type="password" name="password" id="password" 
                    class="form-control input-lg" placeholder="請輸入密碼" style="float:left"/>
                </div>
            </div>
            
            <div class="form-group">
                <label for="firstname" class="col-sm-3 control-label input-lg">驗證碼</label>
                <div class="col-sm-9">
                    <input type="text" id="code"
                    class="form-control input-lg" placeholder="請輸入驗證碼" style="width:45%;float:left"/>
                    <img src="${pageContext.request.contextPath}/imageCode"
                    width="180"height="30"class="textinput"
                    style="height:42px;width:55%;"id="img"/>
                    <a href="javascript:void(0);"id="a1" >換一張</a>
                </div>
            </div>
            <div class="form-group">
                <label for="firstname" class="col-sm-1 control-label input-lg"></label>
                <div class="col-sm-5">
                    <input type="submit" name="submit" value="提交"
                    class="form-control input-lg btn btn-primary"style="width:100%;float:left"/>
                </div>
                <div class="col-sm-5">
                    <input type="reset" name="reset" value="重置" id="re"
                    class="form-control input-lg btn btn-warning"style="width:100%;float:left"/>
                </div>
            </div>
        </form>
        <span style="margin-left:10%">尚未賬號?請先<a href="${pageContext.request.contextPath }/client/register.jsp">註冊</a>!</span>
    </div>
</div>
<!-- 調用底部頁面 -->
<div style="width:100%;height:60px;float:left">
<jsp:include page="foot.jsp"></jsp:include>
</div>
</body>
</html>

效果圖

4.login.js

代碼

由於貌似jquery的post提交是在其餘代碼結束以後才運行的,因而我取巧把二維碼的驗證和表單提交分開寫了,我的認爲也算是個不錯的思路吧~數據庫

$(function(){
    var code;
    $.post("../CheckCode",function(data){
        code=data;
    });
    $("#a1").click(function(){
        $("#img").attr("src","/bookstore/imageCode?time="+new Date().getTime());
    });
    var flag;
    $("#code").keyup(function(){
        $.post("/bookstore/CheckCode",function(data){
            code=data;
            if(code==$("#code").val()){
                flag=true;
            }
            else{
                flag=false;
            }
        });     
    });
    $("form").submit(function(){
        if(flag){
            return true;
        }else{
            alert("驗證碼錯誤");
            return false;
        }
    });
});

5.userServiceImpl的login方法

代碼

我的感受設計的仍是挺精妙的,能夠用一個函數就檢測用戶名是否存在,密碼是否正確,同時也能在正確的時候返回user對象。npm

@Override
    public Object login(String username, String password) throws SQLException {
        // TODO Auto-generated method stub
        User user = dao.findUserByName(username);
        if(user==null) {
            return "user";
        }else {
            if(user.getPassword().equals(password)) {
                return user;
            }else {
                return "pass";
            }
        }
    }

6.LoginServlet

代碼

接收用戶名密碼,而後調用userService的login方法,並對返回值作出相應處理。bootstrap

package cn.edu.bdu.mc.servlets;

import java.io.IOException;
import java.sql.SQLException;

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 cn.edu.bdu.mc.beans.User;
import cn.edu.bdu.mc.services.UserService;
import cn.edu.bdu.mc.services.impls.UserServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // TODO Auto-generated method stub
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            UserService userService = new UserServiceImpl();
            Object login = userService.login(username, password);
            if(login.equals("user")) {
                request.setAttribute("errorMsg", "用戶名不存在");
                request.getRequestDispatcher("client/login.jsp").forward(request, response);
            }else if(login.equals("pass")) {
                request.setAttribute("errorMsg", "密碼錯誤");
                request.getRequestDispatcher("client/login.jsp").forward(request, response);
            }else {
                User user = (User) login;
                request.getSession().setAttribute("user", user);
                response.sendRedirect("client/index.jsp");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

7.user.jsp

代碼

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>空白</title>
</head>
<body style="background-color:#bbb">
<!-- 調用頭部頁面 -->
<div style="width:100%;height:100px;float:left">
<jsp:include page="/client/head.jsp"></jsp:include>
</div>
<!-- 通用內容體大小 -->
<div style="width:70%;height:720px;float:left;margin-left:15%;">
<a href="${pageContext.request.contextPath }/Logout">退出登陸</a>
</div>
<!-- 調用底部頁面 -->
<div style="width:100%;height:60px;float:left">
<jsp:include page="/client/foot.jsp"></jsp:include>
</div>
</body>
</html>

效果圖

8.LogoutServlet

移除session域中的user屬性,而後跳轉回主頁。網絡

package cn.edu.bdu.mc.servlets;

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;

/**
 * Servlet implementation class LogoutServlet
 */
@WebServlet("/Logout")
public class LogoutServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.getSession().removeAttribute("user");
        response.sendRedirect("client/index.jsp");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

9.BookService的改動——添加圖片

代碼

差點忘了往上放這部分。。。

@Override
    public void addImg(int book_id, String path) throws SQLException, IOException {
        // TODO Auto-generated method stub
        dao.addImg(book_id, path);
    }

    @Override
    public InputStream getImgById(int book_id) throws SQLException {
        // TODO Auto-generated method stub
        return dao.getImgById(book_id);
    }
//好像應該看dao裏的,,
/*
    @Override
    public void addImg(int book_id, String path) throws SQLException, IOException {
        // TODO Auto-generated method stub
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        in = new FileInputStream(new File(path));
        conn = JDBCUtil.getConn();
        String sql = "update book set book_img = ? where book_id = ?";
        ps = conn.prepareStatement(sql);
        ps.setBinaryStream(1, in, in.available());
        ps.setInt(2, book_id);
        int count = ps.executeUpdate();
        if (count > 0) {
            System.out.println("插入成功!");
        } else {
            System.out.println("插入失敗!");
        }
        JDBCUtil.release(conn, ps);
    }

    @Override
    public InputStream getImgById(int book_id) throws SQLException {
        // TODO Auto-generated method stub
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        InputStream in = null;
        try {
            conn = JDBCUtil.getConn();
            String sql = "select book_img from book where book_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, book_id);
            rs = ps.executeQuery();
            while (rs.next()) {
                in = rs.getBinaryStream("book_img");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JDBCUtil.release(conn, ps, rs);
        return in;
    }
*/
//注意,必定不要忘記釋放鏈接,否則你網頁卡半天也加載不出來圖片。。。

總結

今天遇到的錯誤還挺多的,好在都解決得差很少了,主頁終於完工了,而後今天其實打代碼的時間比較少,啊,作不完啦作不完啦!好煩啊!

相關文章
相關標籤/搜索