日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何用过滤器过滤HTTP协议和非HTTP协议编码

發(fā)布時間:2025/7/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何用过滤器过滤HTTP协议和非HTTP协议编码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準>>>

在編寫web時,最苦惱的事情就是亂碼了。

一直沒怎么考慮過JSP第一句話中

<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>的ContentType和pageEncoding具體是用做什么的,害自己做了過濾器也老是出現(xiàn)問題。

Google下:

contentType ------------指定的是JSP頁最終 Browser(客戶端)所見到的網(wǎng)頁內(nèi)容的編碼.pageEncoding ------------指定JSP編寫時所用的編碼 如果害怕忘了在每個jsp都加上這樣的編碼,或者由于不同環(huán)境所需的客戶端所見編碼的不同。加上過濾器來解決

1.HTTP協(xié)議下的過濾:

(1.login.jsp ? )用來模擬輸入數(shù)據(jù)登錄

<%@ page language="java" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" > <title>HTTP協(xié)議和URL編碼</title> </head> <body> <p>這里僅僅POST編碼測試</p> <form action="main.jsp" method="post"> <label>測試HTTP協(xié)議</label> <input type="text" name="infoPost" /> <input type="submit" value="提交"> </form> <hr> <p>測試通過GET傳送(url方式)</p> <form action="main.jsp" method="get"> <label>測試URL</label> <input type="text" name="infoGet" /> <input type="submit" value="提交"> </form> </body> </html>

(2.main.jsp ?)用來模擬得到數(shù)據(jù)

<%@ page language="java" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" > <title>獲取頁面?zhèn)鬟^來的值</title> </head> <body>獲取通過HTTP協(xié)議傳送過來的值 <%=request.getParameter("infoPost") %> <hr> 獲取通過URL傳送過來的值 <%=request.getParameter("infoGet") %> </body> </html>(3.web.xml)配置

<!-- 編碼過濾器 --><filter><filter-name>EncodeFilter</filter-name><filter-class>demo.filter.EncodeFilter</filter-class><init-param><param-name>encoding</param-name><param-value>GBK</param-value></init-param></filter><filter-mapping><filter-name>EncodeFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

(4.EncodeFilter.java)過濾器類

package demo.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.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/** 字符編碼 */ public class EncodeFilter implements Filter {protected String encoding;protected FilterConfig filterConfig;public EncodeFilter() {this.encoding = null;this.filterConfig = null;}public void destroy() {filterConfig = null;encoding = null;}public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws IOException, ServletException {/** 處理通過HTTP協(xié)議提交的數(shù)據(jù)*/HttpServletRequest hrequest = (HttpServletRequest) request;HttpServletResponse hresponse = (HttpServletResponse) response;// 這里省略前面兩段,直接用request..setCharacterEncoding(encoding);效果是一樣的,這里為了說明這個是通過HTTP// 協(xié)議提交System.out.println("編碼格式" + encoding);hrequest.setCharacterEncoding(encoding);filterChain.doFilter(request, response);}/** 過濾器初始化*/public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;encoding = filterConfig.getInitParameter("encoding");}}
結(jié)果:

??

post方式參數(shù)存放在請求數(shù)據(jù)包的消息體中。get方式參數(shù)存放在請求數(shù)據(jù)包的請求行的URI字段中,以?開始以param=value&&parame2=value2的形式附加在URI字段之后。而request.setCharacterEncoding(charset); 只對消息體中的數(shù)據(jù)起作用,對于URI字段中的參數(shù)不起作用。

可以繼承擴展HttpServletRequestWrapper 增加過濾功能

添加一個MyEncodeFilter類

package demo.filter;import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper;public class MyEncodeFilter extends HttpServletRequestWrapper{private String encoding="GBK";public MyEncodeFilter(HttpServletRequest request) {super(request);}public MyEncodeFilter(HttpServletRequest request,String encoding) {super(request);this.encoding=encoding;}//新增加一個轉(zhuǎn)碼方法public String encodeName(String en){try {return new String(en.trim().getBytes("ISO-8859-1"),encoding);} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blockreturn en;}}//增強過濾的方法,就是將URI字段中的參數(shù)編碼(重寫)public String getParameter(String name) {//直接調(diào)用父類的getParameter方法獲取參數(shù)的值String value=super.getParameter(name);//如果不為空就將其轉(zhuǎn)碼value=value==null?null:encodeName(value);return value;}}
改變后的Filter:

package demo.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.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/** 字符編碼 */ public class EncodeFilter implements Filter {protected String encoding;protected FilterConfig filterConfig;public EncodeFilter() {this.encoding = null;this.filterConfig = null;}public void destroy() {filterConfig = null;encoding = null;}public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws IOException, ServletException {/** 處理通過HTTP協(xié)議提交的數(shù)據(jù)*/HttpServletRequest hrequest = (HttpServletRequest) request;// 這里省略前面兩段,直接用request..setCharacterEncoding(encoding);效果是一樣的,這里為了說明這個是通過HTTP// 協(xié)議提交System.out.println("編碼格式" + encoding);hrequest.setCharacterEncoding(encoding);//新增加方法<只要通過url來頭中肯定有g(shù)et>if(hrequest.getMethod().equalsIgnoreCase("get")){hrequest=new MyEncodeFilter(hrequest,encoding);}//filterChain.doFilter(hrequest, response);}/** 過濾器初始化*/public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;encoding = filterConfig.getInitParameter("encoding");}}
解決成功!



轉(zhuǎn)載于:https://my.oschina.net/91jason/blog/295370

總結(jié)

以上是生活随笔為你收集整理的如何用过滤器过滤HTTP协议和非HTTP协议编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。