BaseServlet
package com.etime.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//防止乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String type = request.getParameter("type");
//通过反射获取类对象
Class clazz = this.getClass();
try {
//获取方法
Method method = clazz.getDeclaredMethod(type, HttpServletRequest.class, HttpServletResponse.class);
///暴力反射
method.setAccessible(true);
//实现该方法
method.invoke(this, request, response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
ManagerSevlet
package com.etime.servlet;
import com.etime.service.ManagerService;
import com.etime.service.impl.ManagerServiceImpl;
import javax.servlet.http.*;
import java.io.IOException;
public class ManagerServlet extends BaseServlet {
ManagerService managerService = new ManagerServiceImpl();
//注册
private void registered(HttpServletRequest request, HttpServletResponse response) throws IOException {
String userRes = request.getParameter("usernameRes");
String passRes = request.getParameter("passwordRes");
boolean flag = managerService.registered(userRes, passRes);
if (flag) {
//注册成功
response.sendRedirect("login.jsp");
} else {
//注册失败
response.sendRedirect("registered.jsp");
}
}
//登陆
private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
String user = request.getParameter("usernameLog");
String pass = request.getParameter("passwordLog");
boolean flag = managerService.login(user, pass);
if (flag) {
//登陆成功
String choose = request.getParameter("choose");
if ("rem".equals(choose)){
//记住密码
Cookie cookieName = new Cookie("name", user);
Cookie cookiePass = new Cookie("password", pass);
//设置时效
cookieName.setMaxAge(60*60*24);
cookiePass.setMaxAge(60*60*24);
response.addCookie(cookieName);
response.addCookie(cookiePass);
}else {
//取消记住密码
Cookie cookieName = new Cookie("name", null);
Cookie cookiePass = new Cookie("password", null);
//设置时效
cookieName.setMaxAge(0);
cookiePass.setMaxAge(0);
response.addCookie(cookieName);
response.addCookie(cookiePass);
}
HttpSession session = request.getSession();
session.setAttribute("name",user);
response.sendRedirect("index.jsp");
} else {
//登陆失败
response.sendRedirect("login.jsp");
}
}
//退出登陆
private void out(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("login.jsp");
}
}
StudentServlet
package com.etime.servlet;
import com.etime.entity.Student;
import com.etime.service.StudentService;
import com.etime.service.impl.StudentServiceImpl;
import com.etime.util.PageUtil;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@MultipartConfig
@WebServlet(name = "StudentServlet", value = "/StudentServlet")
public class StudentServlet extends BaseServlet {
StudentService studentService = new StudentServiceImpl();
//分页展示学生全部信息
private void studentByPage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取当前页数
String page = request.getParameter("page");
//设置显示条数
int rows = 5;
//获取数据总条数
int countRowStu = studentService.countRowsStu();
//获取分页工具类对象
PageUtil pageUtil = new PageUtil(page, rows, countRowStu);
//获取存储学生数据的集合
List list = studentService.studentByPage(pageUtil);
request.setAttribute("list", list);
request.setAttribute("pageUtil", pageUtil);
request.getRequestDispatcher("showStudent.jsp").forward(request, response);
}
//回显
private void updateEcho(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
int id = Integer.parseInt(request.getParameter("id"));
Student student = studentService.getStudentById(id);
request.setAttribute("stu",student);
request.getRequestDispatcher("updateStudent.jsp").forward(request,response);
}
//修改
private void newUpdate(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");
String email = request.getParameter("email");
int id = Integer.parseInt(request.getParameter("id"));
//上传文件处理
//获取处理文件对象
Part part = request.getPart("photo");
//获取文件名字
String fileName = part.getSubmittedFileName();
if ("".equals(fileName)){
//未修改
fileName = request.getParameter("oldphoto");
}else {
//修改
fileName = UUID.randomUUID() + part.getSubmittedFileName();
//指定文件上传的位置
String path = "E:\\SpecializedCode\\JAVA_PeiXun\\img\\student";
File file = new File(path);
//判断文件夹是否存在
if (file.exists()) {
file.mkdirs();
}
//将文件放入指定位置
part.write(file + "\\" + fileName);
}
Student student = new Student(id,name,age,sex,email,fileName);
boolean flag = studentService.update(student);
if (flag) {
request.setAttribute("state", "修改成功!");
request.getRequestDispatcher("StudentServlet?type=studentByPage").forward(request, response);
} else {
request.setAttribute("state", "修改失败!");
request.getRequestDispatcher("StudentServlet?type=studentByPage").forward(request, response);
}
}
//删除
private void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
boolean flag = studentService.delete(id);
if (flag) {
request.setAttribute("state", "删除成功!");
request.getRequestDispatcher("StudentServlet?type=studentByPage").forward(request, response);
} else {
request.setAttribute("state", "删除失败");
request.getRequestDispatcher("StudentServlet?type=studentByPage").forward(request, response);
}
}
//增加
private void insertStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");
String email = request.getParameter("email");
//上传文件处理
//获取处理文件对象
Part part = request.getPart("photo");
//获取文件名字
String fileName = UUID.randomUUID() + part.getSubmittedFileName();
//指定文件上传的位置
String path = "E:\\SpecializedCode\\JAVA_PeiXun\\img\\student";
File file = new File(path);
//判断文件夹是否存在
if (file.exists()) {
file.mkdirs();
}
//将文件放入指定位置
part.write(file + "\\" + fileName);
Student student = new Student(name, age, sex, email, fileName);
boolean flag = studentService.insert(student);
if (flag) {
request.setAttribute("state", "添加成功!");
request.getRequestDispatcher("StudentServlet?type=studentByPage").forward(request, response);
} else {
request.setAttribute("state", "添加失败!");
request.getRequestDispatcher("StudentServlet?type=studentByPage").forward(request, response);
}
}
//查询
private void selectName(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String selectText = request.getParameter("selectText");
//定义每页展示多少条数据
int rows = 5;
//获取当前页数
String page = request.getParameter("page");
//获取总条数
int countRow = studentService.getSelectNameCountRows(selectText);
//获取分页工具对象
PageUtil pageUtil = new PageUtil(page, rows, countRow);
List list = studentService.selectName(selectText,pageUtil);
request.setAttribute("pageUtil", pageUtil);
request.setAttribute("list", list);
request.setAttribute("selectText",selectText);
request.getRequestDispatcher("showStudent.jsp").forward(request, response);
}
//全部
private void student(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List studentList = studentService.selectStudent();
request.setAttribute("list", studentList);
request.getRequestDispatcher("showStudent.jsp").forward(request, response);
}
}
搜索
新增