码迷,mamicode.com
首页 > 其他好文 > 详细

过滤器解决中文乱码

时间:2019-08-10 21:18:16      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:缓冲区   Servle   over   html   enc   har   throw   xtend   coding   

@WebServlet(urlPatterns = "/form")
public class FormServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        response.getWriter().write("哈哈");
        System.out.println(username);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
@WebFilter(urlPatterns = "/*")
public class ChinaFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //在过滤器中,设置request对象的编码表
        servletRequest.setCharacterEncoding("utf-8");
        //设置response缓冲区的编码表,通知浏览器的解码
        servletResponse.setContentType("text/html;charset=utf-8");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {}
}
<body>
    <form action="${pageContext.request.contextPath}/form" method="post">
        <input type="text" name="username">
        <input type="submit">
    </form>
</body>

 

过滤器解决中文乱码

标签:缓冲区   Servle   over   html   enc   har   throw   xtend   coding   

原文地址:https://www.cnblogs.com/xiaomingVVV/p/11332802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!