CookieOne.java
package com.cookie; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CookieOne */ @WebServlet("/CookieOne") public class CookieOne extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CookieOne() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); //创建一个cookie(api) Cookie cookie=new Cookie("name","xiaomi"); //设置cookie的生命周期 cookie.setMaxAge(3600);///3600秒一个小时 //把cookie信息写给浏览器 response.addCookie(cookie); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }View Code
ReadCookie.java
package com.cookie; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ReadCookie */ @WebServlet("/ReadCookie") public class ReadCookie extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ReadCookie() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); //读取所有cookie信息,选取 Cookie cookies[]=request.getCookies(); System.out.println(cookies.length); //遍历cookie for(int i=0;i<cookies.length;i++){ Cookie cookie=cookies[i]; out.println("cookie的信息 Name=:"+cookie.getName()+"Value="+cookie.getValue()); out.println(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }View Code
MD5加密后再进行数据库访问
MD5算法'
package com.cookie; public class MD5 { public static String getMD5(byte[] source) { String s = null; char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a', 'b','c','d','e','f'}; try { java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); md.update(source); byte tmp[] = md.digest(); char str[] = new char[16*2]; int k = 0; for (int i = 0; i < 16; i++) { byte byte0 = tmp[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } s = new String(str); } catch(Exception e) { e.printStackTrace(); } return s; } public static void main(String[] args) { String a = "I love you"; byte[] b = a.getBytes(); for (int i=0; i<b.length; i++) System.out.print(b[i] + " "); String str = MD5.getMD5(b); System.out.println("a = " + a + "\n" + "str = " + str + "\n"); } }View Code
实例:写出你上次登录时间
package com.cook; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CookieOne */ @WebServlet("/CookieOne") public class CookieOne extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CookieOne() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); Cookie []cookies=request.getCookies(); //保存上次登录的Cookie lasttime boolean b=false; if(cookies!=null){ for(Cookie cookie:cookies){ //取出名字 String name=cookie.getName(); if(name.equals("lasttime")){ out.print("你上次登陆的时间:"+cookie.getValue()); //更新时间 SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime= myFmt.format(new java.util.Date()); Cookie mycookie1=new Cookie("lasttime",nowTime); mycookie1.setMaxAge(7*3600*24); response.addCookie(mycookie1); b=true; break; } }} if(!b){ out.print("你是第一次登录"); //吧当前的日期保存到Cookie SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime= myFmt.format(new java.util.Date()); Cookie cookie1=new Cookie("lasttime",nowTime); cookie1.setMaxAge(7*3600*24); response.addCookie(cookie1); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }View Code
优化后的代码:
package com.cook; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CookieOne */ @WebServlet("/CookieOne") public class CookieOne extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CookieOne() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); Cookie []cookies=request.getCookies(); //保存上次登录的Cookie lasttime boolean b=false; if(cookies!=null){ for(Cookie cookie:cookies){ //取出名字 String name=cookie.getName(); if(name.equals("lasttime")){ out.print("你上次登陆的时间:"+cookie.getValue()); //更新时间 SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime= myFmt.format(new java.util.Date()); cookie.setValue(nowTime); cookie.setMaxAge(7*3600*24); response.addCookie(cookie); b=true; break; } }} if(!b){ out.print("你是第一次登录"); //吧当前的日期保存到Cookie SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime= myFmt.format(new java.util.Date()); Cookie cookie1=new Cookie("lasttime",nowTime); cookie1.setMaxAge(7*3600*24); response.addCookie(cookie1); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }View Code
打开登录页面,自动填写用户名与密码
LoinServlet.java
package com.view; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); //从Cookie中选出 String id=""; Cookie cookies[]=request.getCookies(); if(cookies!=null){ for(Cookie cookie:cookies){ if(cookie.getName().equals("id")){ id=cookie.getValue(); } } } out.println("<h1>用户登录</h1>"); out.println("<form action='/Cook/LoginCL' method='post'>"); out.println("用户id:<input type='text' name='id' value='"+id+"'></br>"); out.println("密 码:<input type='password' name='password'></br>"); out.print("<input type='checkbox' value='keep' name='keepinfo'/>在此电脑上保存<br/>"); out.println("<input type='submit' value='登录' '></br>"); out.println("</form>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }View Code
LoginCL.java
package com.Control; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginCL */ @WebServlet("/LoginCL") public class LoginCL extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginCL() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); //判断有没有选择 String val=request.getParameter("keepinfo"); String id=request.getParameter("id"); String passwors =request.getParameter("password"); if(val!=null&&val.equals("keep")){ //包含该用户 创建cooie并保存到 Cookie cookie=new Cookie("id",id); cookie.setMaxAge(7*24*3600); //回写 response.addCookie(cookie); request.getRequestDispatcher("/ok").forward(request, response); }else { request.getRequestDispatcher("/ok").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }View Code