jsp mysql 中文乱码_jsp编程手记---处理jsp和mysql中文乱码!

问题描述:在jsp以form表单形式向页面提交数值为中文的参数数据时,在页面的另一端以request.getParameter("parameter_name")形式意图获取该中文数据时,发现得到的不是原来的汉字,而是一堆乱码????.这样就使得利用form表单传送数据失败.

解决方案:在该jsp页面所在web项目中加入一个字符过滤器的servlet就能搞定事情.具体做法如下:

servlet代码:

package cn.cstnet.cstnm.filter;

/*

* ====================================================================

*

* JavaWebStudio 开源项目

*

* Struts_db v0.1

*

* ====================================================================

*/

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

/**

* 中文过滤器

*/

public class SetCharacterEncodingFilter 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);

//System.out.println("jdhf");

}

// Pass control on to the next filter

chain.doFilter(request, response);

}

/**

* Place this filter into service.

*

* @param filterConfig The filter configuration object

*/

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;

}

// --------------- 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

* null.

*

* The default implementation unconditionally returns the value configured

* by the encoding initialization parameter for this

* filter.

*

* @param request The servlet request we are processing

*/

protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}//EOC

加入该servlet后,修改web.xml以部署该servlet.

具体部署文件为:

Set Character Encoding

cn.cstnet.cstnm.filter.SetCharacterEncodingFilter

encoding

GB2312//这个就是用来统一的编码

ignore

true

Set Character Encoding

/*//这个表示对包下所有的jsp都进行过滤

做成这样以后,就发现form表单提交的中文数据能够被识别了.好,到这里就终于松了一口气了.

然而,事情并非如此简单,当本人继续往后面做的时候,另外一个仍然是乱码问题又出现了……

问题描述:当在url后面如以query.jsp?name=中科院这种形式传送参数的时候,发现在接收端同样接收到的是乱码。(有人或许会问,为什么非要这样传送参数,这个自然是因为编程需要了,比如你做一个查询,然后得到很多条查询结果的记录,而这些记录又要求分页显示的时候,那个查询条件就需要作为参数来进行传递了,这个时候首先想到的当然是用上面说的这种最简单的方式来传递了)

解决方案:在本人解决这个问题的时候,首先想到的是利用jsp内置对象session来传递。代码:

if(request.getParameter("name")==null)

{

name = String.valueOf(session.getAttribute("query_name"));//当页面在本jsp

//页面 跳转的时候就不再从form表单中传送数据了

//这个时候就用session对象了。

}

else

{

name = request.getParameter("name");//这个是在第一次从其他页面跳转到本页

//面的时候一般是用form表单传递参数,所以如此接收。

session.setAttribute("query_name",name);//保存从form表单中获得的参数

}

上述方案能解决部分问题,但仍然有问题遗留,那就是session的生存周期的问题,默认仅仅是半小时。所以仍然有它的不足之处,甚至可以说是不可行,毕竟一个人点在一个网页滞留半小时的情况还是有的(比如:访问者正在看该网页,但突然来了个电话要接一下等等)。

鉴于上面解决方案的缺点,于是本人经朋友指点,还是试图使用form表单传送参数,但是由于需求是要在点连接的时候传送,也就是点(上一页)(下一页)这些连接的时候需要传送参数,但是又要用form表单,于是有:

写一例子:

代码:index.html

Test

代码:admin.jsp

String page0 = request.getParameter("page");

%>

My JSP 'admin.jsp' starting page

试验一下,你就发现这样就能解决问题了,呵呵,到此为止,我所遇到的中文乱吗的问题算是告一段落了。不容易呀……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值