`
直线曲线
  • 浏览: 46280 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

利用jsp操作cookies

    博客分类:
  • jsp
 
阅读更多
中午吃完饭,将上午完成的用jsp操作cookies的小test粘贴到这里。虽然是很陈旧的知识了,但是毕竟是一次学习。放到这里,希望给予需要的人以帮助。
基本目的为:选择‘保存’并填写input内容后提交,能够在cookie中保存信息,再次打开index页面时,可以达到不用再次输入input的目的。
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="javax.servlet.http.Cookie" %>
<%@ page language="java" import="java.net.*" %>
<%
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>cookie test</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">
	-->
	<script type="text/javascript" src="<%=basePath%>/js/jquery-1.3.2.min.js"> </script>
	<script type="text/javascript" src="<%=basePath%>/js/jquery-pack.js"></script>
	  
<% //遍历所有cookies,判断所用到的cookies的取值 。
				 Cookie[] myCookie = request.getCookies();
				 int checked = 0;
				 String userName = "";
				 if(myCookie!=null && myCookie.length!=0){
					 for(int n=0; n<=(myCookie.length-1); n++){
						    Cookie newCookie = myCookie[n];
						    if(newCookie.getName().equals("save")){
						    	if(null != newCookie.getValue()){
						    		checked = 1;
						    		//解码。
						    		userName = URLDecoder.decode(newCookie.getValue(),"utf-8");						    	
}else{
						    		checked = 0;
						    	}
						     }				      
						 }			 
				 }			
				%>
	<script type="text/javascript">
	$(function(){
		//依据checked,判断是否选否选择保存cookie
				if(<%=checked%>==1){
					$('#saveCookie').attr('checked', 'checked');
					$('#username').val("<%=userName%>");
				}else{
					 $('#saveCookie').removeAttr('checked');
					}
			});
	</script>

  </head>

  <body>

					
    <br/>
    <br/>
    <div align="center">
    	<div>
    		<form id="form" action="doCookie.jsp" method="post">
   			<lable>用户名</lable>
   			<input id="username" name="username" type="text"></input>
    		<label>保存cookie</label>
    		<input id="saveCookie" name="saveCookie" type="checkbox"/>
    		<input id="button" type="submit" value="提交"/>
    		</form>
    	</div>
    	<br/>
    </div>
  </body>

</html>

doCookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="javax.servlet.http.Cookie" %>
<%@ page language="java" import="java.net.*" %>
<%
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 'saveCookie.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">
	-->
  	<%//处理cookies。
  						String saveCookie = request.getParameter("saveCookie");
  						//已经是中文了
  						String userName = new String(request.getParameter("username").getBytes("ISO8859-1"),"utf-8");						Cookie cookie = null;
						if(saveCookie != null){
							//为了让cookie中能够添加中文,在进行一次包装。
							cookie = new Cookie("save",URLEncoder.encode(userName,"utf-8")); 							cookie.setMaxAge(365*24*60);
							
						}else{
							cookie =new Cookie("save",null); 
							cookie.setMaxAge(0);
						}	
						cookie.setPath("/");
						response.addCookie(cookie);
	%>
						
  </head>

  					
						
  
  <body>
  <!-- 自动跳转 -->
	<script type="text/javascript">
		window.location = "index.jsp";
	</script>
  </body>
</html>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics