Servlet --cookie编码保存中文信息
        【摘要】 
                    
 Servlet --cookie保存中文信息
 
  
 
 一、cookie中文信息
 
 1、在cookie默认不支持保存中文,接收到中文参数会报错。
 
 2、可以使用url编码和解码方式,解决cookie不能保存中文的问题。
 
 二、实例
 
 1、使用url进行编码和解码
 
 
 package test...
    
    
    
     Servlet --cookie保存中文信息 
 一、cookie中文信息 
 1、在cookie默认不支持保存中文,接收到中文参数会报错。 
 2、可以使用url编码和解码方式,解决cookie不能保存中文的问题。 
 二、实例 
 1、使用url进行编码和解码 
   
    - 
     
      
     
     
      
       package test09_2Servlet;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       import java.io.IOException;
      
     
- 
     
      
     
     
      
       import java.io.PrintWriter;
      
     
- 
     
      
     
     
      
       import java.net.URLDecoder;
      
     
- 
     
      
     
     
      
       import java.net.URLEncoder;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       import javax.servlet.ServletException;
      
     
- 
     
      
     
     
      
       import javax.servlet.http.Cookie;
      
     
- 
     
      
     
     
      
       import javax.servlet.http.HttpServlet;
      
     
- 
     
      
     
     
      
       import javax.servlet.http.HttpServletRequest;
      
     
- 
     
      
     
     
      
       import javax.servlet.http.HttpServletResponse;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       public class EncodeServlet extends HttpServlet {
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      	
      
     
- 
     
      
     
     
      	public void doGet(HttpServletRequest request, HttpServletResponse response)
      
     
- 
     
      
     
     
      			throws ServletException, IOException {
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       		response.setContentType("text/html");
      
     
- 
     
      
     
     
      		PrintWriter out = response.getWriter();
      
     
- 
     
      
     
     
      		
      
     
- 
     
      
     
     
      
       	}
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      	
      
     
- 
     
      
     
     
      	public void doPost(HttpServletRequest request, HttpServletResponse response)
      
     
- 
     
      
     
     
      			throws ServletException, IOException {
      
     
- 
     
      
     
     
      		
      
     
- 
     
      
     
     
      		//设置响应的编码格式
      
     
- 
     
      
     
     
      
       		response.setContentType("text/html;charset=UTF-8");
      
     
- 
     
      
     
     
      		//设置请求的编码格式
      
     
- 
     
      
     
     
      
       		request.setCharacterEncoding("UTF-8");
      
     
- 
     
      
     
     
      		//获取输出的字符流
      
     
- 
     
      
     
     
      		PrintWriter out = response.getWriter();
      
     
- 
     
      
     
     
      		//获取用户提交的用户名
      
     
- 
     
      
     
     
      		String name = request.getParameter("name");
      
     
- 
     
      
     
     
      		//cookie的name如果是中文进行url编码
      
     
- 
     
      
     
     
      
       		name = URLEncoder.encode(name,"UTF-8");
      
     
- 
     
      
     
     
      		//创建cookie,保存用户名称到vlue中
      
     
- 
     
      
     
     
      		Cookie ck = new Cookie("ckname",name);
      
     
- 
     
      
     
     
      		//设置cookie过期时间为60分钟
      
     
- 
     
      
     
     
      
       		ck.setMaxAge(60*60);
      
     
- 
     
      
     
     
      		//设置cookie可以访问的路径为项目下的组件都可以访问
      
     
- 
     
      
     
     
      
       		ck.setPath(request.getContextPath());
      
     
- 
     
      
     
     
      		//保存cookie到本地
      
     
- 
     
      
     
     
      
       		response.addCookie(ck);
      
     
- 
     
      
     
     
      		
      
     
- 
     
      
     
     
      		//读取cookie,输出cookie信息。
      
     
- 
     
      
     
     
      
       		Cookie[] cc = request.getCookies();
      
     
- 
     
      
     
     
      		if(cc!=null){
      
     
- 
     
      
     
     
      			for(Cookie c:cc){
      
     
- 
     
      
     
     
      				String names = c.getName(); //输出cookie的name
      
     
- 
     
      
     
     
      				String value = c.getValue();//输出cookie的key
      
     
- 
     
      
     
     
      
       				value = URLDecoder.decode(value, "UTF-8");
      
     
- 
     
      
     
     
      				/*
      
     
- 
     
      
     
     
      
        * 客户端第一次登陆后,服务器端传输cookie并写到客户端。
      
     
- 
     
      
     
     
      
        第二次登陆,request请求传给服务端,但在这个request中是不包含maxage值的,所以服务端取到的值还是默认值-1。
      
     
- 
     
      
     
     
      
        也就是说设置完maxage值后,maxage不会再被服务端读取和修改了。这一过程由浏览器完成,浏览器判断maxage的值,
      
     
- 
     
      
     
     
      
        从而判断cookie是否过期。
      
     
- 
     
      
     
     
      
        */
      
     
- 
     
      
     
     
      				int maxAge = c.getMaxAge(); 
      
     
- 
     
      
     
     
      				//读取path永远是null。对于cookie来说,path是受保护的
      
     
- 
     
      
     
     
      				String path = c.getPath();
      
     
- 
     
      
     
     
      				
      
     
- 
     
      
     
     
      
       				out.print("<hr/>name:"+names+",value:"+value+",maxAge:"+maxAge+",path:"+path);
      
     
- 
     
      
     
     
      
       			}
      
     
- 
     
      
     
     
      
       		}
      
     
- 
     
      
     
     
      		
      
     
- 
     
      
     
     
      		
      
     
- 
     
      
     
     
      
       	}
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       }
      
     
   2、index.jsp填写表单页面 
   
    - 
     
      
     
     
      
       <%@ 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 'index.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>
      
     
- 
     
      
     
     
      
           This is my JSP page. <br>
      
     
- 
     
      
     
     
          <form action="EncodeServlet" method="post">
      
     
- 
     
      
     
     
          <input type="text" name="name"/><br/>
      
     
- 
     
      
     
     
          <input type="submit" value="提交"><br/>
      
     
- 
     
      
     
     
          
      
     
- 
     
      
     
     
          </form>
      
     
- 
     
      
     
     
        </body>
      
     
- 
     
      
     
     
      
       </html>
      
     
   3、web.xml 配置信息 
   
    - 
     
      
     
     
      
       <?xml version="1.0" encoding="UTF-8"?>
      
     
- 
     
      
     
     
      
       <web-app version="3.0"
      
     
- 
     
      
     
     
          xmlns="http://java.sun.com/xml/ns/javaee"
      
     
- 
     
      
     
     
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
     
- 
     
      
     
     
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      
     
- 
     
      
     
     
        <servlet>
      
     
- 
     
      
     
     
          <description>This is the description of my J2EE component</description>
      
     
- 
     
      
     
     
          <display-name>This is the display name of my J2EE component</display-name>
      
     
- 
     
      
     
     
          <servlet-name>EncodeServlet</servlet-name>
      
     
- 
     
      
     
     
          <servlet-class>test09_2Servlet.EncodeServlet</servlet-class>
      
     
- 
     
      
     
     
        </servlet>
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
        <servlet-mapping>
      
     
- 
     
      
     
     
          <servlet-name>EncodeServlet</servlet-name>
      
     
- 
     
      
     
     
          <url-pattern>/EncodeServlet</url-pattern>
      
     
- 
     
      
     
     
        </servlet-mapping>
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       </web-app>
      
     
  4、测试
  ①、输入访问地址:http://127.0.0.1:8080/test09_2/ 
   ②、在首页填写中文的名字,点击提交 
   ③、页面显示中文cookie的value值。 
   ④、如果将url编码和解码代码注释了,在提交会显示乱码。 
 
 
文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。
原文链接:brucelong.blog.csdn.net/article/details/76855029
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)