當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JSP乱码解决(过虑器EncodingFilter)
生活随笔
收集整理的這篇文章主要介紹了
JSP乱码解决(过虑器EncodingFilter)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
做JSP程序時(shí)頁面?zhèn)鞯奈淖纸?jīng)常是亂碼,如何解決呢? 一種方法是把tomcat中所有的<Connector>標(biāo)簽的URIEncoding屬性進(jìn)行設(shè)置,如: <Connector port="8000" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" /> 而另一種方法就是寫過濾器 import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; public class EncodingFilter implements Filter {
??? FilterConfig config = null;
??? // default to GBK
??? private String targetEncoding = "GBK"; public void init(FilterConfig config) throws ServletException {
??????? this.config = config;
??????? this.targetEncoding = config.getInitParameter("encoding");
??? } public void destroy() {
??????? config = null;
??????? targetEncoding = null;
??? } public void doFilter(ServletRequest srequest, ServletResponse sresponse,
???????????????????????? FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)srequest;
??????? request.setCharacterEncoding(targetEncoding);
??????? chain.doFilter(srequest, sresponse);
??? }
} 配置:在web.xml中添加
??? <filter>
??????? <filter-name>encodingFilter</filter-name>
??????? <filter-class>xx.xx.xx.EncodingFilter</filter-class>
??????? <init-param>
??????????? <param-name>encoding</param-name>
??????????? <param-value>Shift_JIS</param-value>
??????? </init-param>
??????? <init-param>
??????????? <param-name>useragents</param-name>
??????????? <param-value>Mac</param-value>
??????? </init-param>
??? </filter>
??? <filter-mapping>
??????? <filter-name>encodingFilter</filter-name>
??????? <url-pattern>/*</url-pattern>
??? </filter-mapping>
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; public class EncodingFilter implements Filter {
??? FilterConfig config = null;
??? // default to GBK
??? private String targetEncoding = "GBK"; public void init(FilterConfig config) throws ServletException {
??????? this.config = config;
??????? this.targetEncoding = config.getInitParameter("encoding");
??? } public void destroy() {
??????? config = null;
??????? targetEncoding = null;
??? } public void doFilter(ServletRequest srequest, ServletResponse sresponse,
???????????????????????? FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)srequest;
??????? request.setCharacterEncoding(targetEncoding);
??????? chain.doFilter(srequest, sresponse);
??? }
} 配置:在web.xml中添加
??? <filter>
??????? <filter-name>encodingFilter</filter-name>
??????? <filter-class>xx.xx.xx.EncodingFilter</filter-class>
??????? <init-param>
??????????? <param-name>encoding</param-name>
??????????? <param-value>Shift_JIS</param-value>
??????? </init-param>
??????? <init-param>
??????????? <param-name>useragents</param-name>
??????????? <param-value>Mac</param-value>
??????? </init-param>
??? </filter>
??? <filter-mapping>
??????? <filter-name>encodingFilter</filter-name>
??????? <url-pattern>/*</url-pattern>
??? </filter-mapping>
轉(zhuǎn)載于:https://blog.51cto.com/fluagen/32190
總結(jié)
以上是生活随笔為你收集整理的JSP乱码解决(过虑器EncodingFilter)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上周回顾:***猖狂叫板欧美政府 赛门铁
- 下一篇: 匹克是哪个国家的品牌 创立于1989年