怎么在java中使用cookie
2个回答
展开全部
<%--客户端添加Cookie信息--%>
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <html:base /> <title>客户端添加Cookie信息</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> <body> <% response.addCookie(new Cookie("Cookie1","firstCookie")); response.addCookie(new Cookie("Cookie2","secondCookie")); %> <a href="JSP_getCookie.jsp">getcookie</a> </body></html>
<%--request获取客户端Cookie信息,在工程下新建JSP_getCookie.jsp--%>
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html:html locale="true"> <head> <html:base /> <title>JSP_getCookie.jsp</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> <body> <% Cookie cookie[]=request.getCookies(); String Cookiename1=""; String Cookiename2=""; if(cookie!=null){ for(int i=0;i<cookie.length;i++){ if(cookie[i].getName().equals("Cookie1")){ Cookiename1=cookie[i].getValue(); } if(cookie[i].getName().equals("Cookie2")){ Cookiename2=cookie[i].getValue(); } } %> <% out.println("Cookie1的值:"+Cookiename1); } %> <br/> <% out.println("Cookie2的值:"+Cookiename2); %>
</body></html:html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询