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);
} 展开
3个回答
展开全部
要判断是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");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询