cookie记住密码
2013-11-07 09:18
239 查看
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>
密 码:<input type="text" name="password" onclick="return oUserNo_onchange()"/><br>
<input type="checkbox" name="locals" value="s1"> 记住密码<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>
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>
密 码:<input type="text" name="password" onclick="return oUserNo_onchange()"/><br>
<input type="checkbox" name="locals" value="s1"> 记住密码<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>
相关文章推荐
- tomcat6版本虚拟目录详细配置
- 同一台Windows机器中启动多个Memcached服务
- 基于独立服务器SVNServer配置
- 如何在SUN的sparc平台上显示系统信息(一)
- 网秦发家-隐性的sp赚钱的暴富
- Mac OS X上搭建本地SVN服务器
- 自定义表格控件(通过TabLayout+TabRow)获取表格所有数据,并对表格进行相关事件监听
- WinCE sqlserver连接串
- sqort函数用法总结
- java 工厂模式小结
- Java vs. Python (2): Data Types
- 日志Logger 2( 21)
- 数据插入
- 模块化设计-iOS团队协作开发 v1.0
- jquery的$.extend和$.fn.extend作用及区别
- 日志(20)
- ABAP --- for all entries in 用法
- 学习VS2010------自制对话框添加页面
- oracle用户管理--oracle学习第四天
- java.text包