Filter修改全站请求响应编码

释放双眼,带上耳机,听听看~!

form.html表单

package p01_decorate;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class Charset extends HttpServletRequestWrapper {

    //引入被增强对象
    private HttpServletRequest request;
    public Charset(HttpServletRequest req) {
        super(req);
        this.request = req;
    }

    @Override
    public String getParameter(String name) {
        String value = request.getParameter(name);
        if(value==null) {
            return null;
        }
        String method = request.getMethod();
        if("get".equalsIgnoreCase(method)) {
            try {
                value = new String (value.getBytes("iso-8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return value;
    }
}

form_2.jsp表单

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<center>
    <h3>用户登录</h3>
</center>
<body style="text-align: center;">
    <a href="<%=request.getContextPath()%>/CharsetServlet">单击超链接登录</a>
    <form action="<%=request.getContextPath()%>/CharsetServlet"
        method="post">
        <table border="1" width="600px" cellpadding="0" cellspacing="0"
            align="center">
            <tr>
                <td height="30" align="center">用户名:</td>
                <td> <input type="text" name="username" />
                </td>
            </tr>
            <tr>
                <td height="30" align="center">密   码:</td>
                <td> <input type="password" name="password" />
                </td>
            </tr>
            <tr>
                <td height="30" colspan="2" align="center">
                <input type="submit" value="登录" />
                   
                <input type="reset" value="重置" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

装饰加强request

package p01_decorate;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class Charset extends HttpServletRequestWrapper {

    //引入被增强对象
    private HttpServletRequest request;
    public Charset(HttpServletRequest req) {
        super(req);
        this.request = req;
    }

    @Override
    public String getParameter(String name) {
        String value = request.getParameter(name);
        if(value==null) {
            return null;
        }
        String method = request.getMethod();
        if("get".equalsIgnoreCase(method)) {
            try {
                value = new String (value.getBytes("iso-8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return value;
    }
}

过滤器

package p02_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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import p01_decorate.Charset;

public class CharsetFilter implements Filter {
    public CharsetFilter() {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //转换成带HTTP协议的请求与响应
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse) response;
        //处理响应中文的乱码问题
        req.setCharacterEncoding("utf-8"); // 只对消息体有效
        res.setContentType("text/html;charset=utf-8");
        //包装
        Charset charesetRequest = new Charset(req);
        chain.doFilter(charesetRequest, res);
    }
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

测试下来发现,所有jsp提交的内容,均可以通过iso-8859-1转换成UTF-8编码。但是如果使用的是html页面提交的中文内容,只能是UTF-8本身,无需转码。

人已赞赏
知识分享

Listener监听域对象的生命周期

2020-9-15 21:53:00

知识分享

Filter实现自动登录

2020-9-15 21:58:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索