基于javaweb+mysql的jsp+servlet在线购书商城系统(java+jsp+mysql+servlert+ajax)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的JSP+Servlet在线购书商城系统(java+jsp+mysql+servlert+ajax)
一、项目简述
功能:一个基于JavaWeb的网上书店的设计与实现,归纳 出了几个模块,首先是登录注册模块,图书查找模块,购 物车模块,订单模块,个人中心模块,用户管理模块,图 书管理模块等。 该项目是javaJeb技术的实战操作,采用了MVC设计模 式,包括基本的entity, jscript, servlet,以及ajax异步请 求,查询分页,持久化层方法的封装等等,对javaweb技 术的巩固很有帮助,为J2EE的学习打下基础,适用于课程 设计,毕业设计。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。
项目技术: JSP + Entity+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload 等等。
* @param response
* @throws ServletException
* @throws IOException
*/
private void initUser(HttpServletRequest request, HttpServletResponse response,int userId) throws ServletException, IOException {
UserDaoImpl dao = new UserDaoImpl();
Users user = dao.getUserById(userId);
//将user设置到session中
request.getSession().setAttribute("user", user);
}
/**
* 获取用户列表
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void userListByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDaoImpl dao = new UserDaoImpl();
List<Users> ulist = dao.getUserList();
//获取分页
Integer page=1;
String strPage = request.getParameter("page");
if(StringUtil.isNumber(strPage)==false){
}else{
page =Integer.parseInt(strPage);
}
//封装成分页对象
PageBean<Users> Pager = new PageBean<Users>(page,10,ulist);
request.getSession().setAttribute("ulist", Pager);
response.sendRedirect("admin/userlist.jsp");
}
/**
* 删除用户
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void delUserByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(StringUtil.isNumber(request.getParameter("uid"))==false){
response.sendRedirect("admin/userlist.jsp");
return;
}
int userId = Integer.parseInt(request.getParameter("uid"));
UserDaoImpl dao = new UserDaoImpl();
//从session中获取用户信息
Users user = (Users) request.getSession().getAttribute("user");
if(user!=null){
request.getSession().removeAttribute("user");
}
response.sendRedirect("admin/login.jsp");
}
/**
* 修改用户信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取提交的参数
String id = (String) request.getParameter("id");
if(id==null || "".equals(id)){
response.sendRedirect("login.jsp");
return;
}
String name = (String) request.getParameter("name");
String mail = (String) request.getParameter("mail");
String phone = (String) request.getParameter("phone");
String address = (String) request.getParameter("address");
//封装成Users对象
Users user = new Users();
user.setId(Integer.parseInt(id));
user.setName(name);
user.setMail(mail);
user.setPhone(phone);
user.setAddress(address);
//调用dao层方法
UserDaoImpl dao = new UserDaoImpl();
Boolean b = dao.updateUser(user);
//重置session的user信息
initUser(request, response, Integer.parseInt(id));
//设置修改信息结果字符串
if(b)
request.getSession().setAttribute("MSG_USER_UPDATE_RESULT", "个人资料修改成功");
else
request.getSession().setAttribute("MSG_USER_UPDATE_RESULT", "个人资料修改失败");
response.sendRedirect("user/userinfo.jsp");
}
/**
* 注册用户
* @param request
* @param response
* @throws ServletException
protected void loginByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("cart");
if(cart==null){
cart = new ShoppingCart();
}
//获取参数
String name = request.getParameter("loginId");
String pwd = request.getParameter("loginPwd");
//调用登录验证
UserDaoImpl dao = new UserDaoImpl();
Users user = dao.doLoginByAdmin(name, pwd);
if(user!=null){
//将user对象存入session中,便于前台使用
request.getSession().setAttribute("user", user);
response.sendRedirect("admin/index.jsp");
return;
}else{
response.sendRedirect("admin/login.jsp");
return;
}
}
/**
* 用户注销退出
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从session中获取用户信息
Users user = (Users) request.getSession().getAttribute("user");
if(user!=null){
request.getSession().removeAttribute("user");
}
response.sendRedirect("login.jsp");
}
protected void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("user/user.jsp");
}
/**
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//String opt = request.getParameter("opt");
String role = request.getParameter("role");
//判断是否为管理员操作
if(role!=null && role.equals("3")){
optByAdmin(request, response);
return;
}
}
/**
* 管理员操作
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void optByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String opt = request.getParameter("opt");
//判断是否登录
Users user = (Users) request.getSession().getAttribute("user");
if(user==null || user.getUserRoleId()!=3){
response.sendRedirect("admin/login.jsp");
return;
}
//添加分类
if(opt.equals("add")){
addCategoryByAdmin(request, response);
}
//编辑分类
if(opt.equals("edit")){
editCategoryByAdmin(request, response);
}
}
/**
* 添加分类
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void addCategoryByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取提交的参数
String name = request.getParameter("name");
book.setContentDescription(contentDescription);
book.setAuthorDescription(authorDescription);
//调用dao层方法
BookDaoImpl dao = new BookDaoImpl();
//页面跳转
if(dao.addBook(book)){
request.getSession().setAttribute("ADMIN_MSG_BOOK", "添加成功,图书:"+title);
bookListByAdmin(request, response);
}else{
request.getSession().setAttribute("ADMIN_MSG_BOOK", "添加失败!");
response.sendRedirect("admin/bookadd.jsp");
}
}
}
/**
* ShopingCart操作实体类
*
*/
@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CartServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* Book操作servlet类
*
*/
@WebServlet("/BookServlet")
public class BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public BookServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String opt = request.getParameter("opt");
String role = request.getParameter("role");
//判断是否为管理员操作
if(role!=null && role.equals("3")){
optByAdmin(request, response);
return;
}
//操作判断
if(opt.equals("byTitle")){
queryByTitle(request, response);
}else if(opt.equals("byIsbn")){
queryByIsbn(request, response);
}else if(opt.equals("byCategory")){
queryByCategory(request, response);
}else if(opt.equals("buyBook")){
buyBook(request, response);
}else if(opt.equals("search")){
search(request, response);
}else if(opt.equals("byPublish")){
queryByPublish(request, response);
}else if(opt.equals("byCategoryName")){
queryByCategoryName(request, response);
}
//添加分类
if(opt.equals("add")){
addPublisherByAdmin(request, response);
}
//编辑分类
if(opt.equals("edit")){
editPublisherByAdmin(request, response);
}
}
/**
* 添加分类
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void addPublisherByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取提交的参数
String name = request.getParameter("name");
//信息完整性校验
if(name==null || name.equals("")){
request.getSession().setAttribute("ADMIN_MSG_PUBLISHER", "添加失败,请检查分类是否合法输入");
response.sendRedirect("admin/publisherlist.jsp");
return;
}
//封装对象
Publishers p = new Publishers();
p.setName(name);
//调用dao层方法
PublisherDaoImpl dao = new PublisherDaoImpl();
if(dao.addPublisher(p)){
request.getSession().setAttribute("ADMIN_MSG_PUBLISHER", "添加成功");
//初始化分类集合
initPublisher(request, response);
}else{
request.getSession().setAttribute("ADMIN_MSG_PUBLISHER", "添加失败");
}
//页面跳转
response.sendRedirect("admin/publisherlist.jsp");
}
/**
* 编辑分类
* @param request
* @param response
* @throws ServletException
* @throws IOException
String role = request.getParameter("role");
//判断是否为管理员操作
if(role!=null && role.equals("3")){
optByAdmin(request, response);
return;
}
}
/**
* 管理员操作
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void optByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String opt = request.getParameter("opt");
//判断是否登录
Users user = (Users) request.getSession().getAttribute("user");
if(user==null || user.getUserRoleId()!=3){
response.sendRedirect("admin/login.jsp");
return;
}
//添加分类
if(opt.equals("add")){
addCategoryByAdmin(request, response);
}
//编辑分类
if(opt.equals("edit")){
editCategoryByAdmin(request, response);
}
}
/**
* 添加分类
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @param response
* @throws ServletException
* @throws IOException
*/
protected void logoutByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从session中获取用户信息
Users user = (Users) request.getSession().getAttribute("user");
if(user!=null){
request.getSession().removeAttribute("user");
}
response.sendRedirect("admin/login.jsp");
}
/**
* 修改用户信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取提交的参数
String id = (String) request.getParameter("id");
if(id==null || "".equals(id)){
response.sendRedirect("login.jsp");
return;
}
String name = (String) request.getParameter("name");
String mail = (String) request.getParameter("mail");
String phone = (String) request.getParameter("phone");
String address = (String) request.getParameter("address");
//封装成Users对象
Users user = new Users();
user.setId(Integer.parseInt(id));
user.setName(name);
user.setMail(mail);
user.setPhone(phone);
user.setAddress(address);
//调用dao层方法
UserDaoImpl dao = new UserDaoImpl();
Boolean b = dao.updateUser(user);
//重置session的user信息
* Category操作servlet类
*
*/
@WebServlet("/PublisherServlet")
public class PublisherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public PublisherServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//String opt = request.getParameter("opt");
String role = request.getParameter("role");
//判断是否为管理员操作
if(role!=null && role.equals("3")){
optByAdmin(request, response);
return;
}
}
/**
* 管理员操作
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void optByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String opt = request.getParameter("opt");
//判断是否登录
Users user = (Users) request.getSession().getAttribute("user");
if(user==null || user.getUserRoleId()!=3){
response.sendRedirect("admin/login.jsp");
return;
}
//添加分类
if(opt.equals("add")){
addPublisherByAdmin(request, response);
}
//编辑分类
if(opt.equals("edit")){
editPublisherByAdmin(request, response);
}
}
/**
* 添加分类
}
/**
* Category操作servlet类
*
*/
@WebServlet("/CategoryServlet")
public class CategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CategoryServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//String opt = request.getParameter("opt");
String role = request.getParameter("role");
//判断是否为管理员操作
if(role!=null && role.equals("3")){
optByAdmin(request, response);
return;
}
}
/**
//设置修改信息结果字符串
if(b)
request.getSession().setAttribute("MSG_USER_UPDATE_RESULT", "个人资料修改成功");
else
request.getSession().setAttribute("MSG_USER_UPDATE_RESULT", "个人资料修改失败");
response.sendRedirect("user/userinfo.jsp");
}
/**
* 注册用户
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取提交的参数
String loginId = request.getParameter("loginId");
String loginPwd = request.getParameter("loginPwd");
String name = request.getParameter("name");
String mail = request.getParameter("mail");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
//信息完整性校验
if(loginId==null
|| loginId.equals("")
|| loginPwd==null
|| loginPwd.equals("")
|| mail==null
|| mail.equals("")
|| address==null
|| address.equals("")){
request.getSession().setAttribute("MSG_USER_REGISTER_RESULT", "注册失败:请检查注册信息是否正确");
response.sendRedirect("register.jsp");
return;
}
//封装用户信息成Users对象
Users user = new Users(loginId,loginPwd,name,address,phone,mail,1,1);
//调用dao层方法
UserDaoImpl dao = new UserDaoImpl();
//页面跳转
if(dao.addUser(user)){
response.sendRedirect("registerok.jsp");
}else{
request.getSession().setAttribute("MSG_USER_REGISTER_RESULT", "注册失败!");
response.sendRedirect("register.jsp");
}
private static final long serialVersionUID = 1L;
public UserServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String opt = request.getParameter("opt");
String role = request.getParameter("role");
//判断是否为管理员操作
if(role!=null && role.equals("3")){
optByAdmin(request, response);
return;
}
//登录
if(opt.equals("login")){
login(request, response);
return;
}
//注册
else if(opt.equals("register")){
register(request, response);
return;
}
//是否登录验证
if(request.getSession().getAttribute("user")==null){
response.sendRedirect("login.jsp");
return;
}
//注销
if(opt.equals("logout")){
logout(request, response);
}
//修改信息
else if(opt.equals("updateUser")){
updateUser(request, response);
}
//进入个人中心
else if(opt.equals("show")){
show(request, response);
}
}
/**
* 管理员操作
* @param request
* @param response
* @throws ServletException
* @throws IOException
String name = request.getParameter("name");
if(name==null || name.equals("")){
response.sendRedirect("admin/categorylist.jsp");
return;
}
//封装对象
Categories c = new Categories();
c.setId(cId);
c.setName(name);
//调用dao层方法
CategoriesDaoImpl dao = new CategoriesDaoImpl();
Boolean b = dao.updateCategory(c);
if(b){
initCategory(request, response);
}
response.sendRedirect("admin/categorylist.jsp");
}
/**
* 初始化分类集合
* @param request
* @param response
*/
private void initCategory(HttpServletRequest request, HttpServletResponse response){
//获取图书分类
CategoriesDaoImpl dao = new CategoriesDaoImpl();
List<Categories> clist = dao.getCategories();
request.getServletContext().setAttribute("clist", clist);
}
}
/**
* Category操作servlet类
int bookId = Integer.parseInt(request.getParameter("bid"));
//提取参数
String title = request.getParameter("title");
Double unitPrice = Double.parseDouble(request.getParameter("unitPrice"));
String author = request.getParameter("author");
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
int publisherId = Integer.parseInt(request.getParameter("publisherId"));
String publishDate = request.getParameter("publishDate");
String isbn = request.getParameter("ISBN");
int wordsCount =Integer.parseInt( request.getParameter("wordsCount"));
String contentDescription = request.getParameter("contentDescription");
String authorDescription = request.getParameter("authorDescription");
//封装对象
Books book = new Books();
book.setId(bookId);
book.setTitle(title);
book.setUnitPrice(unitPrice);
book.setAuthor(author);
book.setCategoryId(categoryId);
book.setPublisherId(publisherId);
book.setPublishDate(publishDate);
book.setIsbn(isbn);
book.setWordsCount(wordsCount);
book.setContentDescription(contentDescription);
book.setAuthorDescription(authorDescription);
//调用dao层方法
BookDaoImpl dao = new BookDaoImpl();
Boolean b = dao.upBook(book);
//设置修改信息结果字符串
if(b)
request.getSession().setAttribute("ADMIN_MSG_BOOK", "信息编辑成功,BID:"+bookId);
else
request.getSession().setAttribute("ADMIN_MSG_BOOK", "信息编辑失败,BID:"+bookId);
showBookByAdmin(request, response);
}
/**
* 添加图书
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void addBookByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(cart!=null){
String isbn = request.getParameter("isbn");
//信息完整性校验
if(isbn!=null && !isbn.equals("")){
//获取ShoppingItem
ShoppingItem si = cart.getItem(isbn);
if(si!=null){
si.inAmount();
}
//计算商品总价
cart.getTotalReal();
request.getSession().setAttribute("cart", cart);
}
}
//页面跳转
showCart(request, response);
}
/**
* 减少购物车商品数量
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void deAmount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从session中获取ShoppingCart
ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("cart");
if(cart!=null){
String isbn = request.getParameter("isbn");
//信息完整性校验
if(isbn!=null && !isbn.equals("")){
//获取ShoppingItem
ShoppingItem si = cart.getItem(isbn);
if(si!=null && si.getAmount()>1)
si.deAmount();
//计算商品总价
cart.getTotalReal();
request.getSession().setAttribute("cart", cart);
}
}
showCart(request, response);
}
}
protected void queryByCategoryName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bname = request.getParameter("value");
BookDaoImpl dao = new BookDaoImpl();
List<Books> searchBList = new ArrayList<Books>();
searchBList = dao.getBooksByCategoryName(bname);
//封装成分页对象
PageBean<Books> Pager = new PageBean<Books>(1,5,searchBList);
request.getSession().setAttribute("searchBooks", Pager);
response.sendRedirect("search.jsp");
}
/**
* 根据图书Isbn获取图书,并且跳转到图书显示页面
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void queryByIsbn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bname = request.getParameter("value");
BookDaoImpl dao = new BookDaoImpl();
List<Books> searchBList = new ArrayList<>();
Books book = dao.getBooksByIsbn(bname);
searchBList.add(book);
//封装成分页对象
PageBean<Books> Pager = new PageBean<Books>(1,5,searchBList);
request.getSession().setAttribute("searchBooks", Pager);
response.sendRedirect("search.jsp");
}
/**
* 根据图书Id获取图书,并且跳转到购买页面
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void buyBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
///获取图书Id
int id =Integer.parseInt(request.getParameter("bid"));
BookDaoImpl dao = new BookDaoImpl();
Books book = dao.getBooksById(id);