基于JSP + SERVLET+MVC 实现用户登录

1、login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>登陆</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
html, body {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
	 background-image: url("images/1.png") ;
      overflow: hidden;
    }
canvas{
	position: absolute;
	top: 0;
	z-index: -1;
}
#_top {
	text-align: center;
	height: 100px;
	font-size: 30px;
	padding-top: 30px;
	letter-spacing: 10px; /*字与字之间的间距*/
}
#_top span{
	color: aliceblue;
}

.div_body {
	height: 420px;
	width: 30%;
	margin: 10% auto;
	position: relative;
	background: #fff;
	border-radius: 6px;
	z-index: 2;
}
#_body {
	width: 100%;
	text-align: center;
}
.msg{
	padding-top: 20px;
	color: #374b5d;
	height: 40px;
}
table{
	margin: 0 10%;
	width: 100%;
	height: 85%;
}
table tr{
	width: 100%;
	height: 15%;
}
table tr td{
	font-size: 20px;
	width: 100%;
}
.input{
	width: 80%;
	height: 55%;
	border-radius: 10px;
	text-align: center;
	font-size: 15px;
	border:1px solid #ccc;
	color: #ccc;

}
.btn {
    color: #ffffff;
    background-color: #458be5;
    border-color: #5da5f8;
		width:20%;
		margin-right: 8%;
}
.check{
	width: 13%;

}
</style>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/particle-bg.umd.min.js"></script>

<script type="text/javaScript">
window.onload = function() {
	particleBg('body', {
    color: 'rgba(255, 255, 255, 0.5)'
  });
}
function reloadCode() {
	const time=new Date().getTime();
	document.getElementById("imageCode").src="<%= request.getContextPath()%>/imageServlet??d="+time;
}


</script>
</head>

<body>
	<div class="div_body">
		<div id="_body">
		<h3 class="msg" style="cursor:default">${massage }</h3>
			<form action="ServletLogin" method="post"
				onsubmit="return checked(this)">
				<table>
					<ul class="nav navbar-nav " style="border-bottom:#517fa4 2px">
						<li>登录/li>
					</ul>
					<tr>
						<td><input class="input" type="text" name="ID" placeholder="账号">
						</td>
					</tr>
					<tr>
						<td><input class="input" type="password" name="pwd" placeholder="密码">
						</td>
					</tr>
					<tr>
						<td><input type="text" name="random" placeholder="验证码"/>
						<img alt="验证码" id="imageCode" src="<%= request.getContextPath()%>/imageServlet" style="margin-left: 40px;margin-top:-9px"  />
						</td>
						<td><a href="javascript:reloadCode()">看不清</a></td>
					</tr>
					<tr>
						<td>
							<input class="check" type="radio" name="role" value="0" checked="checked">学生
							<input class="check" type="radio" name="role" value="1">教师
							<input class="check" type="radio" name="role" value="2">管理员
						</td>
					</tr>
					<tr>
						<td>
							<input class="btn" type="submit" value="登录">
							<input class="btn" type="button" name="register" value="注册"
							onclick="window.open('register/register.jsp')">
							<input class="btn" type="reset" value="取消">
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</body>
</html>

2、loginServlet

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.Impl.UserServiceImpl;


import model.User;


public class ServletLogin extends HttpServlet{
	private User user;
	private UserServiceImpl userServiceImpl=new UserServiceImpl();
	
	public void doGet(HttpServletRequest request,HttpServletResponse response)
		throws ServletException,IOException {
		//验证码部分逻辑
		String randomImage = (String) request.getSession().getAttribute("randomImage");
		String imageText = request.getParameter("random");

		String num = request.getParameter("ID");
		String pwd = request.getParameter("pwd");
		int roleID = Integer.parseInt(request.getParameter("role"));
		user = new User(num, pwd, roleID);
		User rs = userServiceImpl.login(user);
			if (rs.getUserName() != null) {

				if (rs.getUserNum().equals(user.getUserNum())) {
					if (rs.getPassword().equals(user.getPassword())) {
						if (imageText.equals(randomImage)) {
							if (rs.getRoleID() == user.getRoleID()) {
								if (rs.getRoleID() == 2) {
									request.getSession().setAttribute("username", rs.getUserName());
									request.getSession().setAttribute("usernum", rs.getUserNum());
									request.getRequestDispatcher("admin/admin.jsp").forward(request, response);
								} else if (rs.getRoleID() == 1) {
									request.getSession().setAttribute("username", rs.getUserName());
									request.getSession().setAttribute("usernum", rs.getUserNum());
									request.getRequestDispatcher("teacher/teacher.jsp").forward(request, response);
								} else if (rs.getRoleID() == 0) {
									request.getSession().setAttribute("username", rs.getUserName());
									request.getSession().setAttribute("usernum", rs.getUserNum());
									request.getRequestDispatcher("student/student.jsp").forward(request, response);
								}
							} else {
								request.setAttribute("massage", "登录的权限不同,请重新输入!!");
								request.getRequestDispatcher("login/login.jsp").forward(request, response);
							}
						} else {
							request.setAttribute("massage", "验证码错误!!");
							request.getRequestDispatcher("login/login.jsp").forward(request, response);
						}
					} else {
						request.setAttribute("massage", "密码不正确!!");
						request.getRequestDispatcher("login/login.jsp").forward(request, response);
					}
				}

			} else {
				request.setAttribute("massage", "用户不存在!!");
				request.getRequestDispatcher("login/login.jsp").forward(request, response);
			}
		}

	
	public void doPost(HttpServletRequest request,HttpServletResponse response)
			throws ServletException,IOException{
			doGet(request,response);
		}
}

3、实体user

package model;

public class User {
	private int userID;
	private String userNum;
	private String userName;
	private String password;
	private String phone;
	private int roleID;
	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	public String getUserNum() {
		return userNum;
	}
	public void setUserNum(String userNum) {
		this.userNum = userNum;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public int getRoleID() {
		return roleID;
	}
	public void setRoleID(int roleID) {
		this.roleID = roleID;
	}
	public User(int userID, String userNum, String userName, String password,
			String phone, int roleID) {
		super();
		this.userID = userID;
		this.userNum = userNum;
		this.userName = userName;
		this.password = password;
		this.phone = phone;
		this.roleID = roleID;
	}
	public User() {
		super();
		
	}
	public User(String userNum, String userName, String password, String phone,
			int roleID) {
		super();
		this.userNum = userNum;
		this.userName = userName;
		this.password = password;
		this.phone = phone;
		this.roleID = roleID;
	}
	public User(String userNum, String password, int roleID) {
		super();
		this.userNum = userNum;
		this.password = password;
		this.roleID = roleID;
	}
	
	
}

4、运行截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值