JSP中解决乱码的方法。用PetStore中的EncodingFilter

js|解决

/*
* Copyright 2002 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
* LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of
* any nuclear facility.
*/

package com.lims.actions.standardline;

/**
* Create Jstar
* @author PetStore
*
*/
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import javax.servlet.Filter;

/**
* <p>Title: EAF(Enterprise Application Framework)</p>
* <p>Description: 用于设定Request的编码方式</p>
* <pre>请在web.xml中定义本Filter,配置如下:
* <filter>
* <filter-name>EncodingFilter</filter-name>
* <display-name>EncodingFilter</display-name>
* <description>Set the request encoding</description>
* <filter-class>com.netstar.zhuhai.eaf.web.EncodingFilter</filter-class>
* <init-param>
* <param-name>encoding</param-name>
* <param-value>UTF-8</param-value>
* </init-param>
* </filter>
*
* <filter-mapping>
* <filter-name>EncodingFilter</filter-name>
* <url-pattern>/*</url-pattern>
* </filter-mapping>
* </pre>
* @version 1.0
*/

public class EncodingFilter implements Filter {
private FilterConfig config = null;
private String targetEncoding = "ASCII";

public EncodingFilter() {
}
public void init(FilterConfig filterconfig) throws javax.servlet.ServletException {
this.config = filterconfig;
this.targetEncoding = config.getInitParameter("encoding");
}
public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterchain) throws java.io.IOException, javax.servlet.ServletException {
HttpServletRequest request = (HttpServletRequest)srequest;

//set the encoding
request.setCharacterEncoding(this.targetEncoding);

//move to next
filterchain.doFilter(srequest,sresponse);
}
public void destroy() {
this.config = null;
this.targetEncoding = null;
}

}

时间: 2024-10-03 13:18:50

JSP中解决乱码的方法。用PetStore中的EncodingFilter的相关文章

AJAX在Post中文的时候解决乱码的方法。

ajax|解决|中文 在上一篇说的"用AJAX的Get和Post调用Servlet的简单示例."中.如果输入的是中文,那么返回的会是乱码.刚才又看到了一个解决AJAX在Post中文的时候解决乱码的方法. 加上设置字符编码的方法:response.setHeader("charset","gb2312"); ********************************************看到的说明原文如下: 用AJAX来GET回一个页面时,

JSP页面传值乱码过滤方法_JSP编程

本文实例讲述了JSP页面传值乱码过滤方法.分享给大家供大家参考,具体如下: 1.过滤器类: package com.token.t1; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.serv

JS及J2EE开发中解决乱码的小方法

1.设置HttpServletResponse.setContextType("text/html: charset=utf-8"): 2.在web页面用encodeURIComponent()对查询字符串进行编码.HttpServletRequest.getParameter (name)对特殊字符被进行替换.(如:+)自动替换成" "(空白字符串). 3.在进行查询字符串进行传输时,尽量对其进行base64编码,减少网络传输量.进行base64编码之后 ,必须进

如何在PHP下载文件名中解决乱码

通过把Content-Type设置为application/octet-stream,可以把动态生成的内容当作文件来下载,相信这个大家都会.那么用Content-Disposition设置下载的文件名,这个也有不少人知道吧.基本上,下载程序都是这么写的: <?php $filename = "document.txt"; header('Content-Type: application/octet-stream'); header('Content-Disposition: a

解析如何在PHP下载文件名中解决乱码的问题_php技巧

通过把Content-Type设置为application/octet-stream,可以把动态生成的内容当作文件来下载,相信这个大家都会.那么用Content-Disposition设置下载的文件名,这个也有不少人知道吧.基本上,下载程序都是这么写的: 复制代码 代码如下: $filename = "document.txt";header('Content-Type: application/octet-stream');header('Content-Disposition: a

JSP中Ajax乱码问题解决方法

AJAX传递中文字符串时必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数    代码如下 复制代码 function uniencode(text)   {       text = escape(text.toString()).replace(/\+/g, "%2B");       var matches = text.match(/(%([0-9A-F]{2}))/gi);     

AJAX在Post中文的时候解决乱码的方法

ajax|解决|中文 加上设置字符编码的方法:response.setHeader("charset","gb2312"); ********************************************看到的说明原文如下: 用AJAX来GET回一个页面时,RESPONSETEXT里面的中文多半会出现乱码,这是因为xmlhttp在处理返回的responseText的时候,是把resposeBody按UTF-8编码进解码考形成的,如果服务器送出的确实是UTF

jsp传值中文乱码问题解决方法示例介绍

在jsp中,我们经常从数据库读取数据返回客户端,但我们常常在制作时出现乱码现象,所以我们可以用<%request.setCharacterEncoding("UTF-8");%>这个方法来保证中文的正确输出,下面举个例子吧, 我 们要接住表单的值或者把数据库数据打印出来的之前,先把<%request.setCharacterEncoding("UTF- 8");%>放在他们的前面,然后,表单的提交方式必须是post,即method="

jsp传值中文乱码问题解决方法示例介绍_JSP编程

在jsp中,我们经常从数据库读取数据返回客户端,但我们常常在制作时出现乱码现象,所以我们可以用<%request.setCharacterEncoding("UTF-8");%>这个方法来保证中文的正确输出,下面举个例子吧, 我们要接住表单的值或者把数据库数据打印出来的之前,先把<%request.setCharacterEncoding("UTF-8");%>放在他们的前面,然后,表单的提交方式必须是post,即method="p