基于javaweb+mysql的jsp+servlet电影票售票管理系统(java+jsp+servlet+javabean+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
后台:管理用户、影片、排片、影厅、订单、影评等
前台:注册登录、查看排片、详情,选座、购票等
后台
前台
技术框架
JSP Servlet MySQL C3P0 DBUtil Tomcat JavaScript CSS
基于javaweb+mysql的JSP+Servlet电影票售票管理系统(java+jsp+servlet+javabean+mysql)
/**
* 描述: 写影评
*/
@WebServlet(urlPatterns = "/evaluate")
public class WriteEvaluateServlet extends BaseServlet {
private static final long serialVersionUID = -3514964050010797273L;
private EvaluateService evaluateService = BaseFactory.getService(EvaluateService.class);
private MoviceService moviceService = BaseFactory.getService(MoviceService.class);
private ReplyService replyService = BaseFactory.getService(ReplyService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String movieName = req.getParameter("movie");
if (movieName == null) {
resp.sendRedirect(req.getContextPath() + "/");
} else {
Movie movie = moviceService.queryByName(movieName);
String evaId = req.getParameter("eva_id");
User user = (User) req.getSession().getAttribute("user");
if (movie == null || evaId == null) {
resp.sendRedirect(req.getContextPath() + "/");
} else if (!evaId.isEmpty()) {
//检查提交的是否相符
Evaluate eva = evaluateService.query(evaId);
if (eva != null) {
String content = req.getParameter("evaluate_content");
//截取回复内容里的昵称
Pattern pattern = Pattern.compile("\\[回复\\].+\\[/回复\\]");
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
String temp = matcher.group();
String nick = temp.replace("[回复]", "")
.replace("[/回复]", "");
if (eva.nickname.equals(nick)) {
Reply reply = new Reply();
reply.username = user.username;
reply.nickname = user.nickname;
reply.evaluate_id = Integer.valueOf(evaId);
reply.time = System.currentTimeMillis();
reply.content = content.substring(temp.length()).trim();
replyService.add(reply);
req.getSession().setAttribute("lookMovie", movie.name);
resp.sendRedirect(req.getContextPath() + "/movie/detail");
} else {
evaluate(req, resp, movieName, movie, user);
}
} else {
evaluate(req, resp, movieName, movie, user);
/**
* 功能: 排片管理
*/
@WebServlet(urlPatterns = {"/admin/plans", "/admin/plans/add", "/admin/plans/cancel"})
public class PlanMgrServlet extends BaseServlet {
private static final long serialVersionUID = -6874708411049815744L;
private PlanService planService = BaseFactory.getService(PlanService.class);
private HallService hallService = BaseFactory.getService(HallService.class);
private MoviceService moviceService = BaseFactory.getService(MoviceService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String servletPath = req.getServletPath();
if (servletPath.contains("cancel")) {//取消排片
doDel(req, resp);
} else if (servletPath.contains("add")) {//执行添加
doAdd(req, resp);
} else {
String keyword = req.getParameter("keyword");
if (Utils.isEmpty(keyword)) {
backListToPlansJsp(planService.queryAll(), req, resp);
} else {
}
}
public static void fillAttr(HttpServletRequest req, String key, String value) {
if (value != null) {
req.setAttribute(key, value);
}
}
public static void fillAttr(HttpServletRequest req, Object bean) {
try {
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(bean);
if (value != null) {
req.setAttribute(field.getName(), value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String generateImgFileName(String fileName) {
// 获得扩展名
int beginIndex = fileName.lastIndexOf(".");
String ext = "";
if (beginIndex != -1) {
ext = fileName.substring(beginIndex);
}
return MD5Utils.getMD5Code(UUID.randomUUID().toString()) + ext;
}
public static int compare(Object o1, String s1, Object o2, String s2) {
if (o2 == null || s2 == null)
return 1;
if (o1 == null || s1 == null)
return -1;
String s3 = CharacterParser.getSelling(s1);
String s4 = CharacterParser.getSelling(s2);
return s3.compareTo(s4);
}
}
package com.demo.web;
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String servletPath = req.getServletPath();
if (servletPath.contains("delete")) {//执行删除
doDel(req, resp);
} else if (servletPath.contains("add")) {//添加影厅
doAdd(req, resp);
} else {
forwardToHallsJsp(req, resp);
}
}
private void forwardToHallsJsp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Hall> list = service.queryAll();
if (list != null) {
req.setAttribute("halls", list);
}
req.setAttribute("chkItem", 3);
req.getRequestDispatcher("/admin/halls.jsp").forward(req, resp);
}
private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Hall hall = new Hall();
Utils.populateToBean(hall, req.getParameterMap());
try {
service.add(hall);
resp.sendRedirect(req.getContextPath() + "/admin/halls");
} catch (MyException e) {
req.setAttribute("msg", e.getMessage());
Utils.fillAttr(req, hall);
forwardToHallsJsp(req, resp);
}
}
private void doDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String hallName = req.getParameter("hall");
//如果带id参数,则是删除此影厅
if (hallName != null) {
service.deleteByName(hallName);
}
resp.sendRedirect(req.getContextPath() + "/admin/halls");
}
}
} else if (fileName != null && !fileName.equals(m.pic_name)) {
new File(uploadPath, m.pic_name).delete();
}
}
resp.sendRedirect(req.getContextPath() + "/admin/movies");
} catch (MyException e) {
req.setAttribute("msg", e.getMessage());
//将原来填写的返回
Utils.fillAttr(req, map, "name");
Utils.fillAttr(req, map, "director");
Utils.fillAttr(req, map, "protagonist");
Utils.fillAttr(req, map, "region");
Utils.fillAttr(req, map, "language");
Utils.fillAttr(req, map, "type");
Utils.fillAttr(req, map, "duration");
Utils.fillAttr(req, map, "synopsis");
if (add) {
req.setAttribute("add", true);
} else {
req.setAttribute("add", false);
Utils.fillAttr(req, map, "id");
}
req.getRequestDispatcher("/admin/add_update_movie.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private void forwardToMoviesJsp(List<Movie> list, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (list != null) {
req.setAttribute("movies", list);
}
req.setAttribute("chkItem", 2);
req.getRequestDispatcher("/admin/movies.jsp").forward(req, resp);
}
}
package com.demo.util;
HttpSession session = req.getSession();
String sessionValicateCode = (String) session.getAttribute("valicateCode");
if (valicateCode == null || !valicateCode.equalsIgnoreCase(sessionValicateCode)) {
backError(req, resp, user, confirmPwd, "验证码不正确");
return;
}
//检查信息并存到数据库
user = service.registerUser(user, confirmPwd);
//将用户添加到session
session.setAttribute("user", user);
//重定向回主页
resp.sendRedirect(req.getContextPath() + "/");
} catch (MyException e) {
backError(req, resp, user, confirmPwd, e.getMessage());
}
}
}
private void backError(HttpServletRequest req, HttpServletResponse resp, User user, String confirmPwd, String msg) throws ServletException, IOException {
req.setAttribute("msg", msg);
req.setAttribute("regUser", user);
Utils.fillAttr(req, "confirmPwd", confirmPwd);
req.getRequestDispatcher("/register.jsp").forward(req, resp);
}
}
package com.demo.web;
/**
* 功能: 影厅管理
*/
@WebServlet(urlPatterns = {"/admin/halls", "/admin/halls/add", "/admin/halls/delete"})
public class HallMgrServlet extends BaseServlet {
private static final long serialVersionUID = 6375850281387346655L;
private HallService service = BaseFactory.getService(HallService.class);
@Override
/**
* 功能: 影片管理
*/
@WebServlet(urlPatterns = {"/admin/movies", "/admin/movies/add", "/admin/movies/update", "/admin/movies/delete"})
public class MovieMgrServlet extends BaseServlet {
private static final long serialVersionUID = 2225249275913675741L;
private MoviceService service = BaseFactory.getService(MoviceService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//根据url判断是做什么操作
String servletPath = req.getServletPath();
if (servletPath.contains("add")) {
doAddMovie(req, resp);
} else if (servletPath.contains("update")) {
doUpdateMovie(req, resp);
} else if (servletPath.contains("delete")) {
doDel(req, resp);
} else {
String keyword = req.getParameter("keyword");
if (Utils.isEmpty(keyword)) {
backUserList(service.queryAll(), req, resp);
} else {
req.setAttribute("keyword", keyword);
backUserList(service.queryUsersByKeyword(keyword), req, resp);
}
}
}
private void doDel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String name = req.getParameter("name");
if (name != null) {
service.deleteByUsername(name);
}
resp.sendRedirect(req.getContextPath() + "/admin");
}
private void backUserList(List<User> list, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (list != null) {
req.setAttribute("users", list);
}
req.setAttribute("chkItem", 1);
req.getRequestDispatcher("/admin/users.jsp").forward(req, resp);
}
}
package com.demo.web;
/**
* 描述:
*/
@WebServlet(urlPatterns = "/pay")
public class PayServlet extends BaseServlet {
/**
* 描述: 写影评
*/
@WebServlet(urlPatterns = "/evaluate")
public class WriteEvaluateServlet extends BaseServlet {
private static final long serialVersionUID = -3514964050010797273L;
private EvaluateService evaluateService = BaseFactory.getService(EvaluateService.class);
private MoviceService moviceService = BaseFactory.getService(MoviceService.class);
private ReplyService replyService = BaseFactory.getService(ReplyService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String movieName = req.getParameter("movie");
if (movieName == null) {
resp.sendRedirect(req.getContextPath() + "/");
} else {
Movie movie = moviceService.queryByName(movieName);
String evaId = req.getParameter("eva_id");
User user = (User) req.getSession().getAttribute("user");
if (movie == null || evaId == null) {
resp.sendRedirect(req.getContextPath() + "/");
} else if (!evaId.isEmpty()) {
//检查提交的是否相符
Evaluate eva = evaluateService.query(evaId);
if (eva != null) {
String content = req.getParameter("evaluate_content");
//截取回复内容里的昵称
Pattern pattern = Pattern.compile("\\[回复\\].+\\[/回复\\]");
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
String temp = matcher.group();
String nick = temp.replace("[回复]", "")
.replace("[/回复]", "");
if (eva.nickname.equals(nick)) {
Reply reply = new Reply();
reply.username = user.username;
reply.nickname = user.nickname;
reply.evaluate_id = Integer.valueOf(evaId);
reply.time = System.currentTimeMillis();
reply.content = content.substring(temp.length()).trim();
} else {
resp.sendRedirect(req.getContextPath() + "/admin/movies");
return;
}
} else {
Plan plan = new Plan();
Utils.populateToBean(plan, req.getParameterMap());
try {
planService.add(plan);
req.setAttribute("success", true);
} catch (MyException e) {
req.setAttribute("msg", e.getMessage());
Utils.fillAttr(req, plan);
}
}
//查询所有影厅,并返回
List<Hall> list = hallService.queryAll();
if (list != null) {
req.setAttribute("halls", list);
}
req.getRequestDispatcher("/admin/plan_movie.jsp").forward(req, resp);
}
private void backListToPlansJsp(List<Plan> list, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (list != null) {
req.setAttribute("plans", list);
}
req.setAttribute("chkItem", 6);
req.getRequestDispatcher("/admin/plans.jsp").forward(req, resp);
}
}
package com.demo.web;
}
req.getSession().setAttribute("user", user);
req.getSession().setMaxInactiveInterval(7200);
if (from == null) {
resp.sendRedirect(req.getContextPath() + "/");
} else {
resp.sendRedirect(req.getContextPath() + from);
}
} catch (MyException e) {
req.setAttribute("msg", e.getMessage());
Utils.fillAttr(req, "username", username);
Utils.fillAttr(req, "password", password);
req.getRequestDispatcher("/login.jsp").forward(req, resp);
}
}
}
}
package com.demo.web;
/**
* 功能: 用户注册
*/
@WebServlet(urlPatterns = "/reg")
public class RegisterServlet extends BaseServlet {
private static final long serialVersionUID = -6923825645904193768L;
private UserService service = BaseFactory.getService(UserService.class);
/**
* 功能: 排片管理
*/
@WebServlet(urlPatterns = {"/admin/plans", "/admin/plans/add", "/admin/plans/cancel"})
public class PlanMgrServlet extends BaseServlet {
private static final long serialVersionUID = -6874708411049815744L;
private PlanService planService = BaseFactory.getService(PlanService.class);
private HallService hallService = BaseFactory.getService(HallService.class);
private MoviceService moviceService = BaseFactory.getService(MoviceService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String servletPath = req.getServletPath();
if (servletPath.contains("cancel")) {//取消排片
doDel(req, resp);
} else if (servletPath.contains("add")) {//执行添加
doAdd(req, resp);
} else {
String keyword = req.getParameter("keyword");
if (Utils.isEmpty(keyword)) {
backListToPlansJsp(planService.queryAll(), req, resp);
} else {
req.setAttribute("keyword", keyword);
backListToPlansJsp(planService.queryPlansByKeyword(keyword), req, resp);
}
}
}
private void doDel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String planInfo = req.getParameter("plan");
}
}
package com.demo.web;
/**
* 描述:购票
*/
@WebServlet(urlPatterns = "/order")
public class GenerateOrderServlet extends BaseServlet {
private static final long serialVersionUID = -989924899669241621L;
private HallService hallService = BaseFactory.getService(HallService.class);
private PlanService planService = BaseFactory.getService(PlanService.class);
private UserService userService = BaseFactory.getService(UserService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String session = req.getParameter("session");
if (session != null) {
//查询排片
String[] ss = session.split("_");
Plan plan = planService.query(ss[0], ss[1], ss[2]);
if (plan != null) {
//查询影厅
Hall hall = hallService.queryByName(plan.hall_name);
if (hall != null) {
User user = (User) req.getSession().getAttribute("user");
//看数据库是否还有用户信息,可能被删除了
if (userService.query(String.valueOf(user.id)) == null) {
resp.sendRedirect(req.getContextPath() + "/logout");
return;
}
return ran.nextInt(end - begin) + begin;
}
private int getRan(int bound) {
return ran.nextInt(bound);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.demo.web;
/**
* 功能: 用户登录
*/
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends BaseServlet {
private static final long serialVersionUID = 8264050085226713309L;
private UserService service = BaseFactory.getService(UserService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
//带了登录界面的上级url的话,回到这个url,否则转到主页
String from = req.getParameter("from");
if (from != null) {
req.setAttribute("from", from);
}
if (username == null || password == null) {
req.getRequestDispatcher("/login.jsp").forward(req, resp);
} else {
try {
User user = service.valicateUser(username, password);
//检查验证码是否正确
String valicateCode = req.getParameter("valicateCode");
HttpSession session = req.getSession();
int beginIndex = fileName.lastIndexOf(".");
String ext = "";
if (beginIndex != -1) {
ext = fileName.substring(beginIndex);
}
return MD5Utils.getMD5Code(UUID.randomUUID().toString()) + ext;
}
public static int compare(Object o1, String s1, Object o2, String s2) {
if (o2 == null || s2 == null)
return 1;
if (o1 == null || s1 == null)
return -1;
String s3 = CharacterParser.getSelling(s1);
String s4 = CharacterParser.getSelling(s2);
return s3.compareTo(s4);
}
}
package com.demo.web;
/**
* 描述:购票
*/
@WebServlet(urlPatterns = "/order")
public class GenerateOrderServlet extends BaseServlet {
private static final long serialVersionUID = -989924899669241621L;
private HallService hallService = BaseFactory.getService(HallService.class);
private PlanService planService = BaseFactory.getService(PlanService.class);
private UserService userService = BaseFactory.getService(UserService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String session = req.getParameter("session");
if (session != null) {
Utils.fillAttr(req, map, "name");
Utils.fillAttr(req, map, "director");
Utils.fillAttr(req, map, "protagonist");
Utils.fillAttr(req, map, "region");
Utils.fillAttr(req, map, "language");
Utils.fillAttr(req, map, "type");
Utils.fillAttr(req, map, "duration");
Utils.fillAttr(req, map, "synopsis");
if (add) {
req.setAttribute("add", true);
} else {
req.setAttribute("add", false);
Utils.fillAttr(req, map, "id");
}
req.getRequestDispatcher("/admin/add_update_movie.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private void forwardToMoviesJsp(List<Movie> list, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (list != null) {
req.setAttribute("movies", list);
}
req.setAttribute("chkItem", 2);
req.getRequestDispatcher("/admin/movies.jsp").forward(req, resp);
}
}
package com.demo.util;
/**
* 功能: 影片详情
*/
@WebServlet(urlPatterns = "/movie/detail")
public class MovieDetailServlet extends BaseServlet {
private static final long serialVersionUID = -4869371109392125427L;
private MoviceService moviceService = BaseFactory.getService(MoviceService.class);
private EvaluateService evaluateService = BaseFactory.getService(EvaluateService.class);
private ReplyService replyService = BaseFactory.getService(ReplyService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String movieName = req.getParameter("movie");
if (movieName == null) {
String lookMovie = (String) req.getSession().getAttribute("lookMovie");
if (lookMovie == null) {
resp.sendRedirect(req.getContextPath() + "/home");
} else {
back(req, resp, lookMovie);
}
} else {
back(req, resp, movieName);
}
}
private void back(HttpServletRequest req, HttpServletResponse resp, String movieName) throws IOException, ServletException {
Movie movie = moviceService.queryByName(movieName);
if (movie == null) {
resp.sendRedirect(req.getContextPath() + "/home");
} else {
if (!isEncoded) {
Map<String, String[]> map = request.getParameterMap();
for (Map.Entry<String, String[]> me : map.entrySet()) {
String[] values = me.getValue();
for (int i = 0; i < values.length; i++) {
values[i] = URLDecoder.decode(values[i], encoding);
}
}
isEncoded = true;
return map;
} else {
return request.getParameterMap();
}
} else {
return request.getParameterMap();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public String[] getParameterValues(String name) {
return getParameterMap().get(name);
}
}
}
package com.demo.web;
/**
* 功能: 订单管理
*/
@WebServlet(urlPatterns = {"/admin/orders", "/admin/orders/delete", "/admin/orders/update"})
}
private void forwardToMoviesJsp(List<Movie> list, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (list != null) {
req.setAttribute("movies", list);
}
req.setAttribute("chkItem", 2);
req.getRequestDispatcher("/admin/movies.jsp").forward(req, resp);
}
}
package com.demo.util;
/**
* 功能: 抽取出的一些用到的方法
*/
public class Utils {
/**
* 将提交的参数值赋值给对象
*/
public static void populateToBean(Object bean, Map<String, ?> params) {
try {
populateBean(bean, params);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void populateBean(Object bean, Map<String, ?> params) throws Exception {
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object obj = params.get(field.getName());
String[] value = null;
if (obj instanceof String) {
value = new String[]{(String) obj};
} else if (obj instanceof String[]) {
value = (String[]) obj;
}
if (value != null ) {
Class<?> clazz = field.getType();
if (clazz == String.class) {
}
}
return list;
}
public static Ticket getTicket(Order order) {
PlanService planService = BaseFactory.getService(PlanService.class);
Plan plan = planService.query(String.valueOf(order.plan_id));
if (plan != null) {
MoviceService moviceService = BaseFactory.getService(MoviceService.class);
Movie movie = moviceService.queryByName(plan.movie_name);
HallService hallService = BaseFactory.getService(HallService.class);
Hall hall = hallService.queryByName(plan.hall_name);
if (movie != null && hall != null) {
Ticket ticket = new Ticket();
ticket.orderId = order.id;
ticket.buyTime = DateUtils.formatDate(order.create_time, "yyyy-MM-dd HH:mm");
ticket.movieName = plan.movie_name;
ticket.picName = movie.pic_name;
ticket.hallName = plan.hall_name;
ticket.playTime = DateUtils.formatDate(plan.play_time, "yyyy-MM-dd HH:mm");
ticket.endTime = DateUtils.formatDate(plan.play_time + movie.duration * 60000, "HH:mm");
ticket.language = movie.language;
ticket.hallType = hall.type;
ticket.seats = new ArrayList<>();
String[] ss = order.seat.split("\\|");
for (String s : ss) {
if (s.matches("\\d+_\\d+")) {
String[] split = s.split("_");
ticket.seats.add(split[0] + "排" + split[1] + "座");
}
}
ticket.ticketCount = ticket.seats.size();
ticket.price = String.format("%.2f", plan.price);
ticket.totalPrice = String.format("%.2f", order.amount);
//根据当前时间对比放映时间,判断订单状态
long currentTime = System.currentTimeMillis();
if (currentTime < plan.play_time) {
ticket.state = "待放映";
} else if (currentTime > plan.play_time && currentTime < plan.play_time + movie.duration * 60000) {
ticket.state = "正在放映";
} else {
ticket.state = "已放映";
}
return ticket;
}
}
return null;
}
if (keyword == null) {
forwardToOrdersJsp(fillFields(orderService.queryAll()), req, resp);
} else {
forwardToOrdersJsp(fillFields(Collections.singletonList(orderService.query(keyword))), req, resp);
}
}
}
private void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String id = req.getParameter("id");
String amount = req.getParameter("amount");
if (id != null && amount != null) {
Double value = Double.valueOf(amount);
value = Math.floor(value * 100) / 100;
orderService.update(id, value);
}
resp.sendRedirect(req.getContextPath() + "/admin/orders");
}
private List<Order> fillFields(List<Order> list) {
if (list != null) {
for (Order order : list) {
User user = userService.query(String.valueOf(order.user_id));
if (user != null) {
order.username = user.username;
}
}
}
return list;
}
private void doDel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String id = req.getParameter("id");
if (id != null) {
orderService.delete(id);
}
resp.sendRedirect(req.getContextPath() + "/admin/orders");
}
private void forwardToOrdersJsp(List<Order> list, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (list != null) {
req.setAttribute("orders", list);
*/
public static boolean isPasswordValid(String password) {
//为空或长度不符
if (password == null || password.length() < 6 || password.length() > 30) {
return false;
}
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(password);
//如果包含空白字符
if (matcher.find()) {
return false;
}
//只包含一种字符
return !password.matches("\\d+") && !password.matches("[a-z]+") && !password.matches("[A-Z]+") && !password.matches("\\W+");
}
public static void fillAttr(HttpServletRequest req, Map<String, ?> map, String key) {
Object value = map.get(key);
String s = null;
if (value instanceof String) {
s = (String) value;
} else if (value instanceof String[]) {
s = ((String[]) value)[0];
}
if (s != null) {
req.setAttribute(key, s);
}
}
public static void fillAttr(HttpServletRequest req, String key, String value) {
if (value != null) {
req.setAttribute(key, value);
}
}
public static void fillAttr(HttpServletRequest req, Object bean) {
try {
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(bean);
if (value != null) {
req.setAttribute(field.getName(), value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String generateImgFileName(String fileName) {
// 获得扩展名
int beginIndex = fileName.lastIndexOf(".");
ticket.seats = new ArrayList<>();
String[] ss = order.seat.split("\\|");
for (String s : ss) {
if (s.matches("\\d+_\\d+")) {
String[] split = s.split("_");
ticket.seats.add(split[0] + "排" + split[1] + "座");
}
}
ticket.ticketCount = ticket.seats.size();
ticket.price = String.format("%.2f", plan.price);
ticket.totalPrice = String.format("%.2f", order.amount);
//根据当前时间对比放映时间,判断订单状态
long currentTime = System.currentTimeMillis();
if (currentTime < plan.play_time) {
ticket.state = "待放映";
} else if (currentTime > plan.play_time && currentTime < plan.play_time + movie.duration * 60000) {
ticket.state = "正在放映";
} else {
ticket.state = "已放映";
}
return ticket;
}
}
return null;
}
private void forwardToOrderJsp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = (User) req.getSession().getAttribute("user");
req.setAttribute("tickets", getTicketList(String.valueOf(user.id)));
req.setAttribute("chkItem", 2);
req.getRequestDispatcher("/tickets.jsp").forward(req, resp);
}
}
package com.demo.web;
/**
* 功能: 影片详情
*/
@WebServlet(urlPatterns = "/movie/detail")
public class MovieDetailServlet extends BaseServlet {
private static final long serialVersionUID = -4869371109392125427L;
private MoviceService moviceService = BaseFactory.getService(MoviceService.class);
private EvaluateService evaluateService = BaseFactory.getService(EvaluateService.class);
private ReplyService replyService = BaseFactory.getService(ReplyService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String movieName = req.getParameter("movie");
if (movieName == null) {
String lookMovie = (String) req.getSession().getAttribute("lookMovie");
if (lookMovie == null) {
resp.sendRedirect(req.getContextPath() + "/home");
} else {
back(req, resp, lookMovie);
}
} else {
back(req, resp, movieName);
}
}
private void back(HttpServletRequest req, HttpServletResponse resp, String movieName) throws IOException, ServletException {
Movie movie = moviceService.queryByName(movieName);
if (movie == null) {
resp.sendRedirect(req.getContextPath() + "/home");
} else {
req.getSession().setAttribute("lookMovie", movieName);
//返回影片信息及该影片的评价、回复
req.setAttribute("movie", movie);
List<Evaluate> list = evaluateService.queryEvaluatesByMovieName(movie.name);
EvaluateMgrServlet.fillEvaMap(list, req, replyService);
req.setAttribute("title", "影片详情");
req.getRequestDispatcher("/movie_detail.jsp").forward(req, resp);
}
/**
* 功能: 影厅管理
*/
@WebServlet(urlPatterns = {"/admin/halls", "/admin/halls/add", "/admin/halls/delete"})
public class HallMgrServlet extends BaseServlet {
private static final long serialVersionUID = 6375850281387346655L;
private HallService service = BaseFactory.getService(HallService.class);
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String servletPath = req.getServletPath();
if (servletPath.contains("delete")) {//执行删除
doDel(req, resp);
} else if (servletPath.contains("add")) {//添加影厅
doAdd(req, resp);
} else {
forwardToHallsJsp(req, resp);
}
}
private void forwardToHallsJsp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Hall> list = service.queryAll();
if (list != null) {
req.setAttribute("halls", list);
}
req.setAttribute("chkItem", 3);
req.getRequestDispatcher("/admin/halls.jsp").forward(req, resp);
}
private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Hall hall = new Hall();
Utils.populateToBean(hall, req.getParameterMap());
try {
service.add(hall);
resp.sendRedirect(req.getContextPath() + "/admin/halls");
} catch (MyException e) {
req.setAttribute("msg", e.getMessage());
Utils.fillAttr(req, hall);
forwardToHallsJsp(req, resp);
}
}
//如果关键字不为空,搜索返回,否则返回全部
String keyword = req.getParameter("keyword");
if (Utils.isEmpty(keyword)) {
forwardToEvaluatesJsp(evaluateService.queryAll(), req, resp);
} else {
req.setAttribute("keyword", keyword);
forwardToEvaluatesJsp(evaluateService.queryMoviesByKeyword(keyword), req, resp);
}
}
}
private void doDel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String evaluateInfo = req.getParameter("i");
if (evaluateInfo != null) {
String[] ss = evaluateInfo.split("_");
evaluateService.delete(ss[0], ss[1], ss[2]);
}
resp.sendRedirect(req.getContextPath() + "/admin/evaluates");
}
private void forwardToEvaluatesJsp(List<Evaluate> list, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
fillEvaMap(list, req, replyService);
req.setAttribute("chkItem", 5);
req.getRequestDispatcher("/admin/evaluates.jsp").forward(req, resp);
}
public static void fillEvaMap(List<Evaluate> list, HttpServletRequest req, ReplyService replyService) {
if (list != null) {
LinkedHashMap<Evaluate, List<Reply>> map = new LinkedHashMap<>();
for (Evaluate eva : list) {
List<Reply> li = replyService.queryByEvaluateId(eva.id);
map.put(eva, li == null ? new ArrayList<>() : li);
}
req.setAttribute("evamap", map);
}
}
}
package com.demo.web;