J2EE项目中解决中文传值乱码

★ 首先说下,这篇文章很菜,基本上是入门的东西,熟手绕道。

 

 

第一种,通常情况下,普通的JSP中,设置页面的字符集,这没有什么好说的。

            看以下代码:

            contentType="text/html; charset=UTF-8"

            pageEncoding="UTF-8"

            content="text/html; charset=UTF-8"

<%@ 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>

 

 第二种,普通的JAVA WEB项目中,使用Servlet过滤器,是一种不错的选择,通常选这种的比较多。

             这种方式分两步,第一,实现(implements)一个Servlet Filter;第二,web.xml中配制filter.

 

Filter实现类

/**********************************************
  * @author  Simon Hoo (simon@cottsoft.com)
  * @contact QQ:9930323 MSN: simon_hoo@msn.com
  * @OnlineSupport: http://www.CottSoft.com
  * @Create  2011-5-7
  * @version V1.0
  ********************************************/

package com.cottsoft.j2ee.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
	private String encoding=null;
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		encoding = filterConfig.getInitParameter("encoding");
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		String encoding = getEncoding();
		if(encoding!=null && !"".equals(encoding.trim())){
			request.setCharacterEncoding(encoding);
		}
		doFilter(request, response, filterChain);
	}

	@Override
	public void destroy() {

	}
	
	public String getEncoding() {
		return encoding;
	}

}
 

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"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<display-name>cottsoft</display-name>
	<welcome-file-list>
		<welcome-file>/Welcome.88</welcome-file>
	</welcome-file-list>
	
	<filter>
		<filter-name>Set Encoding Filter</filter-name>
		<filter-class>
			com.cottsoft.j2ee.filter.EncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>Set Encoding Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


</web-app>
 

第三种,在使用struts框架的J2EE项目中,如是当前项目已经配有多个其它功能的Filter,如果再配一个用于解决字符编码的Filter,就有可能该Filter功能无效(不要说不,我写这篇文章就是因为出现过该问题,才写的)。那么可以通过扩展Struts的RequestProcessor类来实现struts获取表单乱码问题。当然,也可以在web.xml中从上往下所配的第一个Filter实现类中,增加解决乱码功能,但前题是,这个Filter实现类要是允许你自己可以修改的。但这样做就不能做到功能分开的目的了。不符合相关设计原则。下面说说RequestProcessor的方式。

这种方式只能处理struts相关的请求数据。

同样也是分两步,第一步,扩展RequestProcessor类。第二步,在struts-config.xml(Struts配置文件)中配置.

 

RequestProcessor护展类

/**********************************************
  * @author  Simon Hoo (simon@cottsoft.com)
  * @contact QQ:9930323 MSN: simon_hoo@msn.com
  * @OnlineSupport: http://www.CottSoft.com
  * @Create  2011-4-23
  * @version V1.0
  ********************************************/

package com.cottsoft.common;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.RequestProcessor;


public class CottSoftRequestProcessor extends RequestProcessor {
	@Override
	public void process(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		super.process(request, response);
	}
}

 

struts-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://struts.apache.org/dtds/struts-config_1_2.dtd">
         
<struts-config>
  <data-sources />
  <form-beans>
 
  </form-beans>
  
  <global-exceptions />
  
  <global-forwards>
    	<forward name="welcome" path="/welcome.88"></forward>
   </global-forwards>
   
    <action path="/welcome" 
    		type="com.cottsoft.website.web.action.WelcomeAction"
    		name="WelcomeForm"
    		attribute="WelcomeForm"
    		scope="request"
    		input="/welcome.jsp">
    		<forward name="index" path="/index.jsp"></forward>
    </action>

  </action-mappings>
  
  <controller processorClass="com.cottsoft.common.CottSoftRequestProcessor"></controller>
  
  <message-resources parameter="com.cottsoft.website.resources.ApplicationResources"/>
  
</struts-config>

 

当然,还有其它,比如说从数据库角度考虑来处理中文的,很多种方式,这里就不一一列写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值