cookie乱码处理 示例
2018-06-24 01:41:07来源:未知 阅读 ()
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>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- ie8下透明度处理 2020-06-11
- Cookie SameSite属性介绍及其在ASP.NET项目中的应用 2020-03-28
- css常用样式对文本的处理演练 2020-02-14
- cookie,sessionstorage,localstorage区别 2019-12-03
- 【前端知识体系-CSS相关】CSS预处理器 2019-11-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash