spring mvc乱码问题,在过滤器中设置encoding之前,取到的encoding为null,原因何在?

pn_nl 2009-02-03 02:33:00
web.xml设置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.web1.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

jsp文件如下:
<%@page contentType="text/html" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>

<body>
<form name="form1" method="post" action="doLogin.htm">
<input name="userName" type="text" />
<input type="submit" value="login" />
</form>
</body>
</html>

filter如下:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("Encoding before filter is: "
+ request.getCharacterEncoding());
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}

请问高手,原因是什么啊?
...全文
744 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
loveunittesting 2009-02-06
  • 打赏
  • 举报
回复
不清楚,如果用springmvc 的话,还是用实现HandlerInterceptor接口的比较正规,效率高
「已注销」 2009-02-04
  • 打赏
  • 举报
回复
post试试
yousite1 2009-02-03
  • 打赏
  • 举报
回复
不是filter的问题,你试一下把FORM改成method=post的提交方式,
再试一下。我之前碰到了,把提交方法改为post就不会中文乱码了。
yousite1 2009-02-03
  • 打赏
  • 举报
回复
不是filter的问题,你试一下把FORM改成method=post的提交方式,
再试一下。我之前碰到了,把提交方法改为post就不会中文乱码了。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧