cookie乱码处理 示例
package com.log; import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet.do")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//首先设置响应和请求页面的编码格式:
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html charset='utf-8'");
//获取表单控件的数据
Enumeration<String> nameEnu = request.getParameterNames();
//创建list对象
List<Object> list = new ArrayList<>();
while(nameEnu.hasMoreElements())
{
list.add(request.getParameter(nameEnu.nextElement()));
}
//把数据放在session对象
HttpSession session = request.getSession();
if(list.size()>0){
session.setAttribute("uName", list.get(0));
} //第二种方式,通过Cookie保存用户信息
Cookie cook1 = new Cookie("userName", URLEncoder.encode (list.get(0).toString(),"utf-8"));
cook1.setMaxAge(3*3600);
response.addCookie(cook1); // request.getRequestDispatcher("/main.jsp").forward(request, response);
response.sendRedirect(request.getContextPath()+"/welcome.jsp"); } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} } 接受页面: <%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head> <%
session.setAttribute("uName", "你好");
System.out.print(session.getId());
%> <%
//获取cookie对象
Cookie[] getCooks = request.getCookies();
String user = null;
if(getCooks.length > 0)
{
user = getCooks[0].getValue();
user = URLDecoder.decode(user, "utf-8");
}
%> <body>
<%=session.getAttribute("uName") %>已进入主页!
<%=user%> <button onclick="location.href='main.jsp?u1=<%=user %>'" type="button">跳转传 参</button>
</body>
</html>
cookie乱码处理 示例的更多相关文章
- 解决Cookie乱码
在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因 为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cookie时,先将 ...
- [转]在ASP.NET开发中容易忽略的2个小问题 Cookie乱码存取异常 和 iframe弹框的login跳转
本文转自:http://www.cnblogs.com/outtamyhead/p/3642729.html 本文地址:http://www.cnblogs.com/outtamyhead/p/364 ...
- 解决Cookie乱码问题
写了一个cookie的定义和获取,结果我输入中文后,页面报错 报错信息如下: type Exception report message An exception occurred processin ...
- js操作cookie总结 及乱码
涉及到cookie的 setPath问题: https://blog.csdn.net/damys/article/details/49737905 cookie注解:public voic getx ...
- javascrip中cookie的使用详细分析
JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...
- cookie的详细解释
突然看到网页上中英文切换的效果,不明白怎么弄得查了查 查到了cookie 并且附有详细解释 就copy留作 以后温习 http://blog.csdn.net/xidor/article/detail ...
- js中cookie的使用详细分析
JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...
- Request 接收参数乱码原理解析三:实例分析
通过前面两篇<Request 接收参数乱码原理解析一:服务器端解码原理>和<Request 接收参数乱码原理解析二:浏览器端编码原理>,了解了服务器和浏览器编码解码的原理,接下 ...
- ASP.NET Cookie存值问题
想必 用Cookie存值已经是很普遍的了,我也是刚学习了一点皮毛,现在就记下一点知识,便于日后翻阅. 1.C#代码存取Cookie值 //用Request获取到客户端Cookie 判断是否为空 if ...
随机推荐
- CentOS7l联网
原文:https://blog.csdn.net/nothing2017/article/details/61420767 步骤: 1.以root管理员身份登录系统,输入 --->(ls / ...
- git ssh密钥的使用
//配置邮箱,用户名, git config --global user.name git config --global user.email git config --global --list ...
- python 中文路径问题
Python直接读取中文路径的文件时失败,可做如下处理: inpath = 'D:/work/yuanxx/在线导航/驾车导航/walk_log/20130619_172355.txt' uipath ...
- freemarker ! 用法
${(user.name)!""} 请注意,是打了()的 也就是它会先判断user是不是为null 在判断user.name 是不是为null
- 4 Values whose Sum is 0(枚举+二分)
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...
- python 分页 封装
分页 封装 我是在项目根目录创建个分页文件 分页代码: class Pagination(object): def __init__(self, data_num, current_page, url ...
- Educational Codeforces Round 11 B
Description Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of th ...
- python练习六十二:文件处理,往文件中所有添加指定的前缀
往文件中所有添加指定的前缀 方法一:open方法 f_r = open('text.txt') f_w = open('text_new.txt','w+') i = 0 while True: i ...
- Problem07 处理字符串
题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 程序分析:利用while 语句,条件为输入的字符不为'\n'. import java.util.*; public clas ...
- 转 ORACLE 查看RMAN的备份信息总结
http://www.cnblogs.com/kerrycode/p/5684768.html 关于Oracle数据库的RMAN备份,除了邮件外,是否能通过其它方式检查RMAN备份的成功与失败呢?其实 ...