Filterpost请求中文字符编码的过滤器 --学习笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Filterpost请求中文字符编码的过滤器 --学习笔记相关的知识,希望对你有一定的参考价值。

java代码:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet1 extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String passwrod = request.getParameter("passwrod");

        //显示
        response.getWriter().write("用户名 :"+username);
        response.getWriter().write("密码:"+passwrod);

    }

}

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 FilterDemo3 implements Filter {
    private FilterConfig filterConfig;

    public FilterDemo3()
    {

    }
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
    //Web容器调用
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
        String encoding = filterConfig.getInitParameter("encoding");
        //POST请求编码设置
        request.setCharacterEncoding(encoding);
        //响应编码设置
        response.setContentType("text/html;charset="+encoding);
        chain.doFilter(request,response);
    }
    public void destroy() {
    }

}

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>登陆页面</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <form action="/day04/LoginServlet1" method="post">
       <table border="1" align="center">
       <caption>用户登陆</caption>
          <tr>
            <th>用户名</th>
            <td><input type="text" name="username"/>
            </td>
          </tr>
          <tr>
            <th>用户密码</th>
            <td><input type="password" name="passwrod"/>
            </td>
          </tr>
           <tr>
              <td colspan="5" align="center">
               <input type="submit" value="提交"/>
              </td>
           </tr>        
       </table>
    </form>
  </body>
</html>

web.xml

<filter>
        <filter-name>FilterDemo3</filter-name>
        <filter-class>cn.web.servlet.filter.FilterDemo3</filter-class>
        <init-param>
           <param-name>encoding</param-name>
           <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>FilterDemo3</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

以上是关于Filterpost请求中文字符编码的过滤器 --学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

servlet中的字符编码过滤器的使用

Java乱码问题集合

JAVA_WEB 编码格式过滤器

Filter学习Filter(过滤器)常见应用

iOS 开发 post 提交数据 中文乱码 一串问号,怎么解决

深入Struts2的过滤器FilterDispatcher--中文乱码及字符编码过滤器