欢迎光临
我们一直在努力

中文字符从jsp传送到servlet的处理-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

对于在jsp和servlet中的request.getparameter()方法,如果你想要传入

中文字符,有几种解决方法,

一:可以在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() 的转换。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 中文字符从jsp传送到servlet的处理-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章