JSP | 基础 | 中文字符的传递
2024-08-30 08:15:53
设置传递格式:
request.setCharacterEncoding("utf-8");
中文字符解码:
import java.net.URLDecoder; String format = URLDecoder.decode(request.getParameter("format"), "utf-8");
中文字符加码:
import java.net.URLEncoder; String username = URLEncoder.encode(request.getParameter("username"), "UTF-8");
实例:
登陆后创建session:
<%@page import="java.net.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8"); String username = URLEncoder.encode(request.getParameter("username"), "UTF-8");
String password =URLEncoder.encode(request.getParameter("password"),"UTF-8") ; Cookie usernameCookie = new Cookie("username",username);
Cookie passwordCookie = new Cookie("password",password); response.addCookie(usernameCookie);
response.addCookie(passwordCookie); request.getRequestDispatcher("login_success.jsp").forward(request, response);
%> </body>
</html>
表单提交包含中文字符显示:
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body> <%
request.setCharacterEncoding("UTF-8");
Cookie[] cookies = request.getCookies();
String username = "";
String password = ""; for(Cookie cookie : cookies) {
if("username".equals(cookie.getName()))
username =URLDecoder.decode(cookie.getValue(),"utf-8") ; if("password".equals(cookie.getName()))
password = cookie.getValue(); } %> <h1 align="center">恭喜<%=username %>登录成功</h1>
<hr>
<div style="border: solid 1px;">
<p> 用户名:<%=username %> </p>
<p> 密码: <%=password %> </p>
</div> </body>
</html>
最新文章
- [下载]北京新版小学英语五年级上册mp3点读APP
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
- Java关键字——super
- Android MVVM框架RoboBinding初探
- poj 3335(半平面交)
- 现在有T1、T2、T3三个线程,怎样保证T2在T1执行完后执行,T3在T2执行完后执行?使用Join
- Windows Phone 8开发环境搭建
- iOS开发中打开本地应用、打开appStore应用、给app评分功能实现
- 浏览器开发 IE webkit chrome浏览器定制
- Android Studio的使用(十)--读取assets、Raw文件夹下文件,以及menu、drawable文件夹
- oracle 裸设备划分 --centos6.5
- 【代码笔记】Web-Javascript-Javascript函数
- Datatable数据转换成excel导出时 数值类型在EXCEL中为文本形式 无法进行统计
- Linux下批量ping某个网段ip的脚本
- String to Integer (atoi) leetcode java
- 虚拟机vmnet0、vmnet1和vmnet8的区别
- style和getComputedStyle(ff)和currentStyle
- MVC的项目部署成应用程序或虚拟目录路径的问题
- Ubuntu 定时任务
- 字符串连接比较(std::unique_ptr实现)
热门文章
- linux host=${host:-";localhost";}使用方法
- BAT网络运维常见面试题目总结
- 在 Linux 上如何清除内存的 Cache、Buffer 和交换空间
- VK Cup 2015 - Round 2 E. Correcting Mistakes —— 字符串
- selenium使用笔记(一)——selenium你该知道的
- java random配置修改
- 第三篇:python基础之数据类型与变量
- css 鼠标移入边框填充效果
- 搜索算法 pots
- hdu-5000 Clone(dp)