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乱码处理 示例的更多相关文章

  1. 解决Cookie乱码

    在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因 为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cookie时,先将 ...

  2. [转]在ASP.NET开发中容易忽略的2个小问题 Cookie乱码存取异常 和 iframe弹框的login跳转

    本文转自:http://www.cnblogs.com/outtamyhead/p/3642729.html 本文地址:http://www.cnblogs.com/outtamyhead/p/364 ...

  3. 解决Cookie乱码问题

    写了一个cookie的定义和获取,结果我输入中文后,页面报错 报错信息如下: type Exception report message An exception occurred processin ...

  4. js操作cookie总结 及乱码

    涉及到cookie的 setPath问题: https://blog.csdn.net/damys/article/details/49737905 cookie注解:public voic getx ...

  5. javascrip中cookie的使用详细分析

    JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...

  6. cookie的详细解释

    突然看到网页上中英文切换的效果,不明白怎么弄得查了查 查到了cookie 并且附有详细解释 就copy留作 以后温习 http://blog.csdn.net/xidor/article/detail ...

  7. js中cookie的使用详细分析

    JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...

  8. Request 接收参数乱码原理解析三:实例分析

    通过前面两篇<Request 接收参数乱码原理解析一:服务器端解码原理>和<Request 接收参数乱码原理解析二:浏览器端编码原理>,了解了服务器和浏览器编码解码的原理,接下 ...

  9. ASP.NET Cookie存值问题

    想必 用Cookie存值已经是很普遍的了,我也是刚学习了一点皮毛,现在就记下一点知识,便于日后翻阅. 1.C#代码存取Cookie值 //用Request获取到客户端Cookie  判断是否为空 if ...

随机推荐

  1. t-sql read xlsx

    How to Read and Load an Excel 2007 or Excel 2010 File Without Using Import/Export Utility To read an ...

  2. 记一次IIS应用程序域崩溃的原因

    在日常工作中,每次新的功能上线前,我们会搭建一个测试环境提供给客户测试使用,确定无误后才会更新到正式环境上.这一次也不例外,在约定好时间地点,客户进行集中化测试的过程中,反应网站系统打不开,报500错 ...

  3. P2173 [ZJOI2012]网络

    \(\color{#0066ff}{ 题目描述 }\) 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同 ...

  4. Servlet中Web.xml的配置详解(一)

    1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码.DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用的 ...

  5. [国家集训队]特技飞行 贪心BZOJ2697

    题目背景 1.wqs爱好模拟飞行. 2.clj开了一家神犇航空,由于clj还要玩游戏,所以公司的事务由你来打理. 注意:题目中只是用了这样一个背景,并不与真实/模拟飞行相符 题目描述 神犇航空开展了一 ...

  6. [USACO08OPEN]农场周围的道路Roads Around The Farm BZOJ 1621 DFS

    Farmer John's cows have taken an interest in exploring the territory around the farm. Initially, all ...

  7. kvm重新命名

    1.停止虚拟机 virsh shutdown wcltest3 2.导出虚拟机的配置文件 cd /etc/libvirt/qemu virsh dumpxml wcltest3 > vnc.xm ...

  8. MySQL安全优化

    一.数据库相关 1. MySQL版本的选择 在正式生产环境中,建议使用5.6或以上系列的版本(5.7不建议,曾经用过这个版本,问题有点多). 2. 运行用户与端口的配置 2.1.确保MySQL运行用户 ...

  9. python文件引用其他文件中的变量

    问题: 然后再另一个文件中引用该变量 报错:Cannot find reference 'User_Agent' in '__init__.py' less... (Ctrl+F1) 正确写法: fr ...

  10. js学习笔记 -- 函数

    js函数有类似javaMethod用法 Math.max.apply( Math.max.call( Array map,reduce,filter,sort , , , , , , , , ]; v ...