HTTP/1.1協議中共定義了八種方法來代表Request-URI指定的資源的不一樣操做方式:
方法名稱區分大小寫。html
ps.
當某個請求所針對的資源不支持對應的請求方法的時候,服務器應當返回狀態碼405(Method Not Allowed);
當服務器不認識或者不支持對應的請求方法的時候,應當返回狀態碼501(Not Implemented)。java
GET產生一個TCP數據包;POST產生兩個TCP數據包!web
GET:瀏覽器會把http header和data一併發送出去,服務器響應200(返回數據);
POST: 瀏覽器先發送header,服務器響應100 continue,瀏覽器再發送data,服務器響應200 ok(返回數據)。spring
中文亂碼:
Post方式:在JSP中request解析數據時設置編碼格式:request.setCharacterEncoding("utf-8"); 也可使用Spring的CharacterEncodingFilter統一setCharacterEncoding。windows
<!-- characterEncodingFilter字符編碼過濾器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <!--要使用的字符集,通常咱們使用UTF-8(保險起見UTF-8最好)--> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <!--是否強制設置request的編碼爲encoding,默認false,不建議更改--> <param-name>forceRequestEncoding</param-name> <param-value>false</param-value> </init-param> <init-param> <!--是否強制設置response的編碼爲encoding,建議設置爲true--> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <!--這裏不能留空或者直接寫 ' / ' ,否者不起做用--> <url-pattern>/*</url-pattern> </filter-mapping>
CharacterEncodingFilter: private String encoding; //要使用的字符集,通常咱們使用UTF-8(保險起見UTF-8最好) private boolean forceRequestEncoding = false; //是否強制設置request的編碼爲encoding private boolean forceResponseEncoding = false; //是否強制設置response的編碼爲encoding @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String encoding = getEncoding(); if (encoding != null) { //若是設置了encoding的值,則根據狀況設置request和response的編碼 //若設置request強制編碼或request自己就沒有設置編碼則設置編碼爲encoding表示的值 if (isForceRequestEncoding() || request.getCharacterEncoding() == null) { request.setCharacterEncoding(encoding); } //若設置response強制編碼,則設置編碼爲encoding表示的值 if (isForceResponseEncoding()) { response.setCharacterEncoding(encoding); } } filterChain.doFilter(request, response); }
Get方式: 對url編碼encodeURI; 修改tomcat的配置server.xml <Connector> URIEncoding="UTF-8"。瀏覽器
<!-- windows --> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /> <Connector port="8099" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
引貼: GET和POST兩種基本請求方法的區別tomcat