Jsp 記住密碼

來源:互聯網
上載者:User

  再網上找了 類似功能的代碼 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>&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下面 運行即可...

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.