`
yanfaguanli
  • 浏览: 660855 次
文章分类
社区版块
存档分类
最新评论

cookie记住密码

 
阅读更多


login.java::

package com.hsst.util;
import java.sql.*;
public class Login {

/**
* @param args
*/
private Connection conn=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
private boolean flag=false;
private SqlConn db=null;

public Login(){
//db=new SqlConn();
//conn=db.getconn();
}
//关闭方法
public void finalize(){
db.closemethod();
}
//验证用户名密码是否存在
public boolean checkNamePwd(String name,String pwd){

if(name.equals("admin")&pwd.equals("admin")){
flag=true;
}


return flag;
}

public static void main(String[] args) {
//new Login().checkNamePwd("admin", "admin");
}

}
==============================================

LoginServlet.java:

package com.hsst.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uname=request.getParameter("userno");
String pwd=request.getParameter("password");
String locals=request.getParameter("locals");
HttpSession session=request.getSession(true);
boolean flag=new com.hsst.util.Login().checkNamePwd(uname, pwd);
if(flag){
//设置cookie
if(request.getParameter("send")!=null && locals!=null)
{
Cookie cookie=new Cookie(uname, pwd);
cookie.setPath("/");
cookie.setMaxAge(365*24*60*60);
response.addCookie(cookie);
response.sendRedirect(request.getContextPath()+"/ok.jsp");
}
//删除cookie
else if(request.getCookies()!=null){
System.out.println(request.getCookies());
Cookie deletecookie=new Cookie(uname, null);
deletecookie.setPath("/");
deletecookie.setMaxAge(0);
response.addCookie(deletecookie);
response.sendRedirect(request.getContextPath()+"/ok.jsp");
}else{
response.sendRedirect(request.getContextPath()+"/ok.jsp");
}
}
else{
session.setAttribute("errors", "用户名密码错误");
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

}
========================================================================================

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312" contentType="text/html; charset=gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.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>

<%
Cookie cookies[]=request.getCookies();
Cookie sCookie=null;
String svalue=null;
String sname=null;
if(cookies!=null){
%>
<script language="javascript">
var UserNo=new Array(); UserNo[0]="";
var Password=new Array(); Password[0]="";
</script>
<%
for(int i=0;i<cookies.length;i++)
{
sCookie=cookies[i];
//if(sCookie.getName().equals(request.getParameter("userno"))){
// out.print("checked='true'");
//}
%>
<script language="javascript">
UserNo[<%=i%>]="<%=sCookie.getName()%>";
Password[<%=i%>]="<%=sCookie.getValue()%>";
</script>
<%
out.print(sCookie.getName()+" "+sCookie.getValue()+"</br>");
}
}
//取消的时侯再重写一次cookie 加一个标识,登陆时再判断这个标识就行了
%>
<body>
<form id="form1" name="form1" method="post" action="<%=path %>/servlet/LoginServlet">
用户名:<input type="text" name="userno" /><br>
密&nbsp;&nbsp;码:<input type="text" name="password" onclick="return oUserNo_onchange()"/><br>
<input type="checkbox" name="locals" value="s1">&nbsp;记住密码<br>
<input type="submit" name="send" value="提交" />
</form>
<%
String errors=(String)session.getAttribute("errors");
if(errors!=null){
out.print("<font color='red'>用户名或密码错误!或不存在!</font>");
session.removeAttribute("errors");
}

%>
</body>
</html>
<script language=javascript>

function oUserNo_onchange()
{
for(i=0;i<UserNo.length;i++)
if(form1.userno.value==UserNo[i]){
form1.password.value=Password[i];
form1.locals.checked=true;
}
}
</script>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics