@WebServlet("/servletLogin2")
public class ServletLogin2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("username");//获取用户名属性value
String psd = request.getParameter("password");//获取密码属性value
String remember = request.getParameter("remember");//获取记住密码属性属性value
if ("true".equals(remember)) {
Cookie username = new Cookie("username", name);
Cookie password = new Cookie("password", psd);
username.setMaxAge(60 * 60);
password.setMaxAge(60 * 60);
response.addCookie(username);
response.addCookie(password);
} else {
Cookie username = new Cookie("username", name);
Cookie password = new Cookie("password", psd);
username.setMaxAge(0);
password.setMaxAge(0);
response.addCookie(username);
response.addCookie(password);
}
if ("zzz".equals(name) && "111".equals(psd)) {
response.sendRedirect(request.getContextPath() + "/success.jsp");
} else {
response.sendRedirect(request.getContextPath() + "error.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
login2.jsp
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>管理员登录</title>
<!-- 1. 导入CSS的全局样式 -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
<script src="js/common.js"></script>
<%
String names = null;
String pass = null;
Cookie[] ck = request.getCookies();
for (int i = 0; i < ck.length; i++) {
if ("username".equals(ck[i].getName())) {
names = ck[i].getValue();
} else if ("password".equals(ck[i].getName())) {
pass = ck[i].getValue();
}
}
ArrayList<String> arr = new ArrayList<>();
arr.add(names);
arr.add(pass);
for (String s : arr) {
System.out.println(s);
}
request.setAttribute("arr", arr);
// request.getRequestDispatcher("/success.jsp").forward(request, response);
%>
</head>
<body>
<div class="container" style="width: 400px;">
<h3 style="text-align: center;">管理员登录</h3>
<form action="${pageContext.request.contextPath}/servletLogin2" method="get">
<div class="form-group">
<label for="user">用户名:</label>
<input type="text" name="username" value="${arr[0]}" class="form-control"
id="user"
placeholder="请输入用户名"/>
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" name="password" value="${arr[1]}" class="form-control"
id="password"
placeholder="请输入密码"/>
</div>
<input type="checkbox" value="true" name="remember"
checked><span> 记住密码</span>
<hr/>
<div class="form-group" style="text-align: center;">
<input class="btn btn btn-primary" type="submit" value="登录">
</div>
</form>
</div>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
nk rel="icon" href="../../favicon.ico">
<title>Dashboard Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/dashboard.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Dashboard</a></li>
<li><a href="#">Settings</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Help</a></li>
</ul>
<form class="navbar-form navbar-right">
<input type="text" class="form-control" placeholder="Search...">
</form>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar">
<li class="active"><a href="#">Overview <span class="sr-only">(current)</span></a></li>
<li><a href="#">Reports</a></li>
<li><a href="#">Analytics</a></li>
<li><a href="#">Export</a></li>
</ul>
<ul class="nav nav-sidebar">
<li><a href="">Nav item</a></li>
<li><a href="">Nav item again</a></li>
<li><a href="">One more nav</a></li>
<li><a href="">Another nav item</a></li>
<li><a href="">More navigation</a></li>
</ul>
<ul class="nav nav-sidebar">
<li><a href="">Nav item again</a></li>
<li><a href="">One more nav</a></li>
<li><a href="">Another nav item</a></li>
</ul>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header">主页</h1>
<div class="row placeholders">
<div class="col-xs-6 col-sm-3 placeholder">
<img src="img/demo.jpg" width="200" height="200" class="img-responsive" alt="Generic placeholder thumbnail">
<h4>登</h4>
<span class="text-muted">Something else</span>
</div>
<div class="col-xs-6 col-sm-3 placeholder">
<img src="img/demo.jpg" width="200" height="200" class="img-responsive" alt="Generic placeholder thumbnail">
<h4>录</h4>
<span class="text-muted">Something else</span>
</div>
<div class="col-xs-6 col-sm-3 placeholder">
<img src="img/demo.jpg" width="200" height="200" class="img-responsive" alt="Generic placeholder thumbnail">
<h4>成</h4>
<span class="text-muted">Something else</span>
</div>
<div class="col-xs-6 col-sm-3 placeholder">
<img src="img/demo.jpg" width="200" height="200" class="img-responsive" alt="Generic placeholder thumbnail">
<h4>功</h4>
<span class="text-muted">Something else</span>
</div>
</div>
<h2 class="sub-header">Section title</h2>
</div>
</div>
</div>
</div>
<script src="js/jquery.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="js/bootstrap.js"></script>
</body>
</html>
error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户名或密码错误,请重新登录!</h1>
</body>
</html>