使用filter解决request.getParameter的中文乱码问题
2014-01-11 17:29
471 查看
注意:一般一个站点的所有页面的编码,包括数据库编码都要保持一致,下面默认的编码都是UTF-8
----------------------------------例1:直接提交到jsp页面----------------------------------
input_info.html
display_info.jsp
CharacterEncodingFilter.java
charset参数是从web.xml中配置好的,这里读取进来
web.xml
--------例2:提交到Servlet,Serlvet再设置为request的attribute,再提交到jsp页面--------
AttributeSetterServlet.java
input_attribute.html
display_attribute.jsp
web.xml中添加如下代码:
----------------------------------例1:直接提交到jsp页面----------------------------------
input_info.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="display_info.jsp" method="post"> <!-- 输入中文来测试 --> 请输入要显示的内容:<input type="text" name="info"> <input type="submit" value="显示"> </form> </body> </html>
display_info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" 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="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 注意这里并没有调用request.setCharacterEncoding(), 你把CharacterEncodingFilter关闭了试试,看看结果有什么 不一样 --> <% String str = (String) request.getParameter("info"); %> <h1><%=str%></h1> </body> </html>
CharacterEncodingFilter.java
charset参数是从web.xml中配置好的,这里读取进来
public final class CharacterEncodingFilter implements Filter { private String encoding = null; private boolean enable = false; public void destroy() { this.encoding = null; } /** * 完成request.setCharacterEncoding(encoding); */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (this.enable) { String encoding = this.selectEncoding(request); if (encoding != null && !encoding.equals("")) { request.setCharacterEncoding(encoding); } } // Pass control on to the next filter chain.doFilter(request, response); // After other filters are done: if (this.enable) { String encoding = this.selectEncoding(request); if (encoding != null && !encoding.equals("")) { // response.setCharacterEncoding(encoding); // 实验证明这货没起什么作用,所以,不要也行 } } } /** * 读取encoding参数以及enable参数,enable参数与encoding的功能无关,只是用来启用或者停用这个filter的 */ public void init(FilterConfig filterConfig) throws ServletException { this.encoding = filterConfig.getInitParameter("encoding"); if (!Charset.isSupported(encoding)) { encoding = null; // 如果不支持的话,只能选择默认的encoding了,这个filter就不起作用 } // 读取enable参数 String enableString = filterConfig.getInitParameter("enable"); if (enableString.equalsIgnoreCase("true")) { this.enable = true; } else { this.enable = false; } } }
web.xml
<filter> <filter-name>charset-encoding</filter-name> <filter-class>org.foo.filterdemo.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>enable</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>charset-encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
--------例2:提交到Servlet,Serlvet再设置为request的attribute,再提交到jsp页面--------
AttributeSetterServlet.java
public class AttributeSetterServlet extends HttpServlet { /** * Generated serialVersionUID */ private static final long serialVersionUID = 8458482445490259121L; @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 在这里也没有调用request.setCharacterEncoding(), // 你把CharacterEncodingFilter关闭了试试,看看结果有什么 // 不一样 String attr = req.getParameter("attr"); if (attr != null) { // System.out.println(this + ", " + attr); // @Debug attr += " -- 这是一个小尾巴"; req.setAttribute("attr", attr); req.getRequestDispatcher("display_attribute.jsp").forward(req, resp); } } @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
input_attribute.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="AttributeSetterServlet" method="post"> <!-- 输入中文来测试 --> 请输入要设置的attribute:<input type="text" name="attr"> <input type="submit" value="显示"> </form> </body> </html>
display_attribute.jsp
<%@ page language="java" 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> <title>Insert title here</title> </head> <body> <h1>设置的attribute是:<%=request.getAttribute("attr")%></h1> </body> </html>
web.xml中添加如下代码:
<servlet> <servlet-name>attribute-setter-servlet</servlet-name> <servlet-class> org.foo.servletdemo.AttributeSetterServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>attribute-setter-servlet</servlet-name> <url-pattern>/AttributeSetterServlet</url-pattern> </servlet-mapping>
相关文章推荐
- 使用Server.UrlEncode解决传递request中文乱码的问题
- SpringMVC项目中中文字符乱码问题及解决办法总结(非专业最优解决办法) -- ajax传值乱码; request.getParameter()乱码;
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
- request.getParameter()及解决数据库中文乱码问题——实习第七天
- 通过过滤器(Filter)解决JSP的Post和Request中文乱码问题
- ajax post请求中文乱码问题解决(不使用escape方法,只使用filter)
- Struts(2)Struts快速入门案例和使用filter配合Struts,解决中文乱码问题
- 使用filter解决中文乱码问题--struts2乱码
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
- 使用过滤器Filter解决Java系统中中文乱码的问题
- 使用Filter解决中文乱码问题
- 使用filter解决get请求中文乱码问题
- Filter的使用(解决中文乱码问题)
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
- 使用request.getParameter(“参数名”)取网页中输入值的中文乱码解决方法
- request.getParameter获取中文乱码的问题解决
- 通过过滤器(Filter)解决JSP的Post和Request中文乱码问题
- struts2 使用filter解决中文乱码问题
- 采用Filter的方法解决HttpServletRequest.getParameter乱码的问题
- 使用RFileLogger记录中文乱码问题解决