系统一共分为2个角色分别是:管理员,用户
登录
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <jsp:include page="/WEB-INF/views/header.jsp"/>
- <body>
- <div class="row lyear-wrapper">
- <div class="lyear-login">
- <div class="login-center">
- <div class="login-header text-center">
- <a > 中药材网站登录a>
- div>
- <form >
- <div class="form-group has-feedback feedback-left">
- <input type="text" placeholder="请输入用户名" class="form-control" name="userName" id="userName" />
- <span class="mdi mdi-account form-control-feedback" aria-hidden="true"> span>
- <span id="msg" style="color:red;">span>
- div>
- <div class="form-group has-feedback feedback-left">
- <input type="password" placeholder="请输入密码" class="form-control" id="password" name="password" />
- <span class="mdi mdi-lock form-control-feedback" aria-hidden="true">span>
- <span id="msg2" style="color:red;">span>
- div>
- <div class="form-group has-feedback feedback-left row">
-
- <div class="col-xs-12">
- <select class="form-control" id="type" name="type" size="1">
- <option value="1">管理员option>
- <option value="2">用户option>
-
- select>
- div>
- div>
- <font color="red">${message}font>
- <div class="form-group">
- <button class="btn btn-block btn-primary" type="button" id="login" >立即登录button>
-
- div>
- form>
- <a class="btn btn-block btn-primary" type="button" href="LoginServlet?action=toRegister" >去注册a>
- <hr>
- <footer class="col-sm-12 text-center">
- footer>
- div>
- div>
- div>
-
- <script type="text/javascript">
- layui.use(['layer', 'form','jquery'], function(){
- var layer = layui.layer
- ,form = layui.form
- ,jquery=layui.jquery;
- $("#login").on("click", function() {
- var userName = $("#userName").val().trim(); // trim()去除空格
- var password = $("#password").val().trim();
- var type = $("#type").val();
- if(!userName) {
- $("#msg").html("用户名不能为空!");
- $("#userName").focus(); // 聚焦
- return false;
- } else {
- if(!password) {
- $("#msg").html("密码不能为空!");
- $("#password").focus(); // 聚焦
- return false;
- } else {
- $("#msg").html("");
- }
- }
-
- $.ajax({
- type: "post", // post或get
- url: "LoginServlet?action=login", // 提交路径
- data: {
- userName: userName,
- password: password,
- type:type,
- },
- // dataType: "json", // 指定后台传来的数据是json格式
- success: function(data) {
-
- if (data == "yes") {
- layer.msg('登陆成功!', {icon: 6});
- // 延迟跳转
- window.setTimeout("window.location='LoginServlet?action=toMain'",900);
- } else if(data == 'toIndex'){
- layer.msg('登陆成功!', {icon: 6});
- window.setTimeout("window.location='IndexServlet?action=toIndex'",900);
-
- }else{
- layer.msg('账号或者密码错误,登录失败!', {icon: 5});
-
-
- }
-
- },
-
- error: function(err) {}
-
- })
-
- })
-
-
-
- });
- script>
-
- body>
- html>
- protected void login(HttpServletRequest request, HttpServletResponse response) throws Exception {//跳转到添加用户界�?
- String userName = request.getParameter("userName");
- String password = request.getParameter("password");
- String type = request.getParameter("type");
- String message = "no";
- if(type != null && type.equals("1")){//admin
- Admin admin = service.selectAdmin(userName,password);
- if (admin != null) {
- message = "yes";
- request.getSession().setAttribute("flag",1);
- request.getSession().setAttribute("admin",admin);
- }
-
- }else if(type != null && type.equals("2")){
- User user = service.selectUser(userName,password);
- if (user != null) {
- message = "toIndex";
- request.getSession().setAttribute("flag",2);
- request.getSession().setAttribute("user",user);
- }
-
- }
- response.getWriter().print(message);
- }