<%@ page contentType="text/html; charset=gb2312"%> 中文字符从jsp传送到servlet的处理
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
中文字符从jsp传送到servlet的处理
     发布者: 发布时间:2006-05-07

中文字符,有几种解决方法,
一:可以在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() 的转换。
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
Jsp学习笔记(三)-----Jsp语法!
记住JSP内置对象的简单方法
Jsp学习笔记(一)!-------理解Jsp技术!
jsp研究(一)
建立自己的jsp app目录
jsp+mysql制作简单的留言板(6)
 
最近评论:
        
你曾悄悄的来过!
wow gold,wow gold,wow gold,ffxi gil max(7659)
        
你曾悄悄的来过!
wow gold,wow gold,wow gold,ffxi gil max(6250)
        
冰封的往事!
wow power leveling,wow gold,wow power leveling,wow gold max(1589)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号