资源描述
,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,*,单击此处编辑母版标题样式,Jsp,中文乱码处理及过滤器的使用,主讲:曹莹,Jsp,中中文乱码的处理,1.,转换成字节数组之后,再重新生成字符串,2.,对每一个,JSP,页面使用,request.setCharacterEncoding(,“GBK,),3.,使用过滤器过滤实现中文乱码,4.,先编码然后再解码,java,url,的,encoder,和,decoder,.URLEncoder,和,.URLDecoder,过滤器类的定义,过滤器类必须实现,javax.servlet.Filter,该类必须实现三个方法:,init(FilterConfig,arg0,),destroy(),doFilter(ServletRequest,arg0,ServletResponse,arg1,FilterChain,arg2),利用过滤器修改中文乱码,package,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,.*;,public,class,ModifyCodeFilter,implements,Filter,protected,FilterConfig,filterConfig,;,private,String,targetEncoding,=GBK;,public,void,destroy(),this,.filterConfig,=,null,;,public,void,doFilter(ServletRequest,arg0,ServletResponse,arg1,FilterChain,arg2),throws,IOException,ServletException,HttpServletRequest,srequest,=(HttpServletRequest)arg0;,try,srequest.setCharacterEncoding(,this,.targetEncoding,);,arg2.doFilter(arg0,arg1);,catch,(Exception,e),e.printStackTrace,();,public,void,init(FilterConfig,arg0),throws,ServletException,this,.filterConfig,=arg0;,this,.targetEncoding,=arg0.getInitParameter(code);,web.xml,项目配置文件的设置,modifycode,filter.ModifyCodeFilter,eng_china,code,GBK,modifycode,/*,利用过滤器修改留言中的非法信息,package,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,.*;,public,class,ContentFilter,implements,Filter,private,FilterConfig,filterconfig,=,null,;,String,str,=,我们的,你们的,他们的,;,String content=;,public,void,destroy(),this,.filterconfig,=,null,;,public,void,doFilter(ServletRequest,arg0,ServletResponse,arg1,FilterChain,arg2),throws,IOException,ServletException,HttpServletRequest,hsrequest,=(HttpServletRequest)arg0;,content=,hsrequest.getParameter(msgcontent,);,try,if,(content,!=,null,),int,i=0;,for,(i,=0;i,str.length;i,+),String,substr,=,stri,;,if,(content.indexOf(substr,)=-1),hsrequest.setAttribute(msgcontent,content);,else,content=,content.replace(substr,xxx,);,hsrequest.setAttribute(msgcontent,content);,arg2.doFilter(arg0,arg1);,catch,(Exception,e),e.printStackTrace,();,public,void,init(FilterConfig,arg0),throws,ServletException,this,.filterconfig,=arg0;,contentmodify,contentmodify,contentmodify,filter.ContentFilter,contentmodify,/*,
展开阅读全文