再網上找了 類似功能的代碼 Copy 修改之後能用了 因此貼出來了,做個筆記...
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String name = null;
String pass = null;
Cookie[] cookies = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++) {
if(cookies[i].getName().equals("user")){
name = cookies[i].getValue().split("-")[0];
pass = cookies[i].getValue().split("-")[1];
}
}
if(name == null){
name = "";
}
if(pass == null){
pass = "";
}
%>
<html>
<head>
<script type="text/javascript">
function check(){
var tmp = document.getElementById("ck1").checked;
if(tmp != "false"){
document.getElementById("oper").disabled=false;
}else{
document.getElementById("oper").disabled=true;
}
}
window.onload=function(){
if(<%=name %> != null){
document.getElementById("ck").checked = true;
}
}
</script>
<base href="<%=basePath%>">
<title>登入介面</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
<p> </p>
<form action="checkLogin.jsp" >
<table width="388" border="0" align="center" cellspacing="1" bordercolor="#3399CC">
<tr align="center">
<td colspan="2" height="59"><font size="6"><b><font color="#330099" size="5">管理員登入</font></b></font>
</td>
</tr>
<tr>
<td width="96" align="right">通行證:</td>
<td width="154">
<input type="text" name="name" size="15" value="<%=name %>" />
</td>
</tr>
<tr>
<td width="96" align="right">口令:</td>
<td width="154">
<input type="password" name="pass" size="15" value="<%=pass %>" />
</td>
</tr>
<tr>
<td width="96" align="right"></td>
<td width="154">
<input type="checkbox" name="ck" value="1"/> 記住密碼
</td>
</tr>
<tr>
<td width="96" align="right"></td>
<td width="154">
<input type="checkbox" name="ck1" value="1" id="ck1" /> <a href="#">我同意以上條款</a>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="oper" value=" 登 錄 "/>
<input type="reset" name="reset" value=" 複 位 ">
</td>
</tr>
</table>
</form>
</body>
</html>
checkLogin.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String ck = request.getParameter("ck");
if(ck == null){
ck = "0";
}
if (ck.equals("1")) {
try {
Cookie user = new Cookie("user", name + "-" + pass);
user.setMaxAge(60);
response.addCookie(user);
} catch (Exception e) {
e.printStackTrace();
}
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'checkLogin.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page.
<br>
</body>
</html>
PS: 直接發布在Tomcat下面 運行即可...