JSP实验课(Cookie对象的应用实例)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/jjsjsjjdj/article/details/102610694

一、cookie的基本架构

1.交互页面

1.姓名–文本框
2.性别–单选框
3.爱好–复选框(用数组存放)
4.颜色–菜单栏
5.提交按钮

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingCookie.html' 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">
	-->

  </head>
  
  <body>
    <form  action="2.usingCookie.jsp" method="post">
    <table border="1">
       <tr>
          <td>姓名:</td>
          <td><input type="text" name="name"></td>
       </tr>

        <tr>
          <td>性别:</td>
          <td><input type="radio" name="sex" value="M" checked><input type="radio" name="sex" value="F">             
          </td>
       </tr>   
    
        <tr>
         <td>爱好</td>
         <td> 
                             
             <input type="checkbox" name="Habit" value="阅读"> 
                              看书
             <input type="checkbox" name="Habit" value="跑步">     
                              跑步
             <input type="checkbox" name="Habit" value="音乐"> 
                              音乐                 
          </td>
       </tr>   
    
    
        <tr>
          <td>喜好颜色:</td>
          <td>
              <select size="1" name="color">
                 <option  selected>none</option>
                 <option value="蓝色">蓝色</option>
                 <option value="绿色">绿色</option>
                 <option value="红色">红色</option>
                 <option value="黄色">黄色</option>
              </select>           
          </td>
          
          <td colspan="2" align="center">
             <input type="submit" value="发送资料">
          </td>
          
       </tr>       
    </table>
    </form>
    
  </body>
</html>

2.处理页面

1.getParameter获取提交表单信息
2.保存到Cookie对象
3.添加到response
4.response转跳

<%@ page language="java" import="java.util.*"  pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
   request.setCharacterEncoding("gbk");
 %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingCookie.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">
	-->

  </head>
  
  <body>
    <%
      String strnames=request.getParameter("name"); 
      String strname=java.net.URLEncoder.encode(strnames,"utf-8"); 
            
      String strsex=request.getParameter("sex");
      String strcolors=request.getParameter("color");
      String strcolor=java.net.URLEncoder.encode(strcolors,"utf-8"); 
      
      String[] strHabits=request.getParameterValues("Habit");
      String strHabit="";
      String t;
      for(int i=0;i<strHabits.length;i++)
      {
         t=java.net.URLEncoder.encode(strHabits[i],"utf-8");
         strHabit+=t+" "; 
      }
      
      Cookie nameCookie=new Cookie("name",strname);
      Cookie sexCookie=new Cookie("sex",strsex);
      Cookie colorCookie=new Cookie("color",strcolor);
      Cookie HabitCookie=new Cookie("Habit",strHabit);
      
      response.addCookie(nameCookie);
      response.addCookie(sexCookie);
      response.addCookie(colorCookie);
      response.addCookie(HabitCookie);
      
      response.sendRedirect("3.responseCookie.jsp");
     %>
  </body>
</html>

3.展示页面

1.判断cookie是否为null
2.依次获取判断获取对应的cookie值
3.展示到页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'responseCookie.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">
	-->

  </head>
  <body>
      <%
          Cookie cookies[]=request.getCookies();
          if(cookies==null)
             out.print("没有Cookie");      
          else{
             try{
                  if(cookies.length==0)
                  {
                    System.out.println("客户端禁止写入Cookie");
                  }
                  else
                  {
                    int count=cookies.length;
                    String name="",sex="",color="",Habit="";
                    
                    for(int i=0;i<count;i++)
                        if(cookies[i].getName().equals("name"))
                          name=cookies[i].getValue();
                        else 
                             if(cookies[i].getName().equals("sex"))
                                 sex=cookies[i].getValue();
                             else
                                 if(cookies[i].getName().equals("color"))
                                    color=cookies[i].getValue();
                                 else
                                     if(cookies[i].getName().equals("Habit"))
                                            Habit=cookies[i].getValue();               
       %> 
       <font color="<%=color%>" size="5"><%=java.net.URLDecoder.decode(name,"utf-8")%></font>
        的个人信息
       <p>
        <%
           out.println("性别:");
           if(sex.equals("M"))
               out.println("男");
           else
               out.println("女");
               
           out.println("<br> 我喜欢的颜色是:");
           out.println(java.net.URLDecoder.decode(color,"utf-8"));    
           
           out.println("<br> 我的爱好是:");
           out.println(java.net.URLDecoder.decode(Habit,"utf-8")); 
             }
           }
             catch(Exception e){
                System.out.println(e);
             } 
            }
         %>
         
       </p>
  </body>
</html>

二、特别注意

1.在cookie中不支持中文字符串的包装,需要用java.net.URLEncode编码 与 URLDecode解码问题

参考博客:java.net.URLEncode编码 与 URLDecode解码问题.

1.在处理页面进行编码

在这里插入图片描述

2.在展示页面进行解码

在这里插入图片描述

注意:为了防止乱码,将编码方式设为GBK

在这里插入图片描述

2.如何处理复选框的数据

1.通过遍历的方式,用空字符隔开,逐一URLEncode编码连接 ,展示的时候再解码

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jjsjsjjdj/article/details/102610694