系统一共分为2个角色分别是:管理员,用户
登录
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- %>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>登录title>
-
- <link rel="stylesheet" href="<%=path %>/resource/component/pear/css/pear.css" />
- <link rel="stylesheet" href="<%=path %>/resource/admin/css/other/login.css" />
- head>
- <body background="<%=path %>/resource/admin/images/background.svg" style="background-size: cover;">
- <form class="layui-form" action="LoginServlet?action=login" method="post" id="loginForm" name="loginForm" >
- <div class="layui-form-item">
- <img class="logo" src="<%=path %>/resource/admin/images/logo.png" />
- <div class="title">登 录div>
- <div class="desc">
- 计算机类考研资源平台
- div>
- div>
- <div class="layui-form-item">
- <input type="text" placeholder="请输入用户名或者手机号" name="username" id="username" class="layui-input" />
- div>
- <div class="layui-form-item">
- <input type="password" name="password" id="password" placeholder="请输入密码" class="layui-input" />
- div>
- <div class="layui-form-item">
- <label class="layui-form-label">请选择角色label>
- <div class="layui-input-block">
- <select name="type" lay-verify="required" lay-filter="aihao">
- <option value="1">管理员option>
- <option value="2">用户option>
- select>
- div>
- div>
- <%-- <div class="layui-form-item" style="text-align: center;">
- <input type="radio" name="type" value="1" title="管理员" checked>
- <input type="radio" name="type" value="2" title="用户" >
- div>--%>
- <div class="layui-form-item">
- <button type="button" class="pear-btn pear-btn-success login" id="login">
- 登录
- button>
- div>
-
- form>
- <script src="<%=path %>/resource/component/layui/layui.js">script>
- <script src="<%=path %>/resource/component/pear/pear.js">script>
- <script src="<%=path %>/resource/layui/jquery-1.9.1.min.js">script>
-
- <script>
-
- $("#login").click(function(){
- var username = $.trim($('#username').val());
- var password = $.trim($("#password").val());
- if (username == '') {
- layer.msg('用戶名不能为空', function () {
- time:2000
- });
- return false;
- }
- if (password == '') {
- layer.msg('密码不能为空', function () {
- time:2000
- });
- return false;
- }
-
- $.ajax({
- cache: true,
- type: "post",
- url: "login",
- data: $("#loginForm").serialize(),
- async: false,
- success: function (e) {
- if (e == 'ok') {
- alert("登录成功!");
- window.parent.location.href = "toMain";
- } else if (e == 'toIndex') {
- alert("登录成功!");
- window.parent.location.href = "toIndex";
- } else {
- alert("登录失败,账号或者密码错误!");
- }
- }
- })
-
- });
-
- script>
-
-
- body>
- html>
- /**
- * 登录
- * @param username
- * @param request
- * @param password
- * @param session
- * @param response
- * @param mv
- * @return
- * @throws ServletException
- * @throws IOException
- */
- @RequestMapping("/login")
- @ResponseBody
- public String login(@RequestParam("username")String username,
- HttpServletRequest request, @RequestParam("password")String password,
- HttpSession session, HttpServletResponse response, ModelAndView mv) throws ServletException, IOException {
- session.removeAttribute("admin");
- session.removeAttribute("student");
- String type=request.getParameter("type").toString();
- request.getSession().setAttribute("type", type);
- String message = "error";
- if(type != null && type.equals("1")){
- Admin admin1 = service.selectAdmin(username,password);
- if(admin1 != null){
- request.getSession().setAttribute("admin", admin1);
- request.getSession().setAttribute("flag", type);
- message = "ok";
- }
- }else if(type != null && type.equals("2")){
- User te = service.selectUser(username,password);
- if(te != null){
- request.getSession().setAttribute("user", te);
- request.getSession().setAttribute("flag", type);
- message = "toIndex";
- }
- }
- return message;
-
- }