jsp 为何设置了request 和response编码之后还是出现乱码
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)thr...
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
HttpServletRequest httpreq = (HttpServletRequest)request;
HttpServletResponse httpresp = (HttpServletResponse)response; chain.doFilter(httpreq, httpresp);
} 展开
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
HttpServletRequest httpreq = (HttpServletRequest)request;
HttpServletResponse httpresp = (HttpServletResponse)response; chain.doFilter(httpreq, httpresp);
} 展开
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
展开全部
要判断是get方式提交的还是post ,get方式和post方式处理中文乱码都不一样的,get先要打成iso8859-1然后再转换成utf8,post直接设置
===========================================================
public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletrequest;
HttpServletResponse response = (HttpServletResponse) servletresponse;
response.setContentType("text/html;charset=UTF-8");
if (request.getMethod().equalsIgnoreCase("POST")) {
request.setCharacterEncoding("UTF-8");
} else if (request.getMethod().equalsIgnoreCase("GET")) {
Map map = request.getParameterMap();
Iterator it = map.keySet().iterator();
String key = null;
String[] values = null;
while (it.hasNext()) {
key = it.next().toString();
values = (String[]) map.get(key);
for (int i = 0; i < values.length; i++) {
values[i] = new String(values[i].getBytes("ISO-8859-1"),"UTF-8");
}
}
}
chain.doFilter(servletrequest, servletresponse);
}
===========================================================
public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletrequest;
HttpServletResponse response = (HttpServletResponse) servletresponse;
response.setContentType("text/html;charset=UTF-8");
if (request.getMethod().equalsIgnoreCase("POST")) {
request.setCharacterEncoding("UTF-8");
} else if (request.getMethod().equalsIgnoreCase("GET")) {
Map map = request.getParameterMap();
Iterator it = map.keySet().iterator();
String key = null;
String[] values = null;
while (it.hasNext()) {
key = it.next().toString();
values = (String[]) map.get(key);
for (int i = 0; i < values.length; i++) {
values[i] = new String(values[i].getBytes("ISO-8859-1"),"UTF-8");
}
}
}
chain.doFilter(servletrequest, servletresponse);
}
更多追问追答
追问
String[] values = null;
while (it.hasNext()) {
key = it.next().toString();
values = (String[]) map.get(key);
for (int i = 0; i < values.length; i++) {
values[i] = new String(values[i].getBytes("ISO-8859-1"),"UTF-8");
}
}
你只是把request里面的参数的值取出来了,怎么再传回去覆盖原来的值呢,有什么函数吗?
追答
这是过滤器,你在执行post和get请求的之前就被过滤到了,所以你不用担心把转好的值再赋值
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
UTF-8不支持中文,支持中文的只有GBK和GB2312
还有如果用JS传参数带中文,那是需要转编码,类似
if(request.getParameter("aaa") != null){
String a = new String(request.getParameter("aaa").getBytes("iso8859-1"),"gb2312");
}
还有如果用JS传参数带中文,那是需要转编码,类似
if(request.getParameter("aaa") != null){
String a = new String(request.getParameter("aaa").getBytes("iso8859-1"),"gb2312");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询