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 ...
随机推荐
- web安全-xss攻击
web安全问题 xss攻击 1.html标签 html内容的转义 escapeHtml str = str.replace(/&/g,'&'); str = str.replac ...
- U19464 山村游历(Wander) LCT维护子树大小
\(\color{#0066ff}{ 题目描述 }\) 在一个偏远的小镇上,有一些落后的山村.山村之间通过一些道路来连接.当然有的山村可能不连通. 一年当中会发生很多大事,比如说有人提议要在山村\(i ...
- Tarjan+树形DP【洛谷P2515】[HAOI2010]软件安装
[洛谷P2515][HAOI2010]软件安装 题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得 ...
- [SHOI2002]百事世界杯之旅
题目:"--在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽奖活动,获得球星背包,随声听,更克赴日韩观看世界杯. ...
- const常量,常量折叠,字面常量
const int a=10: 涉及到一个叫常量折叠的概念(认为我这说得太简单或者不好理解的可以google一下它获取更多信息), 即编译器虽然会给a分配空间(如果取a的地址进行操作的时候,会强迫编译 ...
- 数据结构1:数据结构与算法C语言版分析概述
本节开始将带领大家系统地学习数据结构,作为一门计算机专业大二学生的必修课程,该课程面对的目标人群为初步具备基本编程能力和编程思想的程序员(大一接触了 C 语言或者 C++).通过系统地学习数据结构,可 ...
- Pre- and Post-order Traversals(先序+后序序列,建立二叉树)
PAT甲级1119,我先在CSDN上面发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89737224 Suppose th ...
- Unity 动画系统 AnimationEvent 动画事件
- 2-28 switch
- 关于pycharm使用sqlite数据可视化的使用
如图