Skip to content

Commit

Permalink
优化登录界面
Browse files Browse the repository at this point in the history
  • Loading branch information
Crearns committed Jan 19, 2018
1 parent b147df3 commit 5391627
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/Action/LoginAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginAction extends HttpServlet {
UserDao udao = new UserDao();
Expand All @@ -29,24 +30,26 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = null;
String password = null;
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
username = request.getParameter("username");
password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
HttpSession session = request.getSession();
if(udao.login(user)){
String result = udao.login(user);
if(result.equals("true")){
session.setAttribute("adminname", user.getName());
response.sendRedirect("/Library/main.jsp");
request.getRequestDispatcher("/main.jsp").forward(request, response);
}
else{

out.write(result);
}
}

private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
HttpSession session = request.getSession();
session.setMaxInactiveInterval(0);
session.setMaxInactiveInterval(1);
response.sendRedirect("/Library/index.jsp");
}

Expand Down
13 changes: 9 additions & 4 deletions src/Dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class UserDao extends DBConnect{


public boolean login(User u){
public String login(User u){
try {
Connection conn = super.getConnection();
String sql = "SELECT password,name FROM User WHERE user=?";
Expand All @@ -23,14 +23,19 @@ public boolean login(User u){
if (rs.next()) {
if (u.getPassword().equals(rs.getString("password"))){
u.setName(rs.getString("name"));
return true;
return "true";
}
else{
return "false";
}
}
else{
return "false";
}
} catch (Exception e) {
e.printStackTrace();
}
return false;

return null;
}

public User queryadminbyname(String s){
Expand Down
Binary file modified web/WEB-INF/classes/Action/LoginAction.class
Binary file not shown.
Binary file modified web/WEB-INF/classes/Dao/UserDao.class
Binary file not shown.
6 changes: 3 additions & 3 deletions web/WEB-INF/classes/MySQL/Library.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ CREATE TABLE `iolog` (

LOCK TABLES `iolog` WRITE;
/*!40000 ALTER TABLE `iolog` DISABLE KEYS */;
INSERT INTO `iolog` VALUES ('000001','2220161464',-1,'2018年01月18日 17时46分24秒',14,1),('000001','2220161464',-1,'2018年01月18日 17时46分37秒',7,1),('000001','2220161464',1,'2018年01月18日 17时48分43秒',NULL,1),('000001','2220161464',1,'2018年01月18日 17时49分03秒',NULL,1);
INSERT INTO `iolog` VALUES ('000001','1',-1,'2018年01月18日 17时46分24秒',14,1),('000001','1',-1,'2018年01月18日 17时46分37秒',7,1),('000001','1',1,'2018年01月18日 17时48分43秒',NULL,1),('000001','1',1,'2018年01月18日 17时49分03秒',NULL,1);
/*!40000 ALTER TABLE `iolog` ENABLE KEYS */;
UNLOCK TABLES;

Expand Down Expand Up @@ -103,7 +103,7 @@ CREATE TABLE `reader` (

LOCK TABLES `reader` WRITE;
/*!40000 ALTER TABLE `reader` DISABLE KEYS */;
INSERT INTO `reader` VALUES ('2220161464','123456','测试','测试',1,'测试','测试',1,1);
INSERT INTO `reader` VALUES ('1','123456','测试','测试',1,'测试','测试',1,1);
/*!40000 ALTER TABLE `reader` ENABLE KEYS */;
UNLOCK TABLES;

Expand Down Expand Up @@ -182,4 +182,4 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2018-01-18 17:57:28
-- Dump completed on 2018-01-18 21:05:06
2 changes: 1 addition & 1 deletion web/booklist.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<head>
<title>图书管理</title>
</head>
<body onload="a()">
<body>
<h1 align="center">欢迎进入图书馆管理系统</h1>
<jsp:include page="nav.html"/>
<table class="table">
Expand Down
21 changes: 20 additions & 1 deletion web/css/welcome.css → web/css/loginpage.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
margin-bottom:0px;
margin-right:0px;
margin-left:1240px;
height:235px;
height:250px;
width:410px;
background-color: white;
padding: 25px;
Expand All @@ -36,6 +36,20 @@
color:white;
}

.errorsubmit{
position:absolute;
margin-top:5px;
margin-bottom:0px;
margin-right:0px;
margin-left:90px;
width:400px;
color:red;
}

button{
width:335px;
}

#rightsize{
align:right;
color:white;
Expand All @@ -51,8 +65,13 @@
}

#labellogin{
position:absolute;
margin-left:95px;
margin-top:-10px;
text-align:center;
width:140px;
color:white;
font-size:20px;
}


22 changes: 11 additions & 11 deletions web/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/welcome.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/loginpage.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script>
<script src="${pageContext.request.contextPath}/js/loginScript.js"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<html>
Expand All @@ -31,16 +32,15 @@
</div>
<div class="loginform">
<div class="col-md-4 column">
<span id="labellogin">系统登录</span><br>
<form role="form" action="LoginAction?action=login" method="post">
<div class="form-group">
<input type="text" class="form-control" id="usernameinput" name="username" placeholder="账号"/>
</div>
<div class="form-group">
<input type="password" class="form-control" id="passwordinput" name="password" placeholder="密码"/>
</div>
<button type="submit" class="btn btn-primary" >登录</button>
</form>
<span id="labellogin">管理员系统登录</span><br>
<div class="form-group">
<input type="text" class="form-control" id="usernameinput" name="username" placeholder="用户名"/>
</div>
<div class="form-group">
<input type="password" class="form-control" id="passwordinput" name="password" placeholder="密码"/>
</div>
<button class="btn btn-primary" onclick="logincheck()">登录</button>
<span class="errorsubmit" id="checkinfo"></span>
</div>
</div>
</body>
Expand Down
61 changes: 61 additions & 0 deletions web/js/loginScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var xmlHttp=false;
function createXMLHttpRequest()
{
if (window.ActiveXObject) //在IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ee){
xmlHttp=false;
}
}
}
else if (window.XMLHttpRequest) //在非IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
}
function logincheck(){
var usernameinput = document.getElementById("usernameinput").value;
var passwordinput = document.getElementById("passwordinput").value;
createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange=logincheckResult; //设置回调函数
var url="LoginAction?action=login&username=" + usernameinput + "&password=" + passwordinput;
xmlHttp.open("POST",url,true); //向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(null);
}

function logincheckResult(){
var usernameinput = document.getElementById("usernameinput").value;
var passwordinput = document.getElementById("passwordinput").value;
if (xmlHttp.readyState==4 && xmlHttp.status==200){
var data= xmlHttp.responseText;
document.getElementById("checkinfo").innerHTML = "";
if(data == "false"){
document.getElementById("checkinfo").innerHTML = "用户名或密码错误";
}
if (usernameinput == ""){
document.getElementById("checkinfo").innerHTML = "用户名不能为空";
}
if (passwordinput == ""){
document.getElementById("checkinfo").innerHTML = "密码不能为空";
}
if (usernameinput == "" && passwordinput == ""){
document.getElementById("checkinfo").innerHTML = "用户名和密码不能为空";
}
if(document.getElementById("checkinfo").innerHTML == ""){
window.location.href="main.jsp";
}
}
}
2 changes: 1 addition & 1 deletion web/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<li class="nav-item">
<strong id="rightsize">Copyright &copy; 2018 <a href="#">Creams</a></strong>
</li>

</div>
</nav>
</body>
Expand Down

0 comments on commit 5391627

Please sign in to comment.