基于javaweb+mysql的jsp+servlet在线酒类商城系统(java+jsp+bootstrap+servlet+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet在线酒类商城系统(java+jsp+bootstrap+servlet+mysql)
项目介绍
本项目为前后台项目,分为普通用户与管理员两种角色,普通用户登录前台,管理员登录后台;
管理员角色包含以下功能:
管理员登录,会员管理,管理员管理,资讯管理,图片广告管理,商品类别管理,品牌管理,商品管理,库存管理,订单管理,留言管理等功能。
用户角色包含以下功能: 用户首页,登录和注册,收藏商品,查看商品,加入购物车,新增收货地址,提交订单,查看我的订单,资讯查看,留言板等功能。
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本; 6.是否Maven项目:否;
技术栈
JSP+CSS+JavaScript+jquery+bootstrap+servlet+mysql
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven; 若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行; 3. 将项目中src/util/SimpleDataSource.java配置文件中的数据库配置改为自己的配置; 4. 运行项目,输入localhost:8080/jsp_jiu_buy_online 登录 注:Tomcat中配置项目路径必须为jsp_jiu_buy_online,否则会报异常; 管理员账号/密码:admin/admin 用户账号/密码: user/123456
String note = "";
request.setCharacterEncoding("utf-8");
RequestContext requestContext = new ServletRequestContext(
request);
if (FileUpload.isMultipartContent(requestContext)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(request
.getRealPath("/upfile/") + "/"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100 * 1024 * 1024);
List items = new ArrayList();
items = upload.parseRequest(request);
title = ((FileItem) items.get(0)).getString();
title = Info.getUTFStr(title);
note = ((FileItem) items.get(2)).getString();
note = Info.getUTFStr(note);
FileItem fileItem = (FileItem) items.get(1);
if (fileItem.getName() != null && fileItem.getSize() != 0) {
if (fileItem.getName() != null
&& fileItem.getSize() != 0) {
File fullFile = new File(fileItem.getName());
img = Info.generalFileName(fullFile.getName());
File newFile = new File(
request.getRealPath("/upfile/") + "/" + img);
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
}
}
String sql = "insert into news (title,img,note,savetime,type) "
+ "values('" + title + "','" + img + "','" + note
+ "','" + Info.getDateStr() + "','新闻')";
dao.commOper(sql);
request.setAttribute("suc", "操作成功!");
go("/admin/newslist.jsp", request, response);
} catch (Exception e1) {
e1.printStackTrace();
request.setAttribute("error", "");
request.getRequestDispatcher("/admin/newsadd.jsp").forward(
request, response);
}
}
// 编辑新闻
dao.commOper("update news set title='" + title + "',note='" + note
+ "' where id=" + id);
request.setAttribute("suc", "");
go("admin/noticesedit.jsp?id=" + id, request, response);
}
// 新增链接
if (ac.equals("yqlinkadd")) {
String linkname = request.getParameter("linkname");
String linkurl = request.getParameter("linkurl");
dao.commOper("insert into yqlink (linkname,linkurl) "
+ " values ('" + linkname + "','" + linkurl + "')");
request.setAttribute("suc", "操作成功!");
go("admin/yqlink.jsp", request, response);
}
// 编辑公告
if (ac.equals("yqlinkedit")) {
String id = request.getParameter("id");
String linkname = request.getParameter("linkname");
String linkurl = request.getParameter("linkurl");
dao.commOper("update yqlink set linkname='" + linkname
+ "',linkurl='" + linkurl + "' where id=" + id);
request.setAttribute("suc", "操作成功!");
go("admin/yqlink.jsp", request, response);
}
// 网站信息编辑
if (ac.equals("siteinfoedit")) {
String id = request.getParameter("id");
HashMap map = dao.select("select * from siteinfo where id=" + id)
.get(0);
try {
String tel = "";
String addr = "";
String note = "";
String logoimg = map.get("logoimg").toString();
request.setCharacterEncoding("utf-8");
RequestContext requestContext = new ServletRequestContext(
request);
if (FileUpload.isMultipartContent(requestContext)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(request
.getRealPath("/upfile/") + "/"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100 * 1024 * 1024);
List items = new ArrayList();
items = upload.parseRequest(request);
tel = ((FileItem) items.get(0)).getString();
tel = Info.getUTFStr(tel);
addr = ((FileItem) items.get(1)).getString();
addr = Info.getUTFStr(addr);
note = ((FileItem) items.get(3)).getString();
note = Info.getUTFStr(note);
private PageManager() {
}
// 默认一页最大记录数
public static final int DEFAULTPAGESIZE = 20;
// 分页段
public static final int segment = 10;
// 当前页数
protected int currentPage;
// 一页长度
protected int pageSize;
// 总页数
protected long pageNumber;
// 总记录数
protected long count;
// 数据
protected Collection collection;
// 数据查询对象
protected CommDAO dao = new CommDAO();
// 表现层代码
protected String info;
// 请求路径
protected String path;
// 服务器请求对象
protected HttpServletRequest request;
/*
* 仅仅只是加到路径中去
*/
protected String parameter = "";
/**
*
* @param 下一页的分页链接
* @param 一页最大记录数
* @param 当前HttpServletRequest对象
}else{
DATE1 = d1[0]+"-"+d1[1];
}
if(d1[2].length()==1)
{
DATE1 = DATE1+"-0"+d1[2];
}else{
DATE1 = DATE1+"-"+d1[2];
}
String[] d2 = DATE2.split("-");
if(d2[1].length()==1)
{
DATE2 = d2[0]+"-0"+d2[1];
}else{
DATE2 = d2[0]+"-"+d2[1];
}
if(d2[2].length()==1)
{
DATE2 = DATE2+"-0"+d2[2];
}else{
DATE2 = DATE2+"-"+d2[2];
}
for(int j=0;j<10000;j++)
{
i=j;
String gday = Info.getDay(DATE1, j);
if(gday.equals(DATE2))
{
break;
}
}
return i;
}
/**
* 比较时间大小
*
*/
public static String compare_datezq(String DATE1, String DATE2) {
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
String sql = "select * from sysuser where username='" + username
+ "' and userpwd='" + userpwd
+ "' and usertype in ('管理员') ";
List<HashMap> list = dao.select(sql);
if (list.size() == 1) {
session.setAttribute("admin", list.get(0));
gor("/jsp_jiu_buy_online/admin/index.jsp", request, response);
} else {
request.setAttribute("error", "");
go("admin/login.jsp", request, response);
}
}
// 后台退出
if (ac.equals("backexit")) {
session.removeAttribute("admin");
go("admin/login.jsp", request, response);
}
// 新增新闻
if (ac.equals("newsadd")) {
try {
String title = "";
String img = "";
String note = "";
request.setCharacterEncoding("utf-8");
RequestContext requestContext = new ServletRequestContext(
request);
if (FileUpload.isMultipartContent(requestContext)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(request
.getRealPath("/upfile/") + "/"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100 * 1024 * 1024);
List items = new ArrayList();
items = upload.parseRequest(request);
title = ((FileItem) items.get(0)).getString();
title = Info.getUTFStr(title);
note = ((FileItem) items.get(2)).getString();
try {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
// File f = new File(request.getRealPath("/upfile/")+"/"+filename);
//System.out.println(f);
bis = new BufferedInputStream((InputStream)new FileInputStream(request.getRealPath("/upfile/")+"/"+filename));
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
bis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
jscode+="<img style=\"cursor: hand\" οnclick=\"uploadimg()\" src=\"/jsp_jiu_buy_online/js/nopic.jpg\" id=txt height=\""+height+"\"/>";
jscode+="<input type=hidden name=\"filename\" id=\"filename\" value=\"\" />";
return jscode;
}
public static int getkc(String gid){
CommDAO dao = new CommDAO();
int znum = 0;
int innum = 0;
int outnum = 0;
ArrayList<HashMap> inlist = (ArrayList<HashMap>)dao.select("select * from kcrecord where type='in' and gid='"+gid+"' ");
ArrayList<HashMap> outlist = (ArrayList<HashMap>)dao.select("select * from kcrecord where type='out' and gid='"+gid+"' ");
if(inlist.size()>0){
for(HashMap inmap:inlist){
innum += Integer.parseInt(inmap.get("happennum").toString());//总入库量
}
}
if(outlist.size()>0){
for(HashMap outmap:outlist){
outnum += Integer.parseInt(outmap.get("happennum").toString());//总出库量
}
}
znum = innum - outnum;//库存量
return znum;
}
}
public class PageManager {
private PageManager() {
}
out.print("false");
} else {
out.print("true");
}
}
// 检查商品的库存
if (ac.equals("checkgoodkc")) {
String gid = request.getParameter("gid");
String sl = request.getParameter("sl");
if (Integer.valueOf(sl) > Info.getkc(gid)) {
out.write("1");
} else {
out.write("0");
}
}
// 商品加入购物车
if (ac.equals("tocar")) {
String gid = request.getParameter("gid");
int sl = Integer.valueOf(request.getParameter("sl"));
if (member != null) {
String mid = member.get("id").toString();
// 检查该人的购物车是否有该物品
ArrayList<HashMap> cklist = (ArrayList<HashMap>) dao
.select("select * from car where mid='" + mid
+ "' and gid='" + gid + "'");
if (cklist.size() > 0) {
dao.commOper("update car set sl=sl+" + sl + " where mid='"
+ mid + "' and gid='" + gid + "' ");
} else {
dao.commOper("insert into car (gid,sl,mid) values ('" + gid
+ "','" + sl + "','" + mid + "')");
}
out.print("true");
} else {
out.print("false");
}
// request.setAttribute("suc", "");
// go("/tocar.jsp?gid="+gid, request, response);
}
.select("select * from sysuser where username='" + username
+ "' and delstatus='0' ");
if (cklist.size() > 0) {
out.write("1");
} else {
out.write("0");
}
}
// 新增管理员
if (ac.equals("sysuseradd")) {
String usertype = "管理员";
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
String realname = request.getParameter("realname");
String sex = request.getParameter("sex");
String idcard = request.getParameter("idcard");
String tel = request.getParameter("tel");
String email = request.getParameter("email");
String addr = request.getParameter("addr");
String delstatus = "0";
String savetime = Info.getDateStr();
dao.commOper("insert into sysuser (usertype,username,userpwd,realname,sex,idcard,tel,email,addr,delstatus,savetime)"
+ " values ('"
+ usertype
+ "','"
+ username
+ "','"
+ userpwd
+ "','"
+ realname
+ "','"
+ sex
+ "','"
+ idcard
+ "','"
+ tel
+ "','"
+ email
+ "','"
+ addr
+ "','"
+ delstatus
+ "','"
+ savetime + "')");
request.setAttribute("suc", "");
go("/admin/sysuseradd.jsp", request, response);
e1.printStackTrace();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//下面开始添加单元格
int i=1;
System.out.println(list);
for(HashMap t:list){
try {
Iterator it = t.entrySet().iterator();
int jj=0;
while(it.hasNext())
{
Label label1 = new Label(jj, i,"");
String a = it.next().toString().replaceAll("null", " abc ");
if(a.split("=").length==1)a=a+"abc";
System.out.println(a);
label1.setString(a.split("=")[1]);
ws.addCell(label1);
jj++;
}
i++;
} catch (RowsExceededException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
//从内存中写入文件中
wwb.write();
//关闭资源,释放内存
wwb.close();
} catch (IOException e) {
e.printStackTrace();
.getParameter("addr");
String idcard = request.getParameter("idcard") == null ? ""
: request.getParameter("idcard");
String tel = request.getParameter("tel") == null ? "" : request
.getParameter("tel");
String delstatus = "0";
String savetime = Info.getDateStr();
dao.commOper("update sysuser set userpwd='" + userpwd + "',email='"
+ email + "',realname='" + realname + "'," + "sex='" + sex
+ "',addr='" + addr + "',idcard='" + idcard + "',tel='"
+ tel + "' where id=" + id);
request.setAttribute("suc", "操作成功!");
go("/admin/userlist.jsp", request, response);
}
if (ac.equals("pwdedit")) {
String oldpwd = request.getParameter("oldpwd");
String newpwd = request.getParameter("newpwd");
HashMap oldmap = dao.select(
"select * from sysuser where id=" + admin.get("id")).get(0);
if (oldpwd.equals(oldmap.get("userpwd"))) {
dao.commOper("update sysuser set userpwd = '" + newpwd
+ "' where id=" + admin.get("id"));
request.setAttribute("info", "密码修改成功!");
} else {
request.setAttribute("info", "旧密码不正确!");
}
go("/admin/myaccount.jsp", request, response);
}
if (ac.equals("aboutedit")) {
String lxr = request.getParameter("lxr");
String tel = request.getParameter("tel");
String addr = request.getParameter("addr");
String note = request.getParameter("note");
dao.commOper("update about set lxr='" + lxr + "',tel='" + tel
+ "',addr='" + addr + "',note='" + note + "' where id=1");
request.setAttribute("suc", "操作成功!");
go("/admin/aboutedit.jsp", request, response);
}
if (ac.equals("msgadd")) {
String msg = request.getParameter("msg");
if (fileItem.getName() != null
&& fileItem.getSize() != 0) {
File fullFile = new File(fileItem.getName());
logoimg = Info.generalFileName(fullFile.getName());
File newFile = new File(
request.getRealPath("/upfile/") + "/"
+ logoimg);
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
}
}
String sql = "update siteinfo set tel='" + tel + "',addr='"
+ addr + "',note='" + note + "',logoimg='" + logoimg
+ "' where id=" + id;
dao.commOper(sql);
request.setAttribute("suc", "");
go("/admin/siteinfo.jsp?id=" + id, request, response);
} catch (Exception e1) {
e1.printStackTrace();
request.setAttribute("error", "");
request.getRequestDispatcher("/admin/siteinfo.jsp?id=" + id)
.forward(request, response);
}
}
// 检查用户名唯一性AJAX
if (ac.equals("sysuserscheck")) {
String username = request.getParameter("username");
ArrayList cklist = (ArrayList) dao
.select("select * from sysuser where username='" + username
+ "' and delstatus='0' ");
if (cklist.size() > 0) {
out.write("1");
} else {
out.write("0");
}
}
// 新增管理员
if (ac.equals("sysuseradd")) {
if (this.currentPage == 1)
buf.append("首页");
else
buf.append("<a href='").append(this.path).append("¤tPage=1")
.append(parameter)
.append("' class='ls'>").append("首页")
.append("</a>");
// #1157B7
buf.append(" ");
if (this.currentPage > 1) {
buf.append("<a href='").append(this.path).append("¤tPage=")
.append(currentPage - 1).append(parameter).append(
"' class='ls'>").append("上页")
.append("</a>");
} else {
buf.append("上页");
}
buf.append(" ");
int currentSegment = this.currentPage % segment == 0 ? this.currentPage
/ segment : this.currentPage / segment + 1;
/*for (int i = 1; i <= this.pageNumber; i++) {
if (this.currentPage == i)
buf.append("<font color='red'>").append(i).append("</font>");
else
buf.append("<a href='").append(this.path).append(
"¤tPage=").append(i).append(parameter).append(
"' class='ls'>[").append(i).append(
"]</a>");
}*/
buf.append(" ");
if (this.currentPage < this.pageNumber) {
buf.append("<a href='").append(this.path).append("¤tPage=")
.append(currentPage + 1).append(parameter).append(
"' class='ls'>").append("下页")
.append("</a>");
* 如没有条件只是列表就不使用这个方法
*/
public void addParameter(List parameter) {
StringBuffer para = new StringBuffer("");
if (parameter != null && parameter.size() != 0) {
Iterator iterator = parameter.iterator();
while (iterator.hasNext()) {
para.append("&").append(iterator.next().toString());
}
}
this.parameter = para.toString();
}
/**
* 刷新分页路径
*
*/
protected void refreshUrl() {
StringBuffer buf = new StringBuffer();
buf.append("<font color='#1157B7'>共").append(count);
buf.append("条");
buf.append(" ");
buf.append("第").append(this.currentPage).append("/").append(
this.pageNumber).append("页");
buf.append(" ");
if (this.currentPage == 1)
buf.append("首页");
else
buf.append("<a href='").append(this.path).append("¤tPage=1")
.append(parameter)
.append("' class='ls'>").append("首页")
.append("</a>");
// #1157B7
buf.append(" ");
if (this.currentPage > 1) {
buf.append("<a href='").append(this.path).append("¤tPage=")
.append(currentPage - 1).append(parameter).append(
"' class='ls'>").append("上页")
.append("</a>");
} else {
buf.append("上页");
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100 * 1024 * 1024);
List items = new ArrayList();
items = upload.parseRequest(request);
FileItem fileItem = (FileItem) items.get(0);
if (fileItem.getName() != null && fileItem.getSize() != 0) {
if (fileItem.getName() != null
&& fileItem.getSize() != 0) {
File fullFile = new File(fileItem.getName());
filename = Info.generalFileName(fullFile.getName());
File newFile = new File(
request.getRealPath("/upfile/") + "/"
+ filename);
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
}
}
dao.commOper("insert into imgadv (filename) " + "values ('"
+ filename + "') ");
request.setAttribute("suc", "操作成功!");
go("/admin/imgadv.jsp", request, response);
} catch (Exception e1) {
e1.printStackTrace();
request.setAttribute("error", "");
request.getRequestDispatcher("/admin/imgadv.jsp").forward(
request, response);
}
}
// 商品品牌
if (ac.equals("ppinfoadd")) {
String ppname = request.getParameter("ppname");
String delstatus = "0";
dao.commOper("insert into ppinfo (ppname,delstatus) values ('"
+ ppname + "','" + delstatus + "')");
request.setAttribute("suc", "操作成功!");
go("/admin/ppinfo.jsp", request, response);
}
// 商品品牌编辑
public class MainCtrl extends HttpServlet {
public MainCtrl() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
MainMethod responses = new MainMethod();
public void go(String url, HttpServletRequest request,
HttpServletResponse response) {
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void gor(String url, HttpServletRequest request,
HttpServletResponse response) {
try {
response.sendRedirect(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
private static Properties config = null;
static {
}
//
// public void init() throws ServletException {
// // Put your code here
// try {
// responses.getClassLoader((String) config.get("pid"));
// } catch (UnknownHostException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (SocketException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
public static void main(String[] args) {
System.out.println(new CommDAO().select("select * from mixinfo"));
}
}
String delstatus = "0";
String savetime = Info.getDateStr();
dao.commOper("insert into sysuser (usertype,username,userpwd,realname,sex,idcard,tel,email,addr,delstatus,savetime)"
+ " values ('"
+ usertype
+ "','"
+ username
+ "','"
+ userpwd
+ "','"
+ realname
+ "','"
+ sex
+ "','"
+ idcard
+ "','"
+ tel
+ "','"
+ email
+ "','"
+ addr
+ "','"
+ delstatus
+ "','"
+ savetime + "')");
request.setAttribute("suc", "");
go("/admin/sysuseradd.jsp", request, response);
}
// 编辑管理员
if (ac.equals("sysuseredit")) {
String id = request.getParameter("id");
String userpwd = request.getParameter("userpwd");
String realname = request.getParameter("realname");
String sex = request.getParameter("sex");
String idcard = request.getParameter("idcard");
String tel = request.getParameter("tel");
String email = request.getParameter("email");
String addr = request.getParameter("addr");
dao.commOper("update sysuser set userpwd='" + userpwd
+ "',realname='" + realname + "',sex='" + sex
+ "',idcard='" + idcard + "',tel='" + tel + "',email='"
+ email + "',addr='" + addr + "' where id=" + id);
request.setAttribute("suc", "");
go("/admin/sysuseredit.jsp?id=" + id, request, response);
}
// 商品类别新增
if (ac.equals("protypeadd")) {
String typename = request.getParameter("typename");
String fatherid = request.getParameter("fatherid");
dao.commOper("insert into protype (typename,fatherid,delstatus) values ('"
+ typename + "','" + fatherid + "','0') ");
request.setAttribute("suc", "操作成功!");
go("/admin/protype.jsp", request, response);
}