中文编码采用GBK或者gb2312,前者支持的字符集合要大。
具体是:
(1)Jsp页面中设定:<%@ page contentType="text/html; charset=GBK" %>
(2)Servlet中,在response.getWriter()调用之前,执行response.setContentType(”text/html; charset=GBK")
(3)如果在Servlet中没有设定,自行编写toGBK函数,在取得参数时候进行转换,代码如下:
/**
* 做编码的转换,转换成GBK。
* @param str 需要做转换的字符串
* @return String 返回转换后的字符串
*/
public static String toGBK(String str) {
String temp = str;
try {
if (temp == null) {
temp = "";
}
if (temp.equals("null")) {
temp = "";
}
byte[] b = temp.getBytes("ISO8859-1"); //按字节将串拆分
str = new String(b, "GBK"); //按GBK编码方式将字节组合
} catch (UnsupportedEncodingException uee) {
}
return str;
}
(4)编写一个Servlet Filter,在每次响应之前进行编码的转换。这样可以避免在每个Servlet中进行上述的(2)(3)操作。Filter示例如下:
package kellerdu.util;
mport javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import net.sf.hibernate.*;
/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to true, any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* users session.</p>
*/
public class SetEncodingServlet extends HttpServlet implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
/**
* 用户认证
*/
try {
HttpServletRequest req = (HttpServletRequest) request;
TblSysUser user = (TblSysUser) req.getSession().getAttribute(
LoginOper.USER);
if (user == null && req.getServletPath().indexOf("login") == -1) {//没有登录
req.getRequestDispatcher("/login.jsp").forward(request,response);
//chain.doFilter(request, response);
} else {
chain.doFilter(request, response);
//每次响应之后关闭Heibernate Session
HibernateUtil.closeSession();
}
} catch (ServletException ex) {
} catch (IOException ex) {
} catch (HibernateException ex) {
}
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
//System.out.println("SetEncodingServlet inits");
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
try {
com.yingrui.voip.HibernateUtil.closeSession();
} catch (HibernateException ex) {
System.out.println("Hibernate Init Exception!");
}
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
相应的web.xml配置如下(这里采用Struts):
<filter>
<filter-name>setencodingservlet</filter-name>
<filter-class>kellerdu.util.SetEncodingServlet</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setencodingservlet</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>setencodingservlet</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
分享到:
相关推荐
解决jsp+servlet开发中的中文乱码问题
JSP Servlet 汉字编码问题 乱码解决方法
jsp传参 servlet接收中文乱码问题的解决方法.docx
JSP Servlet 中的汉字编码各种问题解决方法
自己写的一个ajax+jsp+servlet的例子,中文问题已解决,拿出来分享,希望对你有所帮助。
彻底解决Servlet及jsp解决中文乱码问题
JSP/Servlet 中的汉字编码问题 ...’、乱码的由来 JSP/Servlet 汉字编码问题及在 WAS 中的解决办法 结束语 参考文章 1. 问题的起源每个国家(或区域)都规定了计算机信息交换用的字符编码集,如美国的扩展 ASCII码, 中
jsp+servlet实现CRUD功能,ajax验证用户名,退出系统,数据库密码加密,过滤器解决中文乱码,实现分页功能,实现导出Excel等等功能。页面不是很美观、、、、
网上就 JSP/Servlet 中 DBCS 字符编码问题有许多优秀的文章和讨论,本文对它们作一些整理,并结合 IBM WebSphere Application Server3.5(WAS)的解决方法作一些说明,希望它不是多余的。
该文档提出了解决jsp开发中常见的汉字编码乱码问题的解决方案,值得参考。
下面小编就为大家带来一篇jsp传参 servlet接收中文乱码问题的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Servlet + Tomcat 中文乱码的解决方法,浏览器把Unicode字符转换为GBK字符,这样页面的内容和浏览器的显示模式都设成了GBK,就不会乱码了
jsp和servlet操作mysql中文乱码问题的解决办法.docx
2.使用BaseServlet解决中文乱码问题 http://blog.csdn.net/justerdu/article/details/50893602 3.使用Filter过滤器解决中文乱码问题 4.使用EL表达式配合JSTL(1.1.2),添加JSTL包 5.使用DBUtils1.6操作数据库,使用...
其实 JSP/Servlet 的中文encoding 并没有想像的那么复杂,虽然定位和解决问题没有定规,各种运行环境也各不尽然,但后面的原理是一样的。了解字符集的知识是解决字符问题的基础。不过,随着中文字符集的变化,不仅仅...
讲述javaweb开发中的jsp乱码问题 get、post的区别 http协议的图解
遇到类似的中文参数就转啊转的,感觉世界不那么美好了!这还不累死人啊,一会GET转,一会POST转,痛苦! 超强过滤器彻底解决JSP-SERVLET中文参数GET-POST传递的问题
JSP_利用Filter解决Servlet输出中文乱码的问题.docx
2、如果乱码是在servlet中出现的,则有两种方法:一种是在每个servlet中doget和doPost方法头部加上request.setCharacterEncoding(“UTF-8″);第二种最保险,一劳永逸,是专门写一个过滤器类,也称国际化,类名为...