jsp编码问题
方法一%%%%%%%%%%%%%%%%%%%%%%%%%
例:1:jsp页面用<%@ page language="java" pageEncoding="UTF-8"%>
2: mysql数据库utf8(没问题)
3:mysql链接字符串
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/cms?useUnicode=true&characterEncoding=UTF-8</value>
</property>
4:
出现问题:jsp页面数据一传到mysql数据库里就变成乱码
最后解决方法:把对传递的数据值的变量操作如下:
String username=StringEncode.getEncode(userForm.getUsername());
StringEncode.getEncode的功能如下:
package com.links.utils;
public class StringEncode {
public static String getEncode(String str)
{
String ret;
if(str==null)
return str;
try{
// ret=new String(str.getBytes("UTF-8"),"UTF-8");
ret=new String(str.getBytes("ISO-8859-1"),"UTF-8");
}
catch(Exception e){
ret=str;
}
return ret;
}
}
其它参考
#################################################################
方法二%%%%%%%%%%%%%%%%%%%%%%%%%(为了不都写 StringEncode.getEncode转换)
就是要在web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- <servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
-->
<servlet>
<servlet-name>ChinaRedTea</servlet-name>
<servlet-class>com.henry.util.ChinaRedTea</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ChinaRedTea</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
package com.henry.util;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionServlet;
public class ChinaRedTea extends ActionServlet {
private static final long serialVersionUID = -995919609576385793L;
public ChinaRedTea() {
}
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
super.process(request, response);
}
}