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>
相关推荐
利用js+Cookie实现的记住用户名、密码功能,简单、实用、无后台繁琐代码,新手亦可看懂。
前端网页登录通过cookie记住密码的demo源码,开发网站的登录页会用到
js操作cookie,记住用户名,密码。
cookie的发生,到底需要如何做到,不cookie的发生,又会如何产生。 莎士比亚曾经说过,人的一生是短的,但如果卑劣地过这一生,就太长了。带着这句话,我们还要更加慎重的审视这个问题。
记住密码js 存储cookie记住密码记住密码js
cookie实现记住用户名密码(IntelliJ IDEA+tomcat)完整项目代码
cookie是一种WEB服务器通过浏览器在访问者的硬盘上存储信息的手段。Cookie的目的就是为用户带来方便,为网站带来增值。这篇文章主要介绍了JavaWeb 中Cookie实现记住密码的功能示例,需要的朋友可以参考下
主要给大家介绍了Vue+element+cookie记住密码功能的简单实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://zhaoyi105.iteye.com/blog/2188775
全部js,实现记住密码的功能。里面主要用的是cookie。
java cookie 读写,记住密码 自动登录
用户登录功能(cookie实现),user用户信息表中读取信息,并进行判断,利用cookie实现。
很好用,可在服务器端记住用户名密码,修改COOKIE轻松搞定
完全用JS实现的读取cookie(记住账号密码)源码。
使用jsp+Servlet+cookie实现一个登录功能,登录时可以选中记住密码。
Cookie实现记住密码及解决中文乱码
本篇文章主要介绍了vue项目实现记住密码到cookie功能示例(附源码),具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了java web中使用cookie记住用户的账号和密码的相关资料,需要的朋友可以参考下
NULL 博文链接:https://492664447-qq-com.iteye.com/blog/1565952