Java Web开发中的乱码问题
POST方法乱码:
1:存在乱码的示例:
前端页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" //设置UTF8编码
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 头作用,告诉浏览器UTF8编码 -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<form action="Encode" method="post">
Name:<input name="name" type="text" /> <br> Sex:<input
type="text" name="sex" /><br>
<button type="submit">发送</button>
</form>
</div> </body>
</html>
后台Servlet接受参数:
package com.daxin; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Encode
*/
public class Encode extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //默认编码是ISO8859-1
String name = request.getParameter("name");
String sex = request.getParameter("sex");
System.out.println(name);
System.out.println(sex); } }
当我们启动输入中文时候,打印的是:
?¤§é??
??·
乱码,这个问题的原因就是:Servlet的方法的所有方法默认解码都是ISO-8859-1,而我们前端页面编码是UTF8,所以导致解码乱码!这种方式也存在解决方案:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//默认编码是ISO8859-1
String name = request.getParameter("name");
String sex = request.getParameter("sex");
System.out.println(name);
System.out.println(sex);
//由于上面是ISO编码错误,所以还原之后再使用UTF8编码
String newName = new String(name.getBytes("ISO-8859-1"),"UTF-8");
String newSex = new String(sex.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(newName);
System.out.println(newSex);
}
第二种更优解决方案:设置全局的UTF-8编码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//全局设置UTF8编码,该方法只对实体内容中的数据起作用。post的数据是实体内容中的。所以该设置只对post起作用。get方法不起作用
request.setCharacterEncoding("UTF-8");
//默认编码是ISO8859-1
String name = request.getParameter("name");
String sex = request.getParameter("sex");
System.out.println(name);
System.out.println(sex);
}
GET乱码问题:
由于request.setCharacterEncoding("UTF-8");方法只对实体内容编码起作用,换言之就是只对Post有效,对于GET方法请求依旧无法生效。所以针对Get请求我们只能手动解码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//默认编码是ISO8859-1
String name = request.getParameter("name");
String sex = request.getParameter("sex");
//由于上面是ISO编码错误,所以还原之后再使用UTF8编码
String newName = new String(name.getBytes("ISO-8859-1"),"UTF-8");
String newSex = new String(sex.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(newName);
System.out.println(newSex);
}
但是通常我们一般都只是实现一个Post方法逻辑,而是将GET请求逻辑转给Post处理,所以此时比较健全的写法是:
package com.daxin; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Encode
*/
public class Encode extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { // 全局设置UTF8编码,该方法只对实体内容中的数据起作用。post的数据是实体内容中的
request.setCharacterEncoding("UTF-8");
// 默认编码是ISO8859-1
String name = request.getParameter("name");
String sex = request.getParameter("sex");
if ("GET".equals(request.getMethod())) {
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
} if ("GET".equals(request.getMethod())) {
sex = new String(sex.getBytes("ISO-8859-1"), "UTF-8");
} System.out.println(name);
System.out.println(sex); } }
结论:乱码问题出现,就是因为编码和解码时候使用的不是一个编码,最终导致乱码。所以解决问题的核心思路就是使用同一个编码解码器。前端一般使用的是UTF8编码,后台默认是ISO8859-1,所以我们将后台解码器使用UTF8即可解决编码问题。
Java Web开发中的乱码问题的更多相关文章
- Java Web开发中路径问题小结
Java Web开发中,路径问题是个挺麻烦的问题,本文小结了几个常见的路径问题,希望能对各位读者有所帮助. (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 ...
- Java Web 开发中路径相关问题小结
Java Web开发中路径问题小结 (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 Eclipse中目录结构如图2所示: 图2 那么针对这个站点的几个基本概 ...
- Java Web开发中MVC设计模式简介
一.有关Java Web与MVC设计模式 学习过基本Java Web开发的人都已经了解了如何编写基本的Servlet,如何编写jsp及如何更新浏览器中显示的内容.但是我们之前自己编写的应用一般存在无条 ...
- java web开发中常用的协议的使用和java-web 常见的缓冲技术
一.DNS协议 作用将域名解析为IP 类似于我们只需要知道中央一台,中央二台,而不需要知道它的频率,方便记忆. java dns 域名解析协议实现 1 域名解析,将域名可转换为ip地址InetAd ...
- Java Web开发中路径问题小结(getRequestUrl getContextUrl getServletUrl)
看以博客感觉不错,分享一下http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html (1) Web开发中路径的几个基本概念 假设 ...
- Java Web 开发中的中文乱码与解决方式
乱码产生的原因 不管是request乱码还是response乱码,其实都是由于客户端(浏览器)跟服务器端采用的编码格式不一致造成的.以request乱码为例:浏览器向服务器发送请求,因为浏览器与服务器 ...
- JAVA WEB开发中的资源国际化
为什么要国际化? 不同国家与地区语言,文化,生活习惯等差异.在数字,时间,语言,货币,日期,百分数等的不同. 两个名词: I18N:即资源国际化,全称为Internationalization,因为首 ...
- Java Web开发中的名词解释
1.JVM Java虚拟机,class文件的运行时环境,就好比软件运行在操作系统一样,java要运行在JVM中才行,这也是Java之所以支持扩平台的基础. 2.Servlet/JSP 是满足一定接口需 ...
- java web开发中的奇葩事web.xml中context-param中的注释
同事提交了代码.结果除同事之外,其他人全部编译报错.报错说web.xml中配置的一个bean 没有定义.按照报错提示,各种找,无果. 由于代码全部都是提交到svn主干,之前也没有做过备份,只能一步一步 ...
随机推荐
- Apache RocketMQ在linux上的常用命令
Apache RocketMQ在linux上的常用命令 进入maven安装后的rocketmq的bin目录 1.启动Name Server 2.启动Broker 3.关闭Name Server 4 ...
- 操作Linux系统环境变量的几种方法
一.使用environ指针输出环境变量 代码如下: #include<stdio.h> #include<string.h> #define MAX_INPUT 20 /* 引 ...
- Django之破解数独
数独是一项快乐的益智游戏,起源于18世纪瑞士的一种数学游戏.解答者需要运用纸.笔进行演算,需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫(3*3)内的 ...
- C# ValueTuple 原理
本文告诉大家一些 ValueTuple 的原理,避免在使用出现和期望不相同的值.ValueTuple 是 C# 7 的语法糖,如果使用的 .net Framework 是 4.7 以前,那么需要使用 ...
- UML速记
依赖:虚线箭头 关联:实线箭头 接口:虚线三角 父类:实线三角 聚合:空心菱形 组合:实心菱形 顺着箭头方向: 依赖于和什么关联是什么的子类是什么的接口的实现是什么的聚合是什么的组合
- [Hadoop异常处理] Namenode和Datanode都正常启动,但是web页面不显示
异常 namenode和data都正常启动 但是web页面却不显示,都为零 解决办法一: 在hdfs-site.xml配置文件中,加入 <property> <name>dfs ...
- PHP FastCGI进程管理器PHP-FPM的架构
一个master进程,支持多个pool,每个pool由master进程监听不同的端口,pool中有多个worker进程.每个worker进程都内置PHP解释器,并且进程常驻后台,支持prefork动态 ...
- webpack4 系列教程(七): SCSS提取和懒加载
教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步>>> (原文)webpack4 系列教程(七): SCSS 提取和懒加载. 个人技术小站: https://god ...
- js实现的玫瑰花
<html> <head> <meta charset="utf-8"> <title>开心快乐每一天</title> ...
- 2017-07-20 在Maven Central发布中文API的Java库
知乎原链 相关问题: 哪些Java库有中文命名的API? 且记下随想. 之前没有发布过, 看了SO上的推荐:Publish a library to maven repositories 决定在son ...