(1):用过滤器
新建一个类ChineseFilter
//生成自己的过滤器
package org.perfect.struts.filters;
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;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ChineseFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
String encoding=filterConfig.getInitParameter("encoding");//从WEB.xml配置文件中取出参数,这样我们可以通过配置修改编码格式.
request.setCharacterEncoding(encoding);//设置请求的编码格式
System.out.println(encoding);
filterChain.doFilter(request, response);//关键
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
public void destroy() {
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
super.doGet(arg0, arg1);
}
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
super.doPost(arg0, arg1);
}
}
编译后把.class文件放到项目的web-inf的classes中
然后在修改web.xml
加上
<filter>
<filter-name>ChineseFilter</filter-name>
<filter-class>org.perfect.struts.filters.ChineseFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ChineseFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
重启服务器,应该可以了
第二种方式是:
把获取来的参数重新编码
String username = new String(request.getParameter("username").getBytes("iso-8859-1"),"GBK");
这样也可以了
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
JS中理解码点和码元,解决emoji带来的乱码问题
了解码元和码点,处理emoji表情字符串截取带来的乱码问题,在String原型中增加自定义方法来处理
码元 码点 emoji乱码 字符串截取 字符串长度 -
springmvc 的原理以及工作流程(面试)
springMVC 工作流程及原理
MVC 工作流程 原理 -
SpringMVC jsp界面值渲染不出来
使用springmvc开发过程中,jsp无法值渲染出来。Controller@Controllerpublic class MvcController{ @GetMappin("/
JSP springmvc html mvc spring -
jsp跳转JSP
-
SpringMVC中乱码及Restful
乱码的解决及restful风格的url
springmvc 乱码 restful -
python官方下载社区
Python环境搭建Python可应用于多平台包括Windows、 Linux/Unix 和 Mac OS。Python下载Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到:Python官网:http://www.python.org/你可以在以下链接中下载 Python 的文档,你可以下载 HTML、PDF 和 PostScript 等格式的文档。Python文档下载地
python官方下载社区 python官网怎么下载pycharm Python python Windows