`
tianjun309
  • 浏览: 372798 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

filter_setEncoding_note

阅读更多

(1)通过struts内部的RequestProcessor来设置编码。

EncodingHandler.java

package com.test.util;

import java.io.UnsupportedEncodingException;

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

import org.apache.struts.action.RequestProcessor;

public class EncodingHandler extends RequestProcessor
{

	@Override
	protected boolean processPreprocess(HttpServletRequest request,
			HttpServletResponse response)
	{
		try
		{
			//当struts-config.xml中设置了此类的controller,所有请求会先经过这边做处理。
			// <controller processorClass="com.test.util.EncodingHandler"></controller>
			request.setCharacterEncoding("utf-8");
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return true;
	}
	
}

 

(2)通过servlet 的filter来设置编码,更通用。

EncodingServlet.java

package com.test.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;
import javax.servlet.http.HttpServlet;

public class EncodingServlet extends HttpServlet implements Filter
{
	private static final long serialVersionUID = 1L;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException
	{
		try
		{
			request.setCharacterEncoding("utf-8");
			filterChain.doFilter(request, response);
		}
		catch (Exception ex)
		{
		}

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{

	}

}

 

 

web.xml

 <filter>
  	<filter-name>encodingFilter</filter-name>
	<filter-class>com.test.filter.EncodingServlet</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

 

分享到:
评论

相关推荐

    php.ini-development

    Note: output_handler must be empty if this is set 'On' !!!! ; Instead you must use zlib.output_handler. ; http://php.net/output-handler ;output_handler = ; Transparent output compression using the ...

    测试培训教材

    This allows you to build a more advanced test set execution flow, in which you can filter tests in a test set during execution, based on the status or type of each test. VAPI-XP is also fully ...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + added BIFF8 XLS export filter + added to ODF export the Language property + [enterprise] added "scripts" folder for additional units ("uses" directive in report script) + [enterprise] added logs for...

    Git-2.21.0-64-bit.zip

    * The filter_data used in the list-objects-filter (which manages a lazily sparse clone repository) did not use the dynamic array API correctly---'nr' is supposed to point at one past the last ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    The Set of Native Delphi Components for Borland Delphi versions 1, 2, 3, 4, 5, 6, 7, 2005, 2006, 2009 and Borland C++ Builder 1, 3, 4, 5, 6, 2006 & 2009. 100% Source Code. Last revision date Oct 12, ...

Global site tag (gtag.js) - Google Analytics