基于javaweb的JSP+Servlet美食菜谱分享平台(java+servlet+mysql+jsp)

运行环境

Java≥8、MySQL≥5.7、Tomcat≥8

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

051123492409

061123492409

071123492409

081123492409

091123492409

111123492409

基于javaweb的JSP+Servlet美食菜谱分享平台(java+servlet+mysql+jsp)

启动前先修改上传路径为项目编译后的目录:
yjf.psyd.servlet.CreateRecipeServlet.path

首页:
http://localhost:8080/index

登录:
user1 123456
user2 123456
user3 123456

搜索、查看、收藏、创建菜谱等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
		// 请求转发
req.getRequestDispatcher("/recipeStepRetail").forward(req, resp);
return;

}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}




@WebServlet("/recipeCollection")
public class RecipeCollectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public RecipeCollectionServlet() {
super();
}

// 处理菜谱收藏功能
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 1、获取请求信息
String status = req.getParameter("status");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
        List<String> categorys = new LinkedList<>();
String coverFilePath = null;
List<File> stepFiles = new LinkedList<>();
List<String> stepFilesPath = new LinkedList<>();
User user = null;

// 获取系统当前日期
Date dateNow = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
createDate = dateFormat.format(dateNow);
// System.out.println(createDate);

// 检查前台表单form是否有multipart
if (ServletFileUpload.isMultipartContent(req)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 通过parseRequest解析form中的所有请求字段,并保存到items集合中
List<FileItem> items = null;
try {
items = upload.parseRequest(req);
} catch (FileUploadException e1) {
e1.printStackTrace();
}
// 遍历List中的数据
// Iterator<FileItem> iter = items.iterator();
// while (iter.hasNext()) {
// FileItem item = iter.next();
for (FileItem item : items) {
String itemName = item.getFieldName();
// 判断前台字段是普通form表单字段,还是文件字段
if (item.isFormField()) {
// 普通上传
if (itemName.equals("recTitle")) {
title = item.getString("utf-8");
} else if (itemName.equals("recInfo")) {
info = item.getString("utf-8");
} else if (itemName.equals("recMaterial")) {
material = item.getString("utf-8");
} else if (itemName.equals("recCategory")) {
categorys.add(item.getString("utf-8"));
} else {
if (!"".equals(item.getString("utf-8"))) {
stepInfos.add(item.getString("utf-8"));
} else {
stepInfos.add("");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	public RecipeDetailServlet() {
super();
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1、获取请求信息
String recipeId = req.getParameter("recipeId");
// 获取session对象,用session判断是否收藏
HttpSession hs = req.getSession();
// 把session中的值传到user对象中
User user = (User) hs.getAttribute("user");

// 2、处理请求信息
RecipeService rs = new RecipeServiceImpl();
Recipe r = rs.recipeDetailService(recipeId, user);
// 3、响应请求结果
req.setAttribute("recipe", r);
// 输出r
// System.out.println(r);
// 请求转发
req.getRequestDispatcher("/recipeStepRetail").forward(req, resp);
return;

}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}




@WebServlet("/recipeCollection")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class RecipeCollectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public RecipeCollectionServlet() {
super();
}

// 处理菜谱收藏功能
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 1、获取请求信息
String status = req.getParameter("status");
String RecipeId = req.getParameter("RecipeId");

// 获取session对象
HttpSession hs = req.getSession();
// 把session中的值传到user对象中
User user = (User) hs.getAttribute("user");

RecipeService rs = new RecipeServiceImpl();
// 2、判断status状态
if (status.equals("false")) {
// 3、处理请求结果;插入收藏菜谱
int index = rs.insertCollectionRecipe(RecipeId, user);
if(index>0) {
resp.getWriter().write("{\"index\":" + index + "}");
}
} else {
// 3、处理请求结果:删除收藏菜谱
int index = rs.deleteCollectionRecipe(RecipeId, user);
if(index>0) {
resp.getWriter().write("{\"index\":" + index + "}");
}
}
}

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//		System.out.println("封面上传成功!路径:" + coverFilePath);
// for(File stepFilePath : stepFiles) {
// System.out.println("步骤上传成功!路径:" + stepFilePath);
// }
// for (String stepInfo : stepInfos) {
// System.out.println(stepInfo);
// }
// for (String category : categorys) {
// System.out.println(category);
// }
RecipeService us = new RecipeServiceImpl();
int index = us.createRecipeService(createDate, title, info, material, stepInfos, categorys, coverFilePath,
stepFilesPath, user);
// 响应处理结果
if (index > 0) {
// 弹窗提示,点击后转跳
PrintWriter out = resp.getWriter();
} else {
PrintWriter out = resp.getWriter();
}
}
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
		// 请求转发
req.getRequestDispatcher("/recipeStepRetail").forward(req, resp);
return;

}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}




@WebServlet("/recipeCollection")
public class RecipeCollectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public RecipeCollectionServlet() {
super();
}

// 处理菜谱收藏功能
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 1、获取请求信息
String status = req.getParameter("status");
String RecipeId = req.getParameter("RecipeId");

// 获取session对象
HttpSession hs = req.getSession();
// 把session中的值传到user对象中
User user = (User) hs.getAttribute("user");

RecipeService rs = new RecipeServiceImpl();
// 2、判断status状态
if (status.equals("false")) {
// 3、处理请求结果;插入收藏菜谱
int index = rs.insertCollectionRecipe(RecipeId, user);
if(index>0) {


项目链接:
https://javayms.github.io?id=041123492409201fw
https://javayms.pages.dev?id=041123492409201fw