以下是几种在开发中中文乱码问题的解决方案
当sevlet返回js脚本时弹出框显示中文乱码的解决方案:
可以在servlet中加上response.setContentType(“text/html;charset=utf-8”);
servlet中用response有3种设置输出内容的编码方式:
1.response.setCharacterEncoding(“UTF-8”); 只能用来设置out输出流中所采用的编码,但是他的优先权最高,可以覆盖后两种方法中的设置;
2.response.setContentType(“text/html;charset=UTF-8”); 即可以设置out输出流中字符的编码方式,也可是设置浏览器接收到这些字符后以什么编码方式来解码,它的优先权低于第一种方法,但高于response.setLocale(new java.util.Locale(“zh”,“CN”)); 相当于服务端解析:<%@ page contentType=“text/html;charset=UTF-8” %>
3.response.setLocale(new java.util.Locale(“zh”,“CN”));只能用来设置out输出流中字符的编码方式,但是它的优先权最低,在已经用前两种方法之一设置了编码方式以后,它就被覆盖而不起作用了。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");把页面参数按utf-8编码后传入服务器端
// response.setLocale(new java.util.Locale("zh","CN"));
// response.setCharacterEncoding("utf-8");
int bid = Integer.parseInt(request.getParameter("bid"));
BookService bookService = new BookServiceImpl();
boolean result = bookService.deleteBook(bid);
if(result){
response.getWriter().print("<script type='text/javascript'>alert('删除成功!');window.location='ShowAllServlet'</script>");
}else{
response.getWriter().print("<script type='text/javascript'>alert('删除失败!');window.location='ShowAllServlet'</script>");
}
}
jsp页面显示乱码,则需要你在jsp页面中设置编码方式,这里一般最好三个都要配上utf-8:
<%@ page language=“java” contentType=“text/html; charset=utf-8” pageEncoding=“utf-8”%> 是服务器端java程序运行时的输出编码,服务器端以什么样的编码向客户端输出HTML. 。
< meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”>是指客户端浏览器以什么样的编码来显示网页,指导浏览器解析服务器端传入的HTML流. 同时它还有一个作用,指导其提交表单的时候使用什么编码传入request.
<%@ 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>
<body>
</body>
</html>
如果使用SSM框架开发,则可以通过在web.xml中配置字符编码过滤器的方式统一字符编码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>UserManager</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 统一字符编码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在web开发中,tomcat对于传输的字符串都是采用iso-8859-1编码/解码方式。而浏览器端对于中文都是用gbk或utf-8中文编码/解码方式,所以传到后台都会是乱码的,容器一般都是有处理的,所以中文能正常显示和存储。但有些情况也是会出现乱码的,解决方式如下
String b = new String(str.getBytes(“iso-8859-1”,“客户端的编码/解码方式”)) // 中文解码方式一般用的是utf-8或者gbk。
如:
String b1 = new String(str.getBytes("iso-8859-1","utf-8"));
感谢您的阅读,欢迎参观我的个人网站:小嗨词典【 https://www.happydict.cn】