JSP的6种乱码解决方案,linux教程视频

该博客详细介绍了如何使用Java过滤器处理HTTP请求中的POST和GET方式的乱码问题。通过覆盖HttpServletRequestWrapper类并重写相关方法,确保请求参数以UTF-8编码进行解码,从而避免ISO8859-1编码导致的乱码。同时,展示了在web.xml中配置过滤器的过程。
摘要由CSDN通过智能技术生成

public void destroy() {

System.out.println(“过滤器销毁…”);

}

@Override

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

System.out.println(“进行过滤器开发…”);

// 只要在这里加 先解决post请求乱码

request.setCharacterEncoding(“utf-8”);// 请求

response.setCharacterEncoding(“utf-8”);// 响应

response.setContentType(“text/html;charset=utf-8”);// 响应

// get提交乱码处理

chain.doFilter(new MyRequest((HttpServletRequest) request), response);

}

@Override

public void init(FilterConfig filterConfig) throws ServletException {

System.out.println(“过滤器初始化…”);

}

class MyRequest extends HttpServletRequestWrapper {

HttpServletRequest request;

public MyRequest(HttpServletRequest request) {

super(request);

this.request = request;

}

@Override

public String getParameter(String name) {

String value = request.getParameter(name);

// 先判断是否为空

if (value == null) {

return null;

}

// 再判断提交方法

if (!request.getMethod().equals(“GET”)) {

return value;

}

// get提交

try {

value = new String(value.getBytes(“iso8859-1”), “utf-8”);

} ca

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

tch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return value;

}

@Override

public String[] getParameterValues(String name) {

String[] oldValues = request.getParameterValues(name);

String[] newValues = null;

if (oldValues != null) {

newValues = new String[oldValues.length];

if (!request.getMethod().equals(“GET”)) {

return oldValues;

}

if (newValues != null) {

try {

for (int i = 0; i < oldValues.length; i++) {

String value = oldValues[i];

value = new String(value.getBytes(“iso8859-1”),

“utf-8”);

newValues[i] = value;

}

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return newValues;

}

@Override

public Map<String, String[]> getParameterMap() {

// 1.拿到客户端的值

Map<String, String[]> oldMap = request.getParameterMap();

// 2.新map

Map<String, String[]> newMap = new HashMap<String, String[]>();

// 3.拿出oldMap

Set<Entry<String, String[]>> entrySet = oldMap.entrySet();

for (Entry<String, String[]> entry : entrySet) {

// 4.获取键

String key = entry.getKey();

// 5.获取值

String[] values = entry.getValue();

// 新的数组和原数组一样大

String[] newValues = new String[values.length];

// 6.判断值是否为空

if (values == null) {

newMap.put(key, new String[] {});// 初始化

continue;

}

// 7.取出values里面每一个值进行转码

try {

for (int i = 0; i < values.length; i++) {

String value = values[i];

value = new String(value.getBytes(“iso8859-1”), “utf-8”);

// value = new String(value.getBytes(“iso8859-1”),

// request.getCharacterEncoding());

newValues[i] = value;

}

newMap.put(key, newValues);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return newMap;

}

}

}

在web.xml配置过滤器:

Set Character Encoding

com.wb.filter.TestFilter

encoding

UTF-8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值