Jsp 记住密码

来源:互联网 发布:青岛海尔软件怎么样 编辑:程序博客网 时间:2024/06/07 12:18

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.getElementByIdx_x("ck1").checked;  
 if(tmp != "false"){
  document.getElementByIdx_x("oper").disabled=false;
 }else{
  document.getElementByIdx_x("oper").disabled=true;
 }
}
window.onload=function(){
 if(<%=name %> != null){
  document.getElementByIdx_x("ck").checked = true;
 
}
</script>

<base href="<%=basePath%>">
<title>登录界面</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>

<body>
<p>&nbsp;</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"/> &nbsp;记住密码
    </td>
  </tr>
    <tr>
   <td width="96" align="right"></td>
    <td width="154"> 
      <input type="checkbox" name="ck1" value="1" id="ck1" /> &nbsp;<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下面 运行即可...

原创粉丝点击