可以先看看别的大佬写的了解下cookie,具体我也不知道怎么讲哈哈哈哈或或或!
然后自己写了个记住账号密码功能,附带查看cookie的值,删除cookie的功能!
首先,在index.jsp中写好登录界面,并添加获取cookie中账号和密码信息并放进input中。以下是代码:
<%--
Created by IntelliJ IDEA.
User: HSW
Date: 2018/4/20
Time: 11:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Cookie[] cookies = request.getCookies();
String admin = "";
String password = "";
if(cookies != null)
for(Cookie cookie : cookies){
if(cookie.getName().equals("admin"))
admin = cookie.getValue();
else if(cookie.getName().equals("pwd"))
password = cookie.getValue();
}
%>
<html>
<head>
<title>Cookie Login</title>
<style>
div{
width: 800px;
height: 800px;
margin: 200px auto;
}
h1{
width: 400px;
height: auto;
margin: 0 auto;
}
table{
width: 400px;
height: auto;
margin: 0 auto;
}
</style>
</head>
<body>
<form action="${pageContext.request.contextPath}/setcookie" method="post">
<div>
<h1>登录</h1>
<table>
<tr><td><label for="admin">用户名:</label></td>
<td><input type="text" id="admin" placeholder="请输入你的用户名" name="admin" value="<%=admin%>"></td></tr>
<tr><td><label for="pwd">密码:</label></td>
<td><input type="password" id="pwd" name="pwd" value="<%=password %>"></td></tr>
<tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>
</table>
</div>
</form>
</body>
</html>
将上面java代码字符串信息放进input框里面的方法就是像上面的这句一样:
value="<%=password %>">
每次访问这个页面时就先执行上面的java程序,第一次时没有账号信息,getCookies返回空,所以就不会自动填补input!
然后新建一个Servlet相当于下面的A服务器,将登录信息放进Cookie!
代码如下:
package cn.jxufe;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CookieSer extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
String admin = request.getParameter("admin");
String password = request.getParameter("pwd");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String datestr = format.format(new Date());
Cookie cookie = new Cookie("admin", admin);
Cookie cookie2 = new Cookie("pwd", password);
Cookie cookie3 = new Cookie("date", URLEncoder.encode(datestr,"utf-8"));
response.addCookie(cookie);
response.addCookie(cookie2);
response.addCookie(cookie3);
PrintWriter out = response.getWriter();
out.write("<html>");
out.write("<head>");
out.write("<title>WebCookie</title>");
out.write("</head>");
out.write("<body>");
out.write("账号密码已被添加进cookie");
out.write("<a href=\"/look\">查看登录信息</a>");
out.write("</body>");
out.write("</html>");
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doPost(request,response);
}
}
效果:
可以看到将账号、密码、最近一次登录时间都写进了cookie。
这时返回登录界面刷新下看看:
账号密码都被保存!
然后在上面的Servlet加了一个<a>跳转至另一个查看cookie信息的Servlet,名为LookCookieContent(url-pattern是/look), 代码:
package cn.jxufe;
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 java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
public class LookCookieContent extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
Cookie[] cookies = request.getCookies();
String admin = "暂无该项信息";
String password = "暂无该项信息";
String date = "暂无该项信息";
if(cookies != null)
for(Cookie cookie : cookies){
if(cookie.getName().equals("admin"))
admin = cookie.getValue();
else if(cookie.getName().equals("pwd"))
password = cookie.getValue();
else if(cookie.getName().equals("date"))
date = URLDecoder.decode(cookie.getValue(), "utf-8");
}
PrintWriter out = response.getWriter();
out.write("<html>\n" +
"<head>\n" +
" <title>Cookie Login</title>\n" +
" <style>\n" +
" div{\n" +
" width: 800px;\n" +
" height: 800px;\n" +
" margin: 200px auto;\n" +
" }\n" +
" h1{\n" +
" width: 400px;\n" +
" height: auto;\n" +
" margin: 0 auto;\n" +
" }\n" +
" table{\n" +
" width: 400px;\n" +
" height: auto;\n" +
" margin: 0 auto;\n" +
" }\n" +
" </style>\n" +
"</head>\n" +
"<body>\n" +
" <div>\n" +
" <table> \n" +
" <tr><td>账号:</td><td>" + admin +"</td></tr> \n" +
" <tr><td>密码:</td><td>" + password +"</td></tr>\n" +
" <tr><td>上次登陆时间:</td><td>" + date + "</td></tr>\n" +
" <tr><td colspan=\"2\"><a href=\"\">删除cookie</a></td></tr>\n" +
" </table>\n" +
" </div>\n" +
"</body>\n" +
"</html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
单相思的情场失意,原本糟糕的心情随着一晚上的代码时光慢慢变好,现在依旧没有丝毫睡意,这难道就是学习使我快乐吗?
我还是和我现任女友好好处吧晒出来一起养养眼
emm~继续讲,接下来就是删除cookie了,很简单就是设置maxAge为0就行了!
这个是自己写的无法正常删除cookie的代码(too young啊):
package cn.jxufe;
import javax.servlet.RequestDispatcher;
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;
import java.io.IOException;
@WebServlet(name = "DeleteCookieServlet")
public class DeleteCookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if(cookies != null)
for(Cookie cookie : cookies)
cookie.setMaxAge(0);
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
关于cookie的寿命信息:
在代码中,我们用Cookie.setMaxAge()设置Cookie的生命周期。在这个方法中如果没有参数的话,默认参数就是-1,表示是会话时期cookie,当结束会话或关闭浏览器就会删除。当设置了一个正值,即生命周期是这个正值的秒数,cookie将会保存至硬盘。而像上面那样直接设置为0,即直接删除该Cookie!
运行代码发现无法正常删除,查找原因发现是因为没有执行response.addCookie()将原来的cookie覆盖!
加上试试。
点击删除cookie,返回登录界面时还是有的,因为网页有缓存,刷新下就没有了!
还有Cookie.setPath()和Cookie.setDomain()的作用。
http://这里是domain/这里是path
上面可以看到url中的domain部分和path部分
cookie设定了domain和path之后,只允许同domain以及同path的页面访问。
可以看看这个博客:点击打开链接