在URL中传中文参数出现乱码解决方案

在B/S开发中经常会用到url传参数,而且还会有很多传中文参数的,传中文参数经常会出现乱码的问题。
一下提供我遇到乱码问题的一个解决方案(仅供参考):
(1)在所有的jsp页面做一下设置:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>中文问题</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
</html>
(2)在web.xml中加如下过滤器: org.springframework.web.filter.CharacterEncodingFilter过滤器.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
[color=red]org.springframework.web.filter.CharacterEncodingFilter[/color]
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
(3)server.xml中不要使用
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
(4) server.xml文件加上useBodyEncodingForURI="true"
这样应该可以搞定大多数前台的中文问题.
<Connector port="8080"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000" useBodyEncodingForURI="true"
disableUploadTimeout="true" />
(5)如果URL中包含中文参数要提交后台,可以用encodeURIComponent()函数进行编码。
window.location = "<%=request.getContextPath()%>/deal/toSystemDealList.action?ddlType=" + document.getElementById("ddlType").value + "&txtSelect=" + encodeURIComponent(document.getElementById("txtSelect").value) + "&proClassId=" + document.getElementById("proClassId").value;

document.getElementById("txtSelect").value得到的是以下输入项的值(这个值可能是中文字符)
<input name="txtSelect" id="txtSelect" type="text" />
(6)解决乱码问题还应该注意数据库的配置,下面以mysql为例进行说明:
A.mysql配置文件:修改mysql在windows\my.ini里default-character-set=utf-8
B.mysql里数据库和表也都设为utf8_unicode_ci
C.数据库连结:jdbc:mysql://localhost/mydb?useUnicode=true&characterEncoding=utf-8
注意,关键就在于此:此句中间是'&'不是'&'这是因为数据库连结时,在.jsp和.java文件中应该用&号,而XML文件中需要用&amp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值