问题描述:在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
* <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);
}
}//EOC
加入该servlet后,修改web.xml以部署该servlet.
具体部署文件为:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>cn.cstnet.cstnm.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>//这个就是用来统一的编码
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>//这个表示对包下所有的jsp都进行过滤
</filter-mapping>
做成这样以后,就发现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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
</head>
<body>
<form action="admin.jsp" method="post" name="loginForm">
<input name="page" type="hidden" value="1" />
</form>
<a style='cursor:hand' onclick="loginForm.submit()">
Test</a>
</body>
</html>
代码:admin.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String page0 = request.getParameter("page");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'admin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%out.println(page0);%>
</body>
</html>
试验一下,你就发现这样就能解决问题了,呵呵,到此为止,我所遇到的中文乱吗的问题算是告一段落了。不容易呀……