day05
請求響應流程圖
response
l response概述
response是Servlet.service方法的一個參數,類型為javax.servlet.http.HttpServletResponse。在客戶端發出每個請求時,服務器都會創建一個response對象,並傳入給Servlet.service()方法。response對象是用來對客戶端進行響應的,這說明在service()方法中使用response對象可以完成對客戶端的響應工作。
response對象的功能分為以下四種:
l 設置響應頭信息;
l 發送狀態碼;
l 設置響應正文;
l 重定向;
2 response響應正文
response是響應對象,向客戶端輸出響應正文(響應體)可以使用response的響應流,repsonse一共提供了兩個響應流對象:
l PrintWriter out = response.getWriter():獲取字符流;
l ServletOutputStream out = response.getOutputStream():獲取字節流;
當然,如果響應正文內容為字符,那么使用response.getWriter(),如果響應內容是字節,例如下載時,那么可以使用response.getOutputStream()。
注意,在一個請求中,不能同時使用這兩個流!也就是說,要么你使用repsonse.getWriter(),要么使用response.getOutputStream(),但不能同時使用這兩個流。不然會拋出IllegalStateException異常。
2.1 字符響應流
l 字符編碼
在使用response.getWriter()時需要注意默認字符編碼為ISO-8859-1,如果希望設置字符流的字符編碼為utf-8,可以使用response.setCharaceterEncoding(“utf-8”)來設置。這樣可以保證輸出給客戶端的字符都是使用UTF-8編碼的!
但客戶端瀏覽器並不知道響應數據是什么編碼的!如果希望通知客戶端使用UTF-8來解讀響應數據,那么還是使用response.setContentType("text/html;charset=utf-8")方法比較好,因為這個方法不只會調用response.setCharaceterEncoding(“utf-8”),還會設置content-type響應頭,客戶端瀏覽器會使用content-type頭來解讀響應數據。
l 緩沖區
response.getWriter()是PrintWriter類型,所以它有緩沖區,緩沖區的默認大小為8KB。也就是說,在響應數據沒有輸出8KB之前,數據都是存放在緩沖區中,而不會立刻發送到客戶端。當Servlet執行結束后,服務器才會去刷新流,使緩沖區中的數據發送到客戶端。
如果希望響應數據馬上發送給客戶端:
l 向流中寫入大於8KB的數據;
l 調用response.flushBuffer()方法來手動刷新緩沖區;
3 設置響應頭信息
可以使用response對象的setHeader()方法來設置響應頭!使用該方法設置的響應頭最終會發送給客戶端瀏覽器!
l response.setHeader(“content-type”, “text/html;charset=utf-8”):設置content-type響應頭,該頭的作用是告訴瀏覽器響應內容為html類型,編碼為utf-8。而且同時會設置response的字符流編碼為utf-8,即response.setCharaceterEncoding(“utf-8”);
l response.setHeader("Refresh","5; URL=http://www.itcast.cn"):5秒后自動跳轉到傳智主頁。
4 設置狀態碼及其他方法
l response.setContentType("text/html;charset=utf-8"):等同與調用response.setHeader(“content-type”, “text/html;charset=utf-8”);
l response.setCharacterEncoding(“utf-8”):設置字符響應流的字符編碼為utf-8;
l response.setStatus(200):設置狀態碼;
l response.sendError(404, “您要查找的資源不存在”):當發送錯誤狀態碼時,Tomcat會跳轉到固定的錯誤頁面去,但可以顯示錯誤信息。
5 重定向
5.1 什么是重定向
當你訪問http://www.sun.com時,你會發現瀏覽器地址欄中的URL會變成http://www.oracle.com/us/sun/index.htm,這就是重定向了。
重定向是服務器通知瀏覽器去訪問另一個地址,即再發出另一個請求。
5.2 完成重定向
響應碼為200表示響應成功,而響應碼為302表示重定向。所以完成重定向的第一步就是設置響應碼為302。
因為重定向是通知瀏覽器再第二個請求,所以瀏覽器需要知道第二個請求的URL,所以完成重定向的第二步是設置Location頭,指定第二個請求的URL地址。
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(302);[崔1] response.setHeader("Location", "http://www.itcast.cn");[崔2] } } |
上面代碼的作用是:當訪問AServlet后,會通知瀏覽器重定向到傳智主頁。客戶端瀏覽器解析到響應碼為302后,就知道服務器讓它重定向,所以它會馬上獲取響應頭Location,然發出第二個請求。
5.3 便捷的重定向方式
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("http://www.itcast.cn"); } } |
response.sendRedirect()方法會設置響應頭為302,以設置Location響應頭。
如果要重定向的URL是在同一個服務器內,那么可以使用相對路徑,例如:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("/hello/BServlet"); } } |
重定向的URL地址為:http://localhost:8080/hello/BServlet。
5.4 重定向小結
l 重定向是兩次請求;
l 重定向的URL可以是其他應用,不局限於當前應用;
l 重定向的響應頭為302,並且必須要有Location響應頭;
l 重定向就不要再使用response.getWriter()或response.getOutputStream()輸出數據,不然可能會出現異常;
request
1 request概述
request是Servlet.service()方法的一個參數,類型為javax.servlet.http.HttpServletRequest。在客戶端發出每個請求時,服務器都會創建一個request對象,並把請求數據封裝到request中,然后在調用Servlet.service()方法時傳遞給service()方法,這說明在service()方法中可以通過request對象來獲取請求數據。
request的功能可以分為以下幾種:
l 封裝了請求頭數據;
l 封裝了請求正文數據,如果是GET請求,那么就沒有正文;
l request是一個域對象,可以把它當成Map來添加獲取數據;
l request提供了請求轉發和請求包含功能。
2 request域方法
request是域對象!在JavaWeb中一共四個域對象,其中ServletContext就是域對象,它在整個應用中只創建一個ServletContext對象。request其中一個,request可以在一個請求中共享數據。
一個請求會創建一個request對象,如果在一個請求中經歷了多個Servlet,那么多個Servlet就可以使用request來共享數據。現在我們還不知道如何在一個請求中經歷之個Servlet,后面在學習請求轉發和請求包含后就知道了。
下面是request的域方法:
l void setAttribute(String name, Object value):用來存儲一個對象,也可以稱之為存儲一個域屬性,例如:servletContext.setAttribute(“xxx”, “XXX”),在request中保存了一個域屬性,域屬性名稱為xxx,域屬性的值為XXX。請注意,如果多次調用該方法,並且使用相同的name,那么會覆蓋上一次的值,這一特性與Map相同;
l Object getAttribute(String name):用來獲取request中的數據,當前在獲取之前需要先去存儲才行,例如:String value = (String)request.getAttribute(“xxx”);,獲取名為xxx的域屬性;
l void removeAttribute(String name):用來移除request中的域屬性,如果參數name指定的域屬性不存在,那么本方法什么都不做;
l Enumeration getAttributeNames():獲取所有域屬性的名稱;
3 request獲取請求頭數據
request與請求頭相關的方法有:
l String getHeader(String name):獲取指定名稱的請求頭;
l Enumeration getHeaderNames():獲取所有請求頭名稱;
l int getIntHeader(String name):獲取值為int類型的請求頭。
4 request獲取請求相關的其它方法
request中還提供了與請求相關的其他方法,有些方法是為了我們更加便捷的方法請求頭數據而設計,有些是與請求URL相關的方法。
l int getContentLength():獲取請求體的字節數,GET請求沒有請求體,沒有請求體返回-1;
l String getContentType():獲取請求類型,如果請求是GET,那么這個方法返回null;如果是POST請求,那么默認為application/x-www-form-urlencoded,表示請求體內容使用了URL編碼;
l String getMethod():返回請求方法,例如:GET
l Locale getLocale():返回當前客戶端瀏覽器的Locale。java.util.Locale表示國家和言語,這個東西在國際化中很有用;
l String getCharacterEncoding():獲取請求體編碼,如果沒有setCharacterEncoding(),那么返回null,表示使用ISO-8859-1編碼;
l void setCharacterEncoding(String code):設置請求編碼,只對請求體有效!注意,對於GET而言,沒有請求體!!!所以此方法只能對POST請求中的參數有效!
l String getContextPath():返回上下文路徑,例如:/hello
l String getQueryString():返回請求URL中的參數,例如:name=zhangSan
l String getRequestURI():返回請求URI路徑,例如:/hello/oneServlet
l StringBuffer getRequestURL():返回請求URL路徑,例如:http://localhost/hello/oneServlet,即返回除了參數以外的路徑信息;
l String getServletPath():返回Servlet路徑,例如:/oneServlet
l String getRemoteAddr():返回當前客戶端的IP地址;
l String getRemoteHost():返回當前客戶端的主機名,但這個方法的實現還是獲取IP地址;
l String getScheme():返回請求協議,例如:http;
l String getServerName():返回主機名,例如:localhost
l int getServerPort():返回服務器端口號,例如:8080
System.out.println("request.getContentLength(): " + request.getContentLength()); System.out.println("request.getContentType(): " + request.getContentType()); System.out.println("request.getContextPath(): " + request.getContextPath()); System.out.println("request.getMethod(): " + request.getMethod()); System.out.println("request.getLocale(): " + request.getLocale());
System.out.println("request.getQueryString(): " + request.getQueryString()); System.out.println("request.getRequestURI(): " + request.getRequestURI()); System.out.println("request.getRequestURL(): " + request.getRequestURL()); System.out.println("request.getServletPath(): " + request.getServletPath()); System.out.println("request.getRemoteAddr(): " + request.getRemoteAddr()); System.out.println("request.getRemoteHost(): " + request.getRemoteHost()); System.out.println("request.getRemotePort(): " + request.getRemotePort()); System.out.println("request.getScheme(): " + request.getScheme()); System.out.println("request.getServerName(): " + request.getServerName()); System.out.println("request.getServerPort(): " + request.getServerPort()); |
4.1 案例:request.getRemoteAddr():封IP
可以使用request.getRemoteAddr()方法獲取客戶端的IP地址,然后判斷IP是否為禁用IP。
String ip = request.getRemoteAddr(); System.out.println(ip); if(ip.equals("127.0.0.1")) { response. getWriter().print("您的IP已被禁止!"); } else { response.getWriter().print("Hello!"); } |
5 request獲取請求參數
最為常見的客戶端傳遞參數方式有兩種:
l 瀏覽器地址欄直接輸入:一定是GET請求;
l 超鏈接:一定是GET請求;
l 表單:可以是GET,也可以是POST,這取決與<form>的method屬性值;
GET請求和POST請求的區別:
l GET請求:
l 請求參數會在瀏覽器的地址欄中顯示,所以不安全;
l 請求參數長度限制長度在1K之內;
l GET請求沒有請求體,無法通過request.setCharacterEncoding()來設置參數的編碼;
l POST請求:
l 請求參數不會顯示瀏覽器的地址欄,相對安全;
l 請求參數長度沒有限制;
<a href="/hello/ParamServlet?p1=v1&p2=v2[崔3] ">超鏈接</a> <hr/> <form action="/hello/ParamServlet" method="post[崔4] "> 參數1:<input type="text" name="p1"/><br/> 參數2:<input type="text" name="p2"/><br/> <input type="submit" value="提交"/> </form> |
下面是使用request獲取請求參數的API:
l String getParameter(String name):通過指定名稱獲取參數值;
public void doGet[崔5] (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String v1 = request.getParameter("p1"); String v2 = request.getParameter("p2"); System.out.println("p1=" + v1); System.out.println("p2=" + v2); }
public void doPost[崔6] (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String v1 = request.getParameter("p1"); String v2 = request.getParameter("p2"); System.out.println("p1=" + v1); System.out.println("p2=" + v2); } |
l String[] getParameterValues(String name):當多個參數名稱相同時,可以使用方法來獲取;
<a href="/hello/ParamServlet?name=zhangSan&name=liSi[崔7] ">超鏈接</a> |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] names = request.getParameterValues("name");[崔8] System.out.println(Arrays.toString(names));[崔9] } |
l Enumeration getParameterNames():獲取所有參數的名字;
<form action="/hello/ParamServlet" method="post"> 參數1:<input type="text" name="p1"/><br/> 參數2:<input type="text" name="p2"/><br/> <input type="submit" value="提交"/> </form> |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Enumeration names = request.getParameterNames()[崔10] ; while(names.hasMoreElements()) { System.out.println(names.nextElement()); } } |
l Map getParameterMap():獲取所有參數封裝到Map中,其中key為參數名,value為參數值,因為一個參數名稱可能有多個值,所以參數值是String[],而不是String。
<a href="/day05_1/ParamServlet?p1=v1&p1=vv1&p2=v2&p2=vv2">超鏈接</a> |
Map<String,String[]> paramMap = request.getParameterMap(); for(String name : paramMap.keySet()) { String[] values = paramMap.get(name); System.out.println(name + ": " + Arrays.toString(values)); } |
p2: [v2, vv2] p1: [v1, vv1] |
6 請求轉發和請求包含
無論是請求轉發還是請求包含,都表示由多個Servlet共同來處理一個請求。例如Servlet1來處理請求,然后Servlet1又轉發給Servlet2來繼續處理這個請求。
6.1 請求轉發
在AServlet中,把請求轉發到BServlet:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("AServlet"); RequestDispatcher rd = request.getRequestDispatcher("/BServlet");[崔11] rd.forward(request, response)[崔12] ; } } |
public class BServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("BServlet"); } } |
Aservlet BServlet |
6.2 請求包含
在AServlet中,把請求包含到BServlet:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("AServlet"); RequestDispatcher rd = request.getRequestDispatcher("/BServlet"); rd.include[崔13] (request, response); } } |
public class BServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("BServlet"); } } |
Aservlet BServlet |
6.3 請求轉發與請求包含比較
l 如果在AServlet中請求轉發到BServlet,那么在AServlet中就不允許再輸出響應體,即不能再使用response.getWriter()和response.getOutputStream()向客戶端輸出,這一工作應該由BServlet來完成;如果是使用請求包含,那么沒有這個限制;
l 請求轉發雖然不能輸出響應體,但還是可以設置響應頭的,例如:response.setContentType(”text/html;charset=utf-8”);
l 請求包含大多是應用在JSP頁面中,完成多頁面的合並;
l 請求請求大多是應用在Servlet中,轉發目標大多是JSP頁面;
6.4 請求轉發與重定向比較
l 請求轉發是一個請求,而重定向是兩個請求;
l 請求轉發后瀏覽器地址欄不會有變化,而重定向會有變化,因為重定向是兩個請求;
l 請求轉發的目標只能是本應用中的資源,重定向的目標可以是其他應用;
l 請求轉發對AServlet和BServlet的請求方法是相同的,即要么都是GET,要么都是POST,因為請求轉發是一個請求;
l 重定向的第二個請求一定是GET;
路徑
1 與路徑相關的操作
l 超鏈接
l 表單
l 轉發
l 包含
l 重定向
l <url-pattern>
l ServletContext獲取資源
l Class獲取資源
l ClassLoader獲取資源
2 客戶端路徑
超鏈接、表單、重定向都是客戶端路徑,客戶端路徑可以分為三種方式:
l 絕對路徑;
l 以“/”開頭的相對路徑;
l 不以“/”開頭的相對路徑;
例如:http://localhost:8080/hello1/pages/a.html中的超鏈接和表單如下:
絕對路徑:<a href="http://localhost:8080/hello2/index.html">鏈接1</a> 客戶端路徑:<a href="/hello3/pages/index.html">鏈接2</a> 相對路徑:<a href="index.html">鏈接3</a> <hr/> 絕對路徑: <form action="http://localhost:8080/hello2/index.html"> <input type="submit" value="表單1"/> </form> 客戶端路徑: <form action="/hello2/index.html"> <input type="submit" value="表單2"/> </form> 相對路徑: <form action="index.html"> <input type="submit" value="表單3"/> </form> |
l 鏈接1和表單1:沒什么可說的,它使用絕對路徑;
l 鏈接2和表單2:以“/”開頭,相對主機,與當前a.html的主機相同,即最終訪問的頁面為http://localhost:8080/hello2/index.html;
l 鏈接3和表單3:不以“/”開頭,相對當前頁面的路徑,即a.html所有路徑,即最終訪問的路徑為:http://localhost:8080/hello1/pages/index.html;
重定向1:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("/hello/index.html"); } } |
假設訪問AServlet的路徑為:http://localhost:8080/hello/servlet/AServlet
因為路徑以“/”開頭,所以相對當前主機,即http://localhost:8080/hello/index.html。
重定向2:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("index.html"); } } |
假設訪問AServlet的路徑為:http://localhost:8080/hello/servlet/AServlet
因為路徑不以“/”開頭,所以相對當前路徑,即http://localhost:8080/hello/servlet/index.html
2.1 建議使用“/”
強烈建議使用“/”開頭的路徑,這說明在頁面中的超鏈接和表單都要以“/”開頭,后面是當前應用的名稱,再是訪問路徑:
<form action="/hello/servlet/AServlet">
</form>
<a href="/hello/b.html">鏈接</a>
其中/hello是當前應用名稱,這也說明如果將來修改了應用名稱,那么頁面中的所有路徑也要修改,這一點確實是個問題。這一問題的處理方案會在學習了JSP之后講解!
在Servlet中的重定向也建議使用“/”開頭。同理,也要給出應用的名稱!例如:
response.sendRedirect("/hello/BServlet"); |
其中/hello是當前應用名,如果將來修改了應用名稱,那么也要修改所有重定向的路徑,這一問題的處理方案是使用request.getContextPath()來獲取應用名稱。
response.sendRedirect(request.getContextPath() + "/BServlet"); |
3 服務器端路徑
服務器端路徑必須是相對路徑,不能是絕對路徑。但相對路徑有兩種形式:
l 以“/”開頭;
l 不以“/”開頭;
其中請求轉發、請求包含都是服務器端路徑,服務器端路徑與客戶端路徑的區別是:
l 客戶端路徑以“/”開頭:相對當前主機;
l 服務器端路徑以“/”開頭:相對當前應用;
轉發1:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/BServlet").forward(request, response); } } |
假設訪問AServlet的路徑為:http://localhost:8080/hello/servlet/AServlet
因為路徑以“/”開頭,所以相對當前應用,即http://localhost:8080/hello/BServlet。
轉發2:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("BServlet").forward(request, response); } } |
假設訪問AServlet的路徑為:http://localhost:8080/hello/servlet/AServlet
因為路徑不以“/”開頭,所以相對當前應用,即http://localhost:8080/hello/servlet/BServlet。
4 <url-pattern>路徑
<url-pattern>必須使用“/”開頭,並且相對的是當前應用。
5 ServletContext獲取資源
必須是相對路徑,可以“/”開頭,也可以不使用“/”開頭,但無論是否使用“/”開頭都是相對當前應用路徑。
例如在AServlet中獲取資源,AServlet的路徑路徑為:http://localhost:8080/hello/servlet/AServlet:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path1 = this.getServletContext().getRealPath("a.txt"); String path2 = this.getServletContext().getRealPath("/a.txt"); System.out.println(path1); System.out.println(path2); } } |
path1和path2是相同的結果:http://localhost:8080/hello/a.txt
6 Class獲取資源
Class獲取資源也必須是相對路徑,可以“/”開頭,也可以不使用“/”開頭。
package cn.itcast;
import java.io.InputStream;
public class Demo { public void fun1() { InputStream in = Demo.class.getResourceAsStream("/a.txt"); }
public void fun2() { InputStream in = Demo.class.getResourceAsStream("a.txt"); } } |
其中fun1()方法獲取資源時以“/”開頭,那么相對的是當前類路徑,即/hello/WEB-INF/classes/a.txt文件;
其中fun2()方法獲取資源時沒有以“/”開頭,那么相對當前Demo.class所在路徑,因為Demo類在cn.itcast包下,所以資源路徑為:/hello/WEB-INF/classes/cn/itcast/a.txt。
7 ClassLoader獲取資源
ClassLoader獲取資源也必須是相對路徑,可以“/”開頭,也可以不使用“/”開頭。但無論是否以“/”開頭,資源都是相對當前類路徑。
public class Demo { public void fun1() { InputStream in = Demo.class.getClassLoader().getResourceAsStream("/a.txt"); }
public void fun2() { InputStream in = Demo.class.getClassLoader().getResourceAsStream("a.txt"); } } |
fun1()和fun2()方法的資源都是相對類路徑,即classes目錄,即/hello/WEB-INF/classes/a.txt
編碼
1 請求編碼
1.1 直接在地址欄中給出中文
請求數據是由客戶端瀏覽器發送服務器的,請求數據的編碼是由瀏覽器決定的。例如在瀏覽器地址欄中給出:http://localhost:8080/hello/AServlet?name=傳智,那么其中“傳智”是什么編碼的呢?不同瀏覽器使用不同的編碼,所以這是不確定的!
l IE:使用GB2312;
l FireFox:使用GB2312;
l Chrome:使用UTF-8;
通常沒有哪個應用要求用戶在瀏覽器地址欄中輸入請求數據的,所以大家只需了解一下即可。
1.2 在頁面中發出請求
通常向服務器發送請求數據都需要先請求一個頁面,然后用戶在頁面中輸入數據。頁面中有超鏈接和表單,通過超鏈接和表單就可以向服務器發送數據了。
因為頁面是服務器發送到客戶端瀏覽器的,所以這個頁面本身的編碼由服務器決定。而用戶在頁面中輸入的數據也是由頁面本身的編碼決定的。
index.html
<!DOCTYPE html> <html> <head> <title>index.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8">[崔14] </head>
<body> <form action="/hello/servlet/AServlet"> 名稱:<input type="text" name="name"/> <input type="submit" value="提交"/> </form> <a href="/hello/servlet/AServlet?name=傳智">鏈接</a> </body> </html> |
當用戶在index.html頁面中輸入數據時,都是UTF-8列表的。因為這個頁面本身就是UTF-8編碼的!
頁面的編譯就是頁面中輸入數據的編碼。
1.3 GET請求解讀編碼
當客戶端通過GET請求發送數據給服務器時,使用request.getParameter()獲取的數據是被服務器誤認為ISO-8859-1編碼的,也就是說客戶端發送過來的數據無論是UTF-8還是GBK,服務器都認為是ISO-8859-1,這就說明我們需要在使用request.getParameter()獲取數據后,再轉發成正確的編碼。
例如客戶端以UTF-8發送的數據,使用如下轉碼方式:
String name = request.getParameter(“name”);
name = new String(name.getBytes(“iso-8859-1”), “utf-8”);
1.4 POST請求解讀編碼
當客戶端通過POST請求發送數據給服務器時,可以在使用request.getParameter()獲取請求參數之前先通過request.setCharacterEncoding()來指定編碼,然后再使用reuqest.getParameter()方法來獲取請求參數,那么就是用指定的編碼來讀取了。
也就是說,如果是POST請求,服務器可以指定編碼!但如果沒有指定編碼,那么默認還是使用ISO-8859-1來解讀。
request.setCharacterEncoding(“utf-8”);
String name = request.getParameter(“name”);
2 響應編碼
響應:服務器發送給客戶端數據!響應是由response對象來完成,如果響應的數據不是字符數據,那么就無需去考慮編碼問題。當然,如果響應的數據是字符數據,那么就一定要考慮編碼的問題了。
response.getWriter().print(“傳智”);
上面代碼因為沒有設置repsonse.getWriter()字符流的編碼,所以服務器使用默認的編碼(ISO-8859-1)來處理,因為ISO-8859-1不支持中文,所以一定會出現編碼的。
所以在使用response.getWriter()發送數據之前,一定要設置response.getWriter()的編碼,這需要使用response.setCharacterEncoding()方法:
response.setCharacterEncoding(“utf-8”);
response.getWriter().print(“傳智”);
上面代碼因為在使用response.getWriter()輸出之前已經設置了編碼,所以輸出的數據為utf-8編碼。但是,因為沒有告訴瀏覽器使用什么編碼來讀取響應數據,所以很可能瀏覽器會出現錯誤的解讀,那么還是會出現亂碼的。當然,通常瀏覽器都支持來設置當前頁面的編碼,如果用戶在看到編碼時,去設置瀏覽器的編碼,如果設置的正確那么亂碼就會消失。但是我們不能讓用戶總去自己設置編碼,而且應該直接通知瀏覽器,服務器發送過來的數據是什么編碼,這樣瀏覽器就直接使用服務器告訴他的編碼來解讀!這需要使用content-type響應頭。
response.setContentType(“text/html;charset=utf-8”);
response.getWriter().print(“傳智”);
上面代碼使用setContentType()方法設置了響應頭content-type編碼為utf-8,這不只是在響應中添加了響應頭,還等於調用了一次response.setCharacterEncoding(“utf-8”),也就是說,通過我們只需要調用一次response.setContentType(“text/html;charset=utf-8”)即可,而無需再去調用response.setCharacterEncoding(“utf-8”)了。
在靜態頁面中,使用<meta>來設置content-type響應頭,例如:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
3 URL編碼
通過頁面傳輸數據給服務器時,如果包含了一些特殊字符是無法發送的。這時就需要先把要發送的數據轉換成URL編碼格式,再發送給服務器。
其實需要我們自己動手給數據轉換成URL編碼的只有GET超鏈接,因為表單發送數據會默認使用URL編碼,也就是說,不用我們自己來編碼。
例如:“傳智”這兩個字通過URL編碼后得到的是:“%E4%BC%A0%E6%99%BA”。URL編碼是先需要把“傳智”轉換成字節,例如我們現在使用UTF-8把“傳智”轉換成字符,得到的結果是:“[-28, -68, -96, -26, -103, -70]”,然后再把所有負數加上256,得到[228, 188, 160, 230, 153, 186],再把每個int值轉換成16進制,得到[E4, BC, A0, E6, 99, BA],最后再每個16進制的整數前面加上“%”。
通過URL編碼,把“傳智”轉換成了“%E4%BC%A0%E6%99%BA”,然后發送給服務器!服務器會自動識別出數據是使用URL編碼過的,然后會自動把數據轉換回來。
當然,在頁面中我們不需要自己去通過上面的過程把“傳智”轉換成“%E4%BC%A0%E6%99%BA”,而是使用Javascript來完成即可。當后面我們學習了JSP后,就不用再使用Javascript了。
<script type="text/javascript"> function _go() { location = "/day05_2/AServlet?name=" + encodeURIComponent("傳智+播客"); } </script> |
<a href="javascript:_go();">鏈接</a> |
因為URL默認只支持ISO-8859-1,這說明在URL中出現中文和一些特殊字符可能無法發送到服務器。所以我們需要對包含中文或特殊字符的URL進行URL編碼。
服務器會自動識別數據是否使用了URL編碼,如果使用了服務器會自動把數據解碼,無需我們自己動手解碼。
String s = “傳智”;
s = URLEncoder.encode(s, “utf-8”);// %E4%BC%A0%E6%99%BA
s = URLDecoderdecode(s, “utf-8”);//傳智
設置響應碼為302,表示重定向
設置新請求的URL
hello是應用名,ParamServlet是Servlet綁定URL路徑,在問號后面的是請求參數,第一個參數是p1,值為v1,第二個參數為p2,值為v2
請求方法是post
點擊超鏈接是GET請求,所以會執行doGet()方法
提交表單是POST請求,所以會調用doPost()方法
多個名為name的參數
獲取所有名為name的參數值
打印數組,輸出結果為:[zhangSan, liSi]
獲取所有參數名稱,輸出結果為:p1和p2
獲取“調度器”,其中參數為BServlet綁定的URL,即BServlet的<url-pattern>值。
調用“調度器”的轉發方法,該方法等同與告訴服務器,去調用BServlet的service()方法一樣。
請求包含
這就相當於給客戶端發送了響應頭content-type,指定當前頁面的編碼為utf-8