1、页面代码:
<td>身份: <select name="roleid" id="roleid"> <option value="0" >全部</option> <option value="1" >学生</option> <option value="3" >老师</option> <option value="4" >管理员</option> </select> </td>
2、后台servlet接收参数:
String roleid= request.getParameter("roleid"); //roleid值为空字符串
3、web.xml有一个编码过滤器:
<filter> <filter-name>setencodingfilter</filter-name> <display-name>SetEncodingFilter</display-name> <filter-class>cn.util.SetEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>ignore</param-name> <param-value>true</param-value> </init-param> </filter>
4、过滤器代码:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { if(ignore || request.getCharacterEncoding() == null) { String encoding = selectEncoding(request); if(encoding != null) // System.out.println("过滤前roleid的值可正常获取----->>>>>>"+request.getParameter("roleid")); request.setCharacterEncoding(encoding); // System.out.println("过滤后roleid的值----->>>>>>"+request.getParameter("roleid")); response.setContentType("text/html; charset=\""+encoding+"\""); } chain.doFilter(request, response); }
5、现象:select标签中value值如果是'1','2','3'这种单个字符,后台接收到的roleid为空字符串;如果value值为'11','22','33'等多字符,后台能正常接收。
之前tomcat5.5.33/6.0.20 均能正常使用,换成tomcat6.0.35就这样了
6、解决:
将web.xml中的encoding改成更标准的GB18030
参考自:http://blog.csdn.net/rainbow0101/article/details/7098181