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">
<!-- 字符过滤器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.pro.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.act</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
EncodingFilter.javapackage com.pro.filter;
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 EncodingFilter implements Filter {
private FilterConfig filterConfig = null;
public void init(FilterConfig fc) throws ServletException {
this.filterConfig = fc;
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
String encoding = filterConfig.getInitParameter("encoding");
req.setCharacterEncoding(encoding);
res.setCharacterEncoding(encoding);
res.setContentType("text/html;charset=" + encoding);
System.err.println("encoding"+encoding);
System.err.println(req.getCharacterEncoding());
System.err.println(res.getCharacterEncoding());
System.err.println(res.getContentType());
chain.doFilter(req, res);
}
public void destroy() {
this.filterConfig = null;
}
}
index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <form action="test.jsp" method="get">
<input type="text" name="message" />
<input type="submit">
</form>test.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%=request.getParameter("message") %> index.jsp 和test.jsp 我 只粘了关键代码。提交数据中文总是乱码 无限崩溃中Tomcat 5.5 和6.0 都试过了。 配置文件 service.xml 中的配置也更改过。也不行。 myeclispe 版本是8.5 的。 暂未使用struts 或hibernate 又或spring 等框架。 只是简单创建了一个工程 添加了过滤器,修改了web.xml 。写了两个jsp 页面。 乱码无线循环中http://topic.csdn.net/u/20110810/21/bb4660e1-37ea-47d6-aca7-2b3029ce7681.html 这篇帖子中的回答仍没解决问题。
求解 在线等。 要是闲分少。 可以再开帖
<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">
<!-- 字符过滤器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.pro.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.act</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
EncodingFilter.javapackage com.pro.filter;
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 EncodingFilter implements Filter {
private FilterConfig filterConfig = null;
public void init(FilterConfig fc) throws ServletException {
this.filterConfig = fc;
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
String encoding = filterConfig.getInitParameter("encoding");
req.setCharacterEncoding(encoding);
res.setCharacterEncoding(encoding);
res.setContentType("text/html;charset=" + encoding);
System.err.println("encoding"+encoding);
System.err.println(req.getCharacterEncoding());
System.err.println(res.getCharacterEncoding());
System.err.println(res.getContentType());
chain.doFilter(req, res);
}
public void destroy() {
this.filterConfig = null;
}
}
index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <form action="test.jsp" method="get">
<input type="text" name="message" />
<input type="submit">
</form>test.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%=request.getParameter("message") %> index.jsp 和test.jsp 我 只粘了关键代码。提交数据中文总是乱码 无限崩溃中Tomcat 5.5 和6.0 都试过了。 配置文件 service.xml 中的配置也更改过。也不行。 myeclispe 版本是8.5 的。 暂未使用struts 或hibernate 又或spring 等框架。 只是简单创建了一个工程 添加了过滤器,修改了web.xml 。写了两个jsp 页面。 乱码无线循环中http://topic.csdn.net/u/20110810/21/bb4660e1-37ea-47d6-aca7-2b3029ce7681.html 这篇帖子中的回答仍没解决问题。
求解 在线等。 要是闲分少。 可以再开帖
解决方案 »
- form表单提交action后无法接受到参数
- JPA 注解 主键 insert=false and update=false
- JSF + tomacat + MyEclipse + hibernate javax.faces.el.EvaluationException: java.
- 2台机器上远程调用EJB错误问题
- ssh一个奇怪问题
- webservice返回List如何解析
- 帮忙看个数据库的问题。。。。sql语句
- hibernate怪问题
- 各位哥哥呀!看看吧!
- 问个简单问题,
- 写一个类倒叙输出“hello,world”
- 求助:java.io.IOException: Missing config file~急
msg = new String(msg.getBytes("ISO-8859-1"),"GBK")%><%=msg %>
这种方法也试过了还是不行,,我没有使用过滤器,
最后看来这个方法,在Struts配置文件<Struts></Struts>中加入<constant name="struts.i18n.encoding" value="gbk" />,就能正常显示了!