• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

中文字符从jsp传送到servlet的处理

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 11次 | 进入软件测试论坛讨论

领测软件测试网
中文字符,有几种解决方法,
一:可以在String temp = request.getParameter("xx");
temp = new String(temp.getBytes("ISO8859_1"));
二:常用的方法为,设置一个过滤器:
1. java文件为com.esoon.shabc.utils.SetCharacterEncodingFilter
源文件:
package com.esoon.shabc.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
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.HttpServletRequest;

public class SetCharacterEncodingFilter
implements Filter {

protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;

/**
* 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.
*/
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); //设置request编码的地方
}

// Pass control on to the next filter
// 传递控制到下一个过滤器
chain.doFilter(request, response);
}

/**
* Place this filter into service.
* 从web-app的web.xml文件中读取初始参数的值
*/

public void init(FilterConfig filterConfig) throws ServletException {
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;
}

/**
* 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>.
* 选择request原来的编码
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
2. 然后在web.xml文件中<web-app></web-app>中间添加:
<filter>
<filter-name>Set_Character_Encoding</filter-name>
<filter-class>com.esoon.shabc.utils.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set_Character_Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. 如果所提交的字段在form里面,则这个form应该设置为:method="post"
类似于: <form method="post" name="firstPriInfoForm" ></form>
通过这种方法,即使你所提交有多个中文字段,那么你在servlet中只需要
request.getParameter("##");
而无需再一次new String() 的转换。

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网