好久没写东西了 是太忙了呢 ? 哎一言难尽啊 什么时候学炒黄金不好偏偏要在学javaweb的时候炒黄金 炒的身败名裂 家徒四壁。好吧废话不多说 这个cookie 就是那段时间不好好学习造成后来。。。。哎 添加cookie代码
以下包含添加cookie 删除cookie 以及部分js代码
用户登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
function check(){
var a = document.getElementById("username").value;
if(a==""){
alert("用户名不能为空");
return false;
}
var b = document.getElementById("password").value;
if(b==""){
alert("密码不能为空");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="${pageContext.request.contextPath }/TestCookie" method="get" οnsubmit="return check()">
<table>
${requestScope.message }
<thead>用户注册界面</thead>
<tr>
<td>
用户名
</td>
<td>
<input type="text" id="username" name="username">
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="password" id="password" name="password">
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="提交"/>
</td>
<td><input type="button" οnclick="window.location='${pageContext.request.contextPath }/deleteCookie'" value="取消自动登录"/></td>
<td><input type="checkbox" name="autoLogin" value="yes"/>下次自动登录</td>
</tr>
</table>
</form>
</body>
</html>
TestCookie处理用户请求
package com.cookie;
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;
public class TestCookie extends HttpServlet {
//恒心 专注 勿扰
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
System.out.println("1aaaa");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
Testproperties testproperties = new Testproperties();
String username = testproperties.show("username");
String password = testproperties.show("password");
Cookie[] cookies = request.getCookies();
boolean flag = false;
if(cookies!=null){//调试专用
for(int i = 0 ; i <cookies.length;i++){
System.out.println("------------"+cookies[i].getName()+"---------"+cookies[i].getValue());
}
for(int i = 0 ; i <cookies.length;i++){
if(cookies[i].getName().equals("username")){
if(cookies[i].getValue().equals(username)){
for(int j = 0 ; j<cookies.length;j++){
if(cookies[j].getName().equals("password")){
if(cookies[j].getValue().equals(password))
{
System.out.println("匹配成功");
flag = true;//此时找到username和密码并且比对成功
request.setAttribute("message", "登录成功");
request.getRequestDispatcher("/message.jsp").forward(request, response);
break;
}
}
}
}
}
}
}
if(!flag){//如果找不到cookie或者cookie中的用户名密码不匹配的话
if(username.equals(request.getParameter("username"))&&password.equals(request.getParameter("password"))){
System.out.println("客户端闯过来:"+request.getParameter("username")+""+request.getParameter("password"));
request.setAttribute("message", "登录成功");
if(request.getParameter("autoLogin")!=null){
boolean autoLogin = request.getParameter("autoLogin").equals("yes");
//没有自动登录这个模块时才会让用户选择要不要使用cookie自动登录
if(autoLogin){
Cookie cookie = new Cookie("username", username);
Cookie cookie2 =new Cookie("password",password);
System.out.println("添加时 usernmae "+username+"和 password"+password);
cookie.setPath("/");
cookie.setMaxAge(7000000);
cookie2.setPath("/");//设置路径和有效期
cookie2.setMaxAge(7000000);
response.addCookie(cookie);
response.addCookie(cookie2);
}
}
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
else{
request.setAttribute("message","登录失败");
request.getRequestDispatcher("/UserLogin.jsp").forward(request, response);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
删除cookie
package com.cookie;
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;
public class deleteCookie extends HttpServlet {
//恒心 专注 勿扰
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
for(int i = 0 ; i <cookies.length;i++){
if(cookies[i].getName().equals("username")||cookies[i].getName().equals("password")){
cookies[i].setMaxAge(0);//删除cookie必备
cookies[i].setPath("/");//起先没加这句 简直就是删不掉啊。。。
System.out.println("爱上大沙发i康师傅嘎斯");//调试
response.addCookie(cookies[i]);
}
}
for(int i = 0 ; i <cookies.length;i++){
System.out.println("------------"+cookies[i].getName()+"---------"+cookies[i].getValue());//调试
}
request.getRequestDispatcher("/UserLogin.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}