大项目之网上书城(十)——自动登录

大项目之网上书城(十)——自动登录

主要改动

今天大概就弄了个自动登录吧,还有一个搜索书籍。没有效果图今天。因为刚刚数据库炸了,他说我获取连接太多了。emmmmm,转储为sql文件都差点没成功。暂时不知道什么原因。今天有点慌。

1.自动登录

请看我的另一个博客

2.搜索图书

2.1 head.jsp改了改

<%@ 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">
<link rel="stylesheet" href="${pageContext.request.contextPath }/css/font.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-3.3.1/jquery-3.3.1.min.js"></script>
<style type="text/css">
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">
		<form action="${pageContext.request.contextPath }/SouSuo" method="post">
			<a href="${pageContext.request.contextPath }/client/index.jsp" style="float:left;"><font face="myFont" color = "black" style="font-size:60px">BookStore</font></a>
			<div class="col-sm-6" style="margin-top:25px;">
				<input type="text" name="word" class="form-control input-lg"
					   placeholder="请输入要搜索书籍" style="float:left"/>
			</div>
			<div class="col-sm-3" style="margin-top:25px;margin-left:-20px;">
				<input type="submit" value="搜索" class="form-control input-lg btn btn-primary" style="float:left;" />
			</div>
		</form>
	</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="${pageContext.request.contextPath }/client/cart.jsp"><span class="glyphicon glyphicon-shopping-cart"></span>购物车</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/order.jsp">我的订单</a>
			</c:if>
			<c:if test="${sessionScope.root!=null&&!f}" var="f1">
				<a href="${pageContext.request.contextPath }/client/login.jsp">切换用户</a>
			</c:if>
			<c:if test="${!f&&!f1}">
				<a href="${pageContext.request.contextPath }/client/login.jsp">登录下单</a>
			</c:if>
		</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/user.jsp">${sessionScope.user.username }</a>
			</c:if>
			<c:if test="${sessionScope.root!=null&&!f}" var="f1">
				<a href="${pageContext.request.contextPath }/admin/index.jsp">管理图书</a>
			</c:if>
			<c:if test="${!f&&!f1 }">
				<a href="${pageContext.request.contextPath }/client/login.jsp">我的账户</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>

2.2 SouSuoServlet

package cn.edu.bdu.mc.servlets;

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

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.Book;
import cn.edu.bdu.mc.daos.impls.BookDaoImpl;
import cn.edu.bdu.mc.services.BookService;
import cn.edu.bdu.mc.services.impls.BookServiceImpl;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String word = request.getParameter("word");
			BookService bookService = new BookServiceImpl();
			List<Book> list = bookService.findBookByName(word);
			request.setAttribute("list", list);
			request.getRequestDispatcher("client/list.jsp").forward(request, response);
		} 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);
	}

}

2.3 list.jsp

<%@page import="java.util.List"%>
<%@page import="cn.edu.bdu.mc.beans.Book"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>空白</title>
</head>
<body style="background-color:#bbb;width:1400px;margin:0 auto">
<!-- 调用头部页面 -->
<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%;">
<%
	List<Book>list = (List<Book>)request.getAttribute("list");
	if(list==null){
		response.sendRedirect("index.jsp");
	}else{
		for(Book book:list){
			%>
			<div class="panel panel-info">
			  <div class="panel-heading">
			    <h3 class="panel-title"><a href="client/<%=book.getClazz() %>/shu.jsp?er_id=<%=book.getSecond_id() %>"><%=book.getBook_name() %></a></h3>
			  </div>
			  <table class="table">
			  	<tr>
			  		<th>价格</th>
			  		<th>类别</th>
			  		<th>数量</th>
			  		<th>点击量</th>
			  		<th>购买量</th>
			  		<th>热度</th>
			  		<th>描述</th>
			  	</tr>
			  	<tr>
			  		<td><%=book.getPrice() %>&nbsp;元</td>
			  		<td><%=book.getClazz() %></td>
			  		<td><%=book.getCount() %></td>
			  		<td><%=book.getClick_num() %></td>
			  		<td><%=book.getBuy_num() %></td>
			  		<td><%=book.getRe_du() %></td>
			  		<td><%=book.getDescribtion() %></td>
			  	</tr>
			  </table>
			</div>
			<%
		}
	}
%>
</div>
<!-- 调用底部页面 -->
<div style="width:100%;height:60px;float:left">
<jsp:include page="/client/foot.jsp"></jsp:include>
</div>
</body>
</html>

总结

数据库这个获取连接太多真的吓到我了,明天再研究。。

posted @ 2019-06-24 00:52  return_false  阅读(469)  评论(0编辑  收藏  举报