web —— jsp笔记

cookies 的使用

 1、首先确保客户机上允许使用cookie,一般在浏览器的 隐私 设置项里可以设置。

 2、下面给出具体的例子

  a)index.jsp中如果没注册过,让游客注册,如果注册了,拿出贮存在cookie中的注册信息显示出来

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="utf8"%>
<%@page import="java.util.Date" %>
<%@page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLDecoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">

<title>First Jsp/ show system time</title>
</head>
<body onload="setInterval(updateTime(), 1000)">
    <%!
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String updateTime(){
            Date date = new Date();
         return  df.format(date);
        }
        
    %>
    当前时间:<%= updateTime() %>
    <br/><br/>
    <% 
        Cookie[] cookies = request.getCookies();
        String user="";
        String date="";
        String date2="";
        if(cookies != null)
        {
            for(int i=0;i<cookies.length;i++)
            {
                if(cookies[i].getName().equals("mrCookie"))
                {
                    user = URLDecoder.decode(cookies[i].getValue().split("#")[0], "gbk") ;
                    date = cookies[i].getValue().split("#")[1];
                    date2 = cookies[i].getValue().split("#")[2];
                }
            }
        }
        if("".equals(user)&& "".equals(date)){
    %>
            游客,你好,欢迎光临!
            <form action = "deal.jsp" method = "post">
                请输入姓名:<input type="text" name="user"/>
                <input type= "submit" value="确认">
            </form>
    <%
        }else{
    %>
            欢迎,[<b><%= user %></b>] 再次光临!<br/>
            您的注册时间为:<%= date %><br/>
            cookies有效期至:<%= date2 %>
    <% 
        }
    %>
</body>  
</html>
index.jsp 

  b)deal.jsp 写入cookies 信息

<%@ page language="java" contentType="text/html; charset=utf8"
    pageEncoding="utf8"%>
<%@ page import="java.net.URLEncoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Insert title here</title>
</head>
<body>
    <% 
        request.setCharacterEncoding("GB18030");
        String user = URLEncoder.encode(request.getParameter("user"), "gbk");
        java.util.Date date_start = new java.util.Date();
        long times= date_start.getTime();
        java.util.Date date_end = new java.util.Date(times+20*1000);
        
        Cookie cookie = new Cookie("mrCookie",user+"#"+date_start.toLocaleString()+"#"+date_end.toLocaleString());
        cookie.setMaxAge(20);
        response.addCookie(cookie);
    %>
    <a href="index.jsp">回到index.jsp页面</a>
</body>
</html>
deal.jsp

 【注】1、deal.jsp 中的 request.setCharacterEncoding("GB18030"); 编码设置应该与index.jsp中的第一行的 @page charset=GB18030" 一致

    2、Decode 和 encode 应该使用同种或兼容的编码(gbk?gb18030? 好像兼容?)

解决中文乱码(jsp内置的request传递参数包含中文时)

方法有两个:

1、<% request.setCharacterEncoding("utf8"); %> //在request指明发出请求页的编码

2、new String(request.getParameter("name").getBytes("ISO-8859-1"), "utf8"); //对于每一个request字段,使用String的新编码构造函数,指定目标编码;

原文地址:https://www.cnblogs.com/BensonLaur/p/5729756.html