java web中文编码问题(ajax中编码问题)

时间:2022-11-05 22:01:57

再谈java web中文编码问题,这次稍微有些复杂,先说遇到的问题。

一、遇到的问题

1、用ajax中的load方法加载文件

  $("#customerTypeLevelRelList").load("<%=CONTEXT%>/jsp_lib/store/web/customerPrice.jsp", {customerRootId:customerRootId, storeId:<%=store.getStoreId()%>});

其中customerRootId为中文“洪海亮”,传到customerPrice.jsp取到的却是乱码

String customerRootId = request.getParameter("customerRootId");

java web中文编码问题(ajax中编码问题)

2、load方法普及

java web中文编码问题(ajax中编码问题)


3、传入的是数据对象,则发送的是post请求,但是系统中用post请求发送的中文参数没有问题。


二、问题原因

看其调用栈,红色标注的地方,多了一个SQLFilter,在web.xml中确实配置了并且只是针对.jsp才过滤,防止数据库注入的

<filter-mapping>
  <filter-name>SQL Filter</filter-name>
  <url-pattern>*.jsp</url-pattern>
 </filter-mapping>


customerPrice.jsp line: 18 


customerPrice_jsp(HttpJspBase).service(HttpServletRequest,HttpServletResponse) line: 98  


customerPrice_jsp(HttpServlet).service(ServletRequest,ServletResponse) line: 717  


JspServletWrapper.service(HttpServletRequest,HttpServletResponse, boolean) line: 388  


JspServlet.serviceJspFile(HttpServletRequest,HttpServletResponse, String, Throwable, boolean) line: 313 


JspServlet.service(HttpServletRequest,HttpServletResponse) line: 260


JspServlet(HttpServlet).service(ServletRequest,ServletResponse) line: 717  


ApplicationFilterChain.internalDoFilter(ServletRequest,ServletResponse) line: 290 


ApplicationFilterChain.doFilter(ServletRequest,ServletResponse) line: 206  


SiteMeshFilter(ContentBufferingFilter).bufferAndPostProcess(FilterChain,HttpServletRequest, HttpServletResponse, Selector) line: 169 


SiteMeshFilter(ContentBufferingFilter).doFilter(ServletRequest,ServletResponse, FilterChain) line: 126 


MyConfigurableSiteMeshFilter(ConfigurableSiteMeshFilter).doFilter(ServletRequest,ServletResponse, FilterChain) line: 163  


MyConfigurableSiteMeshFilter.doFilter(ServletRequest,ServletResponse, FilterChain) line: 81   


ApplicationFilterChain.internalDoFilter(ServletRequest,ServletResponse) line: 235 


ApplicationFilterChain.doFilter(ServletRequest,ServletResponse) line: 206  


OrgFilter.doFilter(ServletRequest, ServletResponse,FilterChain) line: 204  


ApplicationFilterChain.internalDoFilter(ServletRequest,ServletResponse) line: 235 


ApplicationFilterChain.doFilter(ServletRequest,ServletResponse) line: 206  


SetCharacterEncodingFilter.doFilter(ServletRequest,ServletResponse, FilterChain) line: 142   


ApplicationFilterChain.internalDoFilter(ServletRequest,ServletResponse) line: 235 


ApplicationFilterChain.doFilter(ServletRequest,ServletResponse) line: 206  


SQLFilter.doFilter(ServletRequest,ServletResponse, FilterChain) line: 52


ApplicationFilterChain.internalDoFilter(ServletRequest,ServletResponse) line: 235 


ApplicationFilterChain.doFilter(ServletRequest,ServletResponse) line: 206  


StandardWrapperValve.invoke(Request, Response) line: 233   


StandardContextValve.invoke(Request, Response) line: 191   


StandardHostValve.invoke(Request, Response) line: 127  


ErrorReportValve.invoke(Request, Response) line: 103


StandardEngineValve.invoke(Request, Response) line: 109


CoyoteAdapter.service(Request, Response) line: 293  


Http11AprProcessor.process(long) line: 879   


Http11AprProtocol$Http11ConnectionHandler.process(long)line: 600 


AprEndpoint$Worker.run() line: 1703   


Thread.run() line: 745  


其中用到了,这个时候还没有设置setCharacterEncoding,因此采用默认编码格式ISO-8859-1解码,由于ajax中post请求默认是用UTF-8编码的,所以出现了乱码。

Iterator values = httpRequest.getParameterMap().values().iterator();// 获取所有的表单参数
Iterator values2 = httpRequest.getParameterMap().values().iterator();// 获取所有的表单参数

SetCharacterEncodingFilter中虽然再对编码格式进行设置,这时候已经没有用了(post表单提交的参数的解码是在第一次调用request.getParameter也包括getParameterMap时发生的),也就是说已经按照ISO-8859-1解码了,再设置编码格式已经没用了。

由于ajax中post请求默认是用UTF-8编码的,这也就说明为什么SetCharacterEncodingFilter中对ajax请求都设置了UTF-8编码格式

java web中文编码问题(ajax中编码问题)

三、解决方法

调整filter的顺序,将SetCharacterEncodingFilter放在首位,保证解码之前设置了编码格式