基于javaweb+mysql的jsp+servlet汉服商城系统(java+servlet+jsp+jdbc+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet汉服商城系统(java+servlet+jsp+jdbc+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/8.0等版本均可;
6.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
技术栈
Servlet、JSP、JDBC、MySQL5.7、Tomcat8
使用说明
-
使用Navicat或者其它工具,在mysql中创建对应sql文件名称的数据库,并导入项目的sql文件;
-
使用IDEA/Eclipse/MyEclipse导入项目;
-
将项目中util/SimpleDataSource.java配置文件中的数据库配置改为自己的配置;
-
运行项目,在浏览器中输入http://localhost:8080/jsp_hfzl_shop 登录 注:tomcat中配置项目路径必须为jsp_hfzl_shop
用户账号/密码: user/123456
管理员账号/密码:admin/admin
HttpServletResponse response) {
try {
response.sendRedirect(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
HashMap admin = (HashMap) session.getAttribute("admin");
HashMap member = (HashMap) session.getAttribute("member");
String ac = request.getParameter("ac");
if (ac == null)
ac = "";
CommDAO dao = new CommDAO();
String date = Info.getDateStr();
String today = date.substring(0, 10);
String tomonth = date.substring(0, 7);
// 登录
if (ac.equals("login")) {
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_hfzl_shop/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);
}
// 新增新闻
* @return
*/
public static String getDay(String date,int day) {
String b = date.substring(0,10);
String c = b.substring(0,4);
String d = b.substring(5,7);
String f = b.substring(8,10);
String aa = c+"/"+d+"/"+f;
String a = "";
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
GregorianCalendar grc=new GregorianCalendar();
grc.setTime(new Date(aa));
grc.add(GregorianCalendar.DAY_OF_MONTH,day);
String resu = dateFormat.format(grc.getTime());
String t[]= resu.split("-");
String sesuu = "";
for(int i=0;i<t.length;i++)
{
if(t[i].length()==1)
{
t[i]="0"+t[i];
}
sesuu += t[i]+"-";
}
return sesuu.substring(0,10);
}
/**
* 计算两个时期之间的天数
*
*/
public static int dayToday(String DATE1, String DATE2) {
int i = 0;
DATE1 = DATE1.substring(0,DATE1.indexOf(" "));
DATE2 = DATE2.substring(0,DATE2.indexOf(" "));
String[] d1 = DATE1.split("-");
if(d1[1].length()==1)
{
DATE1 = d1[0]+"-0"+d1[1];
}else{
DATE1 = d1[0]+"-"+d1[1];
}
if(d1[2].length()==1)
{
request.getRequestDispatcher("/admin/imgadvadd.jsp").forward(
request, response);
}
}
// 编辑图片
if (ac.equals("imgadvedit")) {
String id = request.getParameter("id");
HashMap map = dao.select("select * from imgadv where id=" + id)
.get(0);
try {
String img = map.get("filename").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);
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());
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 = "update imgadv set filename='" + img
+ "' where id=" + id;
dao.commOper(sql);
request.setAttribute("suc", "");
go("/admin/imgadvedit.jsp?id=" + id, request, response);
} catch (Exception e1) {
e1.printStackTrace();
request.setAttribute("error", "");
request.getRequestDispatcher("/admin/imgadvedit.jsp?id=" + id)
.forward(request, response);
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuffer sb = new StringBuffer(50);
response.setContentType("application/x-msdownload;charset=utf-8");
try {
response.setHeader("Content-Disposition", new String(sb.toString()
.getBytes(), "ISO8859-1"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String filename = request.getParameter("filename");
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
try {
filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){
try {
filename = URLEncoder.encode(filename, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
dao.commOper("insert into goods (goodno,goodname,fid,sid,price,note,savetime,shstatus,filename,istj,tprice,delstatus,salestatus,goodpp) "
+ "values ('"
+ goodno
+ "','"
+ goodname
+ "','"
+ fid
+ "','"
+ sid
+ "','"
+ price
+ "','"
+ note
+ "','"
+ savetime
+ "','通过','"
+ filename
+ "','"
+ istj
+ "','"
+ tprice
+ "','0','在售','" + goodpp + "') ");
request.setAttribute("suc", "操作成功!");
go("/admin/goodsgl.jsp", request, response);
} catch (Exception e1) {
e1.printStackTrace();
request.setAttribute("error", "");
request.getRequestDispatcher("/admin/goodsgl.jsp").forward(
request, response);
}
}
// 会员修改商品
if (ac.equals("goodsedit")) {
String id = request.getParameter("id");
HashMap map = dao.select("select * from goods where id=" + id).get(
0);
try {
String goodname = "";
String fid = "";
String sid = "";
String goodpp = "";
String price = "";
String note = "";
String filename = map.get("filename").toString();
request.setCharacterEncoding("utf-8");
RequestContext requestContext = new ServletRequestContext(
request);
+ istj
+ "','"
+ tprice
+ "','0','在售','" + goodpp + "') ");
request.setAttribute("suc", "操作成功!");
go("/admin/goodsgl.jsp", request, response);
} catch (Exception e1) {
e1.printStackTrace();
request.setAttribute("error", "");
request.getRequestDispatcher("/admin/goodsgl.jsp").forward(
request, response);
}
}
// 会员修改商品
if (ac.equals("goodsedit")) {
String id = request.getParameter("id");
HashMap map = dao.select("select * from goods where id=" + id).get(
0);
try {
String goodname = "";
String fid = "";
String sid = "";
String goodpp = "";
String price = "";
String note = "";
String filename = map.get("filename").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);
goodname = ((FileItem) items.get(0)).getString();
goodname = Info.getUTFStr(goodname);
fid = ((FileItem) items.get(1)).getString();
fid = Info.getUTFStr(fid);
sid = ((FileItem) items.get(2)).getString();
sid = Info.getUTFStr(sid);
goodpp = ((FileItem) items.get(3)).getString();
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>");
} else {
buf.append("下页");
}
buf.append(" ");
if (this.currentPage == this.pageNumber)
buf.append("末页 ");
else
buf.append("<a href='").append(this.path).append("¤tPage=")
.append(this.pageNumber).append(parameter).append(
"' class='ls'>").append("末页")
.append("</a></font> ");
//
// for (int i = 0; i < this.pageNumber; i++) {
// if (this.currentPage == i + 1) {
// buf.append("<font color=red>[" + (i + 1) + "]</font>").append(
// " ");
// } else {
// buf.append("<a href='").append(this.path).append(
// "¤tPage=").append(i + 1).append(parameter)
// .append("' style='TEXT-DECORATION:none'>").append(
// "[" + (i + 1) + "]").append("</a> ");
// }
// }
buf.append("<select class='input-xlarge' οnchange=\"javascript:window.location='").append(
if (this.count != 0) {
this.pageNumber = count % this.pageSize == 0 ? this.count
/ this.pageSize : this.count / this.pageSize + 1;
if (this.currentPage > this.pageNumber)
this.currentPage = (int) this.pageNumber;
}
this.request.getSession().setAttribute("currentPage",
String.valueOf(this.currentPage));
this.collection = this.dao.select(hql,
this.currentPage , this.pageSize);
this.refreshUrl();
}
/**
*
* @param 查询条件集合
* 如没有条件只是列表就不使用这个方法
*/
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("条");
sub2=source.substring(end,source.length());
sourceLowcase=subLowcase1+subLowcase2;
source=sub1+sub2;
}
//System.out.println(start+" "+end);
}
return source.replaceAll(" ", "");
}
public static void delPic(String path,String img)
{
if(img!=null)
{
if(!img.equals(""))
{
File file1=new File(path + "/" + img);
if(file1.exists() ) {
file1.deleteOnExit();
// file1.delete();
}}}
}
public static void writeExcel(String fileName,String[] pros,java.util.List<HashMap> list,HttpServletRequest request, HttpServletResponse response){
WritableWorkbook wwb = null;
try {
//首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
wwb = Workbook.createWorkbook(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
if(wwb!=null){
//创建一个可写入的工作表
//Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet("sheet1", 0);
try {
for(int i=0;i<pros.length;i++)
{
Label label1 = new Label(i, 0,"");
label1.setString(pros[i]);
ws.addCell(label1);
}
} catch (RowsExceededException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (cklist.size() > 0) {
session.setAttribute("member", cklist.get(0));
go("/index.jsp", request, response);
} else {
request.setAttribute("no", "用户名或密码错误!");
go("/login.jsp", request, response);
}
}
// 前台退出
if (ac.equals("frontexit")) {
session.removeAttribute("member");
go("/index.jsp", request, response);
}
// 发布商品
if (ac.equals("goodsadd")) {
try {
String goodno = Info.getAutoNo();
String goodname = "";
String fid = "";
String sid = "";
String goodpp = "";
String price = "";
String filename = "";
String note = "";
String shstatus = "通过";
String istj = "no";
String savetime = Info.getDateStr();
String tprice = "";
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();
try{//yyyyMMddHHmmss
java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat("MMddHHmmss");
java.util.Date currentTime_1=new java.util.Date();
dateString=formatter.format(currentTime_1);
}catch(Exception e){
}
return dateString;
}
public static void main(String[] g )
{
//System.out.print(Info.getDateStr());
//System.out.println(a);
}
public static String getUTFStr(String str) {
if(str==null){
return "";
}
try {
str = new String(str.getBytes("ISO-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public static String getGBKStr(String str) throws UnsupportedEncodingException{
if(str==null){
return "";
}
return new String(str.getBytes("ISO-8859-1"),"GBK");
}
public static String getGB2312Str(String str) throws UnsupportedEncodingException{
if(str==null){
return "";
}
return new String(str.getBytes("ISO-8859-1"),"gb2312");
}
}
public static void writeExcel(String fileName,String[] pros,java.util.List<HashMap> list,HttpServletRequest request, HttpServletResponse response){
WritableWorkbook wwb = null;
try {
//首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
wwb = Workbook.createWorkbook(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
if(wwb!=null){
//创建一个可写入的工作表
//Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet("sheet1", 0);
try {
for(int i=0;i<pros.length;i++)
{
Label label1 = new Label(i, 0,"");
label1.setString(pros[i]);
ws.addCell(label1);
}
} catch (RowsExceededException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//下面开始添加单元格
int i=1;
System.out.println(list);
for(HashMap t:list){
try {
try {
this.currentPage = Integer.parseInt((String) request
.getSession().getAttribute("currentPage"));
} catch (Exception e1) {
this.currentPage = 1;
}
}
}
/**
*
* @param 下一页的分页链接
* @param 一页最大记录数
* @param 当前HttpServletRequest对象
* @param 数据库操作对象
*/
public static PageManager getPage(String path, int pageSize,
HttpServletRequest request) {
return new PageManager(path, pageSize, request);
}
/**
*
*
* @param hql语句
*
*/
public void doList(String hql) {
String sql = "select count(*) "+hql.substring(hql.indexOf("from"));
sql = sql.substring(0,sql.indexOf("order"));
this.count = this.dao.getInt(sql);
// 检查用户名唯一性AJAX 会员注册
if (ac.equals("memberunamecheck")) {
String uname = request.getParameter("username");
ArrayList cklist = (ArrayList) dao
.select("select * from member where uname='" + uname
+ "' and delstatus='0' ");
if (cklist.size() > 0) {
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);
}
// 直接购买
/**
* 比较时间大小
*
*/
public static String compare_datezq(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
return "big";
} else if (dt1.getTime() < dt2.getTime()) {
return "small";
} else {
return "den";
}
} catch (Exception exception) {
exception.printStackTrace();
}
return "den";
}
/**
* 过滤html代码
*
*/
public static String filterStrIgnoreCase(String source, String from, String to){
String sourceLowcase=source.toLowerCase();
String sub1,sub2,subLowcase1,subLowcase2;
sub1=sub2=subLowcase1=subLowcase2="";
int start=0,end;
boolean done=true;
if(source==null) return null;
if(from==null||from.equals("")||to==null||to.equals(""))
return source;
while(done){
start=sourceLowcase.indexOf(from,start);
if(start==-1) {
break;
}
end=sourceLowcase.indexOf("/>",start);
if(end==-1) {
done=false;
}
}
else{
end=end+to.length();
subLowcase2=sourceLowcase.substring(end,source.length());
sub2=source.substring(end,source.length());
sourceLowcase=subLowcase1+subLowcase2;
source=sub1+sub2;
}
//System.out.println(start+" "+end);
}
return source.replaceAll(" ", "");
}
public static void delPic(String path,String img)
{
if(img!=null)
{
if(!img.equals(""))
{
File file1=new File(path + "/" + img);
if(file1.exists() ) {
file1.deleteOnExit();
// file1.delete();
}}}
}
public static void writeExcel(String fileName,String[] pros,java.util.List<HashMap> list,HttpServletRequest request, HttpServletResponse response){
WritableWorkbook wwb = null;
try {
//首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
wwb = Workbook.createWorkbook(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
String email = request.getParameter("email") == null ? "" : request
.getParameter("email");
String tname = request.getParameter("tname") == null ? "" : request
.getParameter("tname");
String sex = request.getParameter("sex") == null ? "" : request
.getParameter("sex");
String addr = request.getParameter("addr") == null ? "" : request
.getParameter("addr");
String ybcode = request.getParameter("ybcode") == null ? ""
: request.getParameter("ybcode");
String qq = request.getParameter("qq") == null ? "" : request
.getParameter("qq");
String tel = request.getParameter("tel") == null ? "" : request
.getParameter("tel");
dao.commOper("update member set upass='" + upass + "',email='"
+ email + "',tname='" + tname + "',sex='" + sex
+ "',addr='" + addr + "',ybcode='" + ybcode + "',qq='" + qq
+ "',tel='" + tel + "' where id=" + id);
request.setAttribute("suc", "会员信息修改成功!");
go("/grinfo.jsp", request, response);
}
// 会员登录
if (ac.equals("frontlogin")) {
String uname = request.getParameter("uname");
String upass = request.getParameter("upass");
ArrayList cklist = (ArrayList) dao
.select("select * from member where uname='" + uname
+ "' and upass='" + upass + "' and delstatus='0'");
if (cklist.size() > 0) {
session.setAttribute("member", cklist.get(0));
go("/index.jsp", request, response);
} else {
request.setAttribute("no", "用户名或密码错误!");
go("/login.jsp", request, response);
}
}
// 前台退出
if (ac.equals("frontexit")) {
session.removeAttribute("member");
go("/index.jsp", request, response);
}
String num = request.getParameter("num");
String type = request.getParameter("type");
String reason = request.getParameter("reason");
String savetime = Info.getDateStr();
int znum = 0;
int innum = 0;
int outnum = 0;
ArrayList<HashMap> inlist = (ArrayList<HashMap>) dao
.select("select * from kcrecord where type='in' and pid='"
+ pid + "' ");
ArrayList<HashMap> outlist = (ArrayList<HashMap>) dao
.select("select * from kcrecord where type='out' and pid='"
+ pid + "' ");
if (inlist.size() > 0) {
for (HashMap inmap : inlist) {
innum += Integer.parseInt(inmap.get("num").toString());// 总入库量
}
}
if (outlist.size() > 0) {
for (HashMap outmap : outlist) {
outnum += Integer.parseInt(outmap.get("num").toString());// 总出库量
}
}
znum = innum - outnum;// 库存量
if (Integer.parseInt(num) > znum) {
request.setAttribute("no", "");
go("/admin/kcout.jsp", request, response);
} else {
dao.commOper("insert into kcrecord (pid,num,type,reason,savetime) values"
+ " ('"
+ pid
+ "','"
+ Integer.parseInt(num)
+ "','"
+ type + "','" + reason + "','" + savetime + "') ");
request.setAttribute("suc", "");
go("/admin/kcout.jsp", request, response);
}
}
// 新增图片