Java project: train ticket reservation system (java+JDBC+JSP+Servlet+html+mysql)

Source code acquisition: Download from “Resources” on the blog homepage!

1. Project operation

Environment configuration:

Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA, Eclispe, MyEclispe, Sts support)

Project technology:

JSP + Servlert + html+ css + JavaScript + JQuery + Ajax, etc.;
 

 

 

 

 

 

 

Personal Center Controller:

/**
 * Personal Center Controller
 */
@Controller
public class UserInforController {
	
    @Autowired
    private UserInforServiceImpl userInforService = null;

    /**
     * Change password operation
     * @param oldPassword
     * @param newPassword
     * @param rePassword
     * @param httpSession
     * @return
     */
    @RequestMapping("changePassword.do")
    @ResponseBody
    public Map<String, String> changePassword(String oldPassword, String newPassword,
                                              String rePassword, HttpSession httpSession){
        HashMap<String, String> map = new HashMap<String, String>();
        if (newPassword.equals(rePassword)){
            SystemManager admin = (SystemManager) httpSession.getAttribute("admin");
            String encodeByMD5 = MD5Utils.encodeByMD5(oldPassword);
            if (encodeByMD5.equals(admin.getSmPassword())){
                String newPasswords = MD5Utils.encodeByMD5(newPassword);
                admin.setSmPassword(newPasswords);
                userInforService.updateSystemManagePassword(admin.getSmId(),admin);
                map.put("type","success");
                map.put("msg","Password changed successfully");
                return map;
            }else{
                map.put("type","error");
                map.put("msg","Original password is wrong");
                return map;
            }
        }else{
            map.put("type","error");
            map.put("msg","The two passwords are inconsistent");
            return map;
        }
    }
    
    /**
     * Employee changes personal password
     * @param oldPassword
     * @param newPassword
     * @param rePassword
     * @param httpSession
     * @return
     */
    @RequestMapping("changeEmployeePassword.do")
    @ResponseBody
    public Map<String, String> changeEmployeePassword(String oldPassword, String newPassword,
                                              String rePassword, HttpSession httpSession){
        HashMap<String, String> map = new HashMap<String, String>();
        if (newPassword.equals(rePassword)){
            Integer eid = (Integer) httpSession.getAttribute("employeeId");
            try {
                userInforService.updateEmployeePassword(eid, oldPassword, newPassword);
                map.put("type","success");
                map.put("msg","Password changed successfully");
                return map;
            } catch (CustomException e) {
                map.put("type","error");
                map.put("msg","Original password is wrong");
                return map;
            }
        }else{
            map.put("type","error");
            map.put("msg","The two passwords are inconsistent");
            return map;
        }
    }

    /**
     * View personal information
     * @param httpSession
     * @return
     */
    @RequestMapping("inforEmployee.do")
    public @ResponseBody EmployeeCustomVo getInforEmployee(HttpSession httpSession){
        Integer id = (Integer) httpSession.getAttribute("employeeId");
        EmployeeCustomVo employeeCustomVo = userInforService.getInforEmployee(id);
        return employeeCustomVo;
    }

    /**
     * Modify Personal Information
     * @param httpSession
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping("updateInforEmployee.do")
    public Message updateInforEmployee(HttpSession httpSession, Employee employee){
        Integer id = (Integer) httpSession.getAttribute("employeeId");
        employee.seteId(id);
        if(userInforService.updateEmploueeById(id,employee)<=0) {
        	return Message.error("Failed to modify information");
        }
       return Message.success();
    }



    /**
     *Personal salary information
     * @param pageNum
     * @param limit
     * @param year
     * @param httpSession
     * @return
     * @throws Exception
     */
    @RequestMapping("employeeSalaryList.do")
    @ResponseBody
    public EmployeeSalaryVO findSelective(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="10") int limit,
            @RequestParam(value="year", defaultValue="1") String year,
            HttpSession httpSession) throws Exception {

        Integer eId = (Integer) httpSession.getAttribute("employeeId");
        //pageNum: starting page pageSize: size of each page
        PageHelper.startPage(pageNum,limit);
        //Search conditions, must follow startPage
        List<Salary> salaryList = userInforService.getEmployeeSalaryList(eId, year);

        PageInfo pageResult = new PageInfo(salaryList);

        //Set the data required by the front desk
        EmployeeSalaryVO employeeSalaryVO = new EmployeeSalaryVO();
        employeeSalaryVO.setCode(0);
        employeeSalaryVO.setMsg("");
        employeeSalaryVO.setCount((int) pageResult.getTotal());
        employeeSalaryVO.setData(pageResult.getList());
        return employeeSalaryVO;
    }

}

Administrator and employee login control:

/**
 * @Author: admin
 * @Description: Administrator and employee login control
 **/
@Controller
public class LoginController {
    @Autowired
    private LoginServiceImpl loginService = null;
    /**
     * @Author: admin
     * @Description: Verification code change
     * @Date: 14:33 2021/10/5
     * @Param: [request, response]
     * @Return: void
     **/
    @RequestMapping(value = "/changeCode.do")
    @ResponseBody
    public void getIdentifyingCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        // The verification code is stored in the identifyingCode attribute of the session
        CaptchaUtil.outputCaptcha(request, response);
    }

    // Get the employee login interface
    @RequestMapping("/")
    public String getLoginPage(){
        return "employee/login.html";
    }

    // Get the administrator login interface
    @RequestMapping("/admin.do")
    public String getAdminLoginPage(HttpServletRequest request){
    	String realPath = request.getServletContext().getRealPath("/");
    	request.getSession().setAttribute("realPath", realPath);
        return "admin/adminLogin.html";
    }
    
    
    /**
     * Employee login operation
     * @param model
     * @param httpSession
     * @param username
     * @param password
     * @param identifyingcode
     * @return
     */
    @RequestMapping(value = "/employeeLogin.do")
    @ResponseBody
    public Message employeeLogin(HttpSession httpSession, String username,
                             String password, String identifyingcode)
    {
    	if(StringUtils.isEmpty(username)) {
    		return Message.error("Please fill in your employee number");
    	}
    	if(StringUtils.isEmpty(password)) {
    		return Message.error("Please fill in the password");
    	}
    	if(StringUtils.isEmpty(identifyingcode)) {
    		return Message.error("Please fill in the verification code");
    	}
        String code = (String) httpSession.getAttribute("identifyingCode");
        if(!identifyingcode.equalsIgnoreCase(code)){
        	return Message.error("Verification code error");  
        }
        Employee employee = loginService.findEmployeeByIdAndPassword(username, password);
        if(employee==null) {
        	return Message.error("Incorrect employee number or password");
        	
        } 
        httpSession.setAttribute("employeeId",employee.geteId());
       return Message.success("Employee login successful");
    }

    @RequestMapping(value = "/loginSuccess.do")
    public String loginSucceses(Model model) throws Exception
    {
        return "employee/index.html";
    }

    /**
     * Administrator login operation
     * @param model
     * @param httpSession
     * @param username
     * @param password
     * @param identifyingcode
     * @return
     */
    @RequestMapping(value = "/adminLogin.do")
    @ResponseBody
    public Message adminLogin(HttpSession httpSession, String username,
                                            String password, String identifyingcode)
    {
        
    	if(StringUtils.isEmpty(username)) {
    		return Message.error("Please fill in the account number");
    	}
    	if(StringUtils.isEmpty(password)) {
    		return Message.error("Please fill in the password");
    	}
    	if(StringUtils.isEmpty(identifyingcode)) {
    		return Message.error("Please fill in the verification code");
    	}
        String code = (String) httpSession.getAttribute("identifyingCode");
        if(identifyingcode.equalsIgnoreCase(code)){
            SystemManager manager = loginService.findSystemManagerByIdAndPassword(username, password);
           if(manager==null) {
        	   return Message.error("Wrong account or password");
           }
            // Save to session
            httpSession.setAttribute("admin",manager);
            return Message.success("Login successful");
        }else {
        	return Message.error("Verification code error");
        }
    }
    @RequestMapping(value = "/getAdminAccount.do")
    @ResponseBody
    public String getAdminAccount(HttpSession httpSession){
        SystemManager systemManager = (SystemManager) httpSession.getAttribute("admin");
//        SystemManager manager = loginService.findSystemManagerById(id);
        return systemManager.getSmAccount();
    }

    @RequestMapping(value = "/getEmployeeAccount.do")
    @ResponseBody
    public Map<String,String> getEmployeeAccount(HttpSession httpSession){
        Integer id = (Integer) httpSession.getAttribute("employeeId");
        Employee employee = loginService.findEmployeeById(id);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account",employee.geteAccount());
        map.put("name",employee.geteName());
        return map;
    }
    @RequestMapping(value = "/logout.do")
    public String logout(HttpSession httpSession){
        httpSession.removeAttribute("employeeId");
        return "redirect:/";
    }

    @RequestMapping(value = "/logoutAdmin.do")
    public String logoutAdmin(HttpSession httpSession){
       httpSession.removeAttribute("admin");
       return "redirect:/admin.do";
    }
}

User management operations:

/**
 * User management operations
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * User added page
     * @return
     */
    @GetMapping("/add")
    public String create() {
        return "user/add";
    }

    /**
     * User addition operation
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody User user) {
        if(StringUtils.isEmpty(user.getUserName())){
            return MapControl.getInstance().error("Please fill in the user name").getMap();
        }
        if(StringUtils.isEmpty(user.getName())){
            return MapControl.getInstance().error("Please fill in the name").getMap();
        }
        if(StringUtils.isEmpty(user.getUserPwd())){
            return MapControl.getInstance().error("Please fill in the password").getMap();
        }
        int result = userService.create(user);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    /**
     * Delete based on id
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int result = userService.delete(id);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    //batch deletion
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String ids) {
        int result = userService.delete(ids);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    /**
     * Edit user information operation
     * @param user
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody User user) {
        if(StringUtils.isEmpty(user.getUserName())){
            return MapControl.getInstance().error("Please fill in the user name").getMap();
        }
        if(StringUtils.isEmpty(user.getName())){
            return MapControl.getInstance().error("Please fill in the name").getMap();
        }
        if(StringUtils.isEmpty(user.getUserPwd())){
            return MapControl.getInstance().error("Please fill in the password").getMap();
        }
        int result = userService.update(user);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    /**
     * According to id query, jump to the modification page
     * @param id
     * @param modelMap
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap modelMap) {
        User user = userService.detail(id);
        modelMap.addAttribute("user", user);
        return "user/edit";
    }

    //Query all
    @PostMapping("/query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody User user) {
        List<User> list = userService.query(user);
        Integer count = userService.count(user);
        return MapControl.getInstance().success().page(list, count).getMap();
    }

    //Jump list page
    @GetMapping("/list")
    public String list() {
        return "user/list";
    }

}

Source code acquisition: Download from “Resources” on the blog homepage!

Related Posts

The detailed process of SpringBoot integrating Alibaba Cloud SMS service (guaranteed that beginners can also implement it)

Mockito + JUnit unit test example

“New Semester, New FLAG” Breaking the Limit

Detailed explanation of the use of Java’s jmap command

[WebSocket] Protocol Detailed Explanation

Docker, which has 54k stars on Github, what exactly is it?

Java Date time zone settings (GMT and CST)

ES ElasticSearch Connection reset by peer problem solving

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*