上周完成了
效果如下
记住密码的复选框是通过
`<%
//
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);
String[] choice = request.getParameterValues(“savepwd”);
// 判断用户是否登陆成功
if (username.equals("邓益春") && password.equals("123456")){
Cookie uname = new Cookie("uname",username);
Cookie upwd = new Cookie("upwd", password);
Cookie savepwd = new Cookie("savepwd", "no");
if (choice != null) {
savepwd = new Cookie("savepwd", "yes");
}
response.addCookie(uname);
response.addCookie(upwd);
response.addCookie(savepwd);
// 采用重定向,跳转到登录成功页面
response.sendRedirect("success.jsp");
} else {
// 采用重定向,跳转到登录页面
response.sendRedirect("login.jsp");
}
%>`
修改登录(login.jsp)页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="do_login.jsp" method="post">
<table border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">用户名</td>
<td><input id="uname" type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密 码</td>
<td><input id="upwd" type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input id="savename" type="checkbox" name="save" value="记住用户名"/>记住用户名
<input id="savepwd" type="checkbox" name="save" value="记住密码" onclick="setchkname()"/>记住密码
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
<%
String uname = "";
String upwd = "";
String savepwd = "";
String savename = "";
Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies) {
if (cookie.getName().equals("uname")) {
uname = cookie.getValue();
}
if (cookie.getName().equals("upwd")) {
upwd = cookie.getValue();
}
if (cookie.getName().equals("savepwd")) {
savepwd = cookie.getValue();
}
if (cookie.getName().equals("savename")){
savename = cookie.getValue();
}
}
%>
<script type="text/javascript">
var chkSavepwd = document.getElementById("savepwd");
var chkSavename = document.getElementById("savename");
var savepwd = "<%= savepwd %>";
var savename = "<%= savename %>";
if (savepwd == "yes") {
chkSavepwd.checked = true;
}
if (savename == "yes") {
chkSavename.checked = true;
}
var txtUname = document.getElementById("uname");
var txtUpwd = document.getElementById("upwd");
if (chkSavename.checked){
txtUname.value = "<%= new String(uname.getBytes("iso-8859-1"), "utf-8") %>";
}
if (chkSavepwd.checked){
txtUpwd.value = "<%= upwd %>";
}
/**
* 选中记住密码,一定会选中记住用户名
*/
function setchkname() {
if (chkSavepwd.checked){
chkSavename.checked = true;
}
}
</script>
</body>
</html>
修改登陆(do_login)页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="do_login.jsp" method="post">
<table border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">用户名</td>
<td><input id="uname" type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密 码</td>
<td><input id="upwd" type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input id="savename" type="checkbox" name="save" value="记住用户名"/>记住用户名
<input id="savepwd" type="checkbox" name="save" value="记住密码" onclick="setchkname()"/>记住密码
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
<%
String uname = "";
String upwd = "";
String savepwd = "";
String savename = "";
Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies) {
if (cookie.getName().equals("uname")) {
uname = cookie.getValue();
}
if (cookie.getName().equals("upwd")) {
upwd = cookie.getValue();
}
if (cookie.getName().equals("savepwd")) {
savepwd = cookie.getValue();
}
if (cookie.getName().equals("savename")){
savename = cookie.getValue();
}
}
%>
<script type="text/javascript">
var chkSavepwd = document.getElementById("savepwd");
var chkSavename = document.getElementById("savename");
var savepwd = "<%= savepwd %>";
var savename = "<%= savename %>";
if (savepwd == "yes") {
chkSavepwd.checked = true;
}
if (savename == "yes") {
chkSavename.checked = true;
}
var txtUname = document.getElementById("uname");
var txtUpwd = document.getElementById("upwd");
if (chkSavename.checked){
txtUname.value = "<%= new String(uname.getBytes("iso-8859-1"), "utf-8") %>";
}
if (chkSavepwd.checked){
txtUpwd.value = "<%= upwd %>";
}
/**
* 选中记住密码,一定会选中记住用户名
*/
function setchkname() {
if (chkSavepwd.checked){
chkSavename.checked = true;
}
}
</script>
</body>
</html>
得到效果
记住密码,就默认记住用户名
331

被折叠的 条评论
为什么被折叠?



