day10

請求響應流程圖

response

  1. response概述

response是Servlet.service方法的一個引數,型別為javax.servlet.http.HttpServletResponse。在客戶端發出每個請求時,伺服器都會建立一個response物件,並傳入給Servlet.service()方法。response物件是用來對客戶端進行響應的,這說明在service()方法中使用response物件可以完成對客戶端的響應工作。

response物件的功能分為以下四種:

  • 設定響應頭資訊;
  • 傳送狀態碼;
  • 設定響應正文;
  • 重定向;
2 response響應正文

response是響應物件,向客戶端輸出響應正文(響應體)可以使用response的響應流,repsonse一共提供了兩個響應流物件:

  • PrintWriter out = response.getWriter():獲取字元流;
  • ServletOutputStream out = response.getOutputStream():獲取位元組流;

 

當然,如果響應正文內容為字元,那麼使用response.getWriter(),如果響應內容是位元組,例如下載時,那麼可以使用response.getOutputStream()。

注意,在一個請求中,不能同時使用這兩個流!也就是說,要麼你使用repsonse.getWriter(),要麼使用response.getOutputStream(),但不能同時使用這兩個流。不然會丟擲IllegalStateException異常。

2.1 字元響應流

  • 字元編碼

在使用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頭來解讀響應資料。

  • 緩衝區

response.getWriter()是PrintWriter型別,所以它有緩衝區,緩衝區的預設大小為8KB。也就是說,在響應資料沒有輸出8KB之前,資料都是存放在緩衝區中,而不會立刻傳送到客戶端。當Servlet執行結束後,伺服器才會去重新整理流,使緩衝區中的資料傳送到客戶端。

如果希望響應資料馬上傳送給客戶端:

  • 向流中寫入大於8KB的資料;
  • 呼叫response.flushBuffer()方法來手動重新整理緩衝區;
3 設定響應頭資訊

  可以使用response物件的setHeader()方法來設定響應頭!使用該方法設定的響應頭最終會發送給客戶端瀏覽器!

  • response.setHeader("content-type", "text/html;charset=utf-8"):設定content-type響應頭,該頭的作用是告訴瀏覽器響應內容為html型別,編碼為utf-8。而且同時會設定response的字元流編碼為utf-8,即response.setCharaceterEncoding("utf-8");
  • response.setHeader("Refresh","5; URL=http://www.itcast.cn"):5秒後自動跳轉到傳智主頁。
4 設定狀態碼及其他方法
  • response.setContentType("text/html;charset=utf-8"):等同與呼叫response.setHeader("content-type", "text/html;charset=utf-8");
  • response.setCharacterEncoding("utf-8"):設定字元響應流的字元編碼為utf-8;
  • response.setStatus(200):設定狀態碼;
  • 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);

        response.setHeader("Location", "http://www.itcast.cn");

    }

}

 

  上面程式碼的作用是:當訪問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 重定向小結

  • 重定向是兩次請求;
  • 重定向的URL可以是其他應用,不侷限於當前應用;
  • 重定向的響應頭為302,並且必須要有Location響應頭;
  • 重定向就不要再使用response.getWriter()或response.getOutputStream()輸出資料,不然可能會出現異常;

request

1 request概述

request是Servlet.service()方法的一個引數,型別為javax.servlet.http.HttpServletRequest。在客戶端發出每個請求時,伺服器都會建立一個request物件,並把請求資料封裝到request中,然後在呼叫Servlet.service()方法時傳遞給service()方法,這說明在service()方法中可以通過request物件來獲取請求資料。

request的功能可以分為以下幾種:

  • 封裝了請求頭資料;
  • 封裝了請求正文資料,如果是GET請求,那麼就沒有正文;
  • request是一個域物件,可以把它當成Map來新增獲取資料;
  • request提供了請求轉發和請求包含功能。
2 request域方法

request是域物件!在JavaWeb中一共四個域物件,其中ServletContext就是域物件,它在整個應用中只建立一個ServletContext物件。request其中一個,request可以在一個請求中共享資料。

一個請求會建立一個request物件,如果在一個請求中經歷了多個Servlet,那麼多個Servlet就可以使用request來共享資料。現在我們還不知道如何在一個請求中經歷多個Servlet,後面在學習請求轉發和請求包含後就知道了。

下面是request的域方法:

  • void setAttribute(String name, Object value):用來儲存一個物件,也可以稱之為儲存一個域屬性,例如:servletContext.setAttribute("xxx", "XXX"),在request中儲存了一個域屬性,域屬性名稱為xxx,域屬性的值為XXX。請注意,如果多次呼叫該方法,並且使用相同的name,那麼會覆蓋上一次的值,這一特性與Map相同;
  • Object getAttribute(String name):用來獲取request中的資料,當前在獲取之前需要先去儲存才行,例如:String value = (String)request.getAttribute("xxx");,獲取名為xxx的域屬性;
  • void removeAttribute(String name):用來移除request中的域屬性,如果引數name指定的域屬性不存在,那麼本方法什麼都不做;
  • Enumeration getAttributeNames():獲取所有域屬性的名稱;
3 request獲取請求頭資料

request與請求頭相關的方法有:

  • String getHeader(String name):獲取指定名稱的請求頭;
  • Enumeration getHeaderNames():獲取所有請求頭名稱;
  • int getIntHeader(String name):獲取值為int型別的請求頭。
4 request獲取請求相關的其它方法

request中還提供了與請求相關的其他方法,有些方法是為了我們更加便捷的方法請求頭資料而設計,有些是與請求URL相關的方法。

  • int getContentLength():獲取請求體的位元組數,GET請求沒有請求體,沒有請求體返回-1;
  • String getContentType():獲取請求型別,如果請求是GET,那麼這個方法返回null;如果是POST請求,那麼預設為application/x-www-form-urlencoded,表示請求體內容使用了URL編碼;
  • String getMethod():返回請求方法,例如:GET
  • Locale getLocale():返回當前客戶端瀏覽器的Locale。java.util.Locale表示國家和言語,這個東西在國際化中很有用;
  • String getCharacterEncoding():獲取請求編碼,如果沒有setCharacterEncoding(),那麼返回null,表示使用ISO-8859-1編碼;
  • void setCharacterEncoding(String code):設定請求編碼,只對請求體有效!注意,對於GET而言,沒有請求體!!!所以此方法只能對POST請求中的引數有效!
  • String getContextPath():返回上下文路徑,例如:/hello
  • String getQueryString():返回請求URL中的引數,例如:name=zhangSan
  • String getRequestURI():返回請求URI路徑,例如:/hello/oneServlet
  • StringBuffer getRequestURL():返回請求URL路徑,例如:http://localhost/hello/oneServlet,即返回除了引數以外的路徑資訊;
  • String getServletPath():返回Servlet路徑,例如:/oneServlet
  • String getRemoteAddr():返回當前客戶端的IP地址;
  • String getRemoteHost():返回當前客戶端的主機名,但這個方法的實現還是獲取IP地址;
  • String getScheme():返回請求協議,例如:http;
  • String getServerName():返回主機名,例如:localhost
  • 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獲取請求引數

最為常見的客戶端傳遞引數方式有兩種:

  • 瀏覽器位址列直接輸入:一定是GET請求;
  • 超連結:一定是GET請求;
  • 表單:可以是GET,也可以是POST,這取決與<form>的method屬性值;

GET請求和POST請求的區別:

  • GET請求:
    • 請求引數會在瀏覽器的位址列中顯示,所以不安全;
    • 請求引數長度限制長度在1K之內;
    • GET請求沒有請求體,無法通過request.setCharacterEncoding()來設定引數的編碼;
  • POST請求:
    • 請求引數不會顯示瀏覽器的位址列,相對安全;
    • 請求引數長度沒有限制;

<a
href="/hello/ParamServlet?p1=v1&p2=v2">超連結</a>

<hr/>

<form
action="/hello/ParamServlet"
method="post">

:<input
type="text"
name="p1"/><br/>

:<input
type="text"
name="p2"/><br/>

    <input
type="submit"
value="提交"/>

</form>

 

下面是使用request獲取請求引數的API:

  • String getParameter(String name):通過指定名稱獲取引數值;

 

    public
void doGet(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(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);        

    }

 

  • String[] getParameterValues(String name):當多個引數名稱相同時,可以使用方法來獲取;

<a
href="/hello/ParamServlet?name=zhangSan&name=liSi">超連結</a>

    public
void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String[] names = request.getParameterValues("name");

        System.out.println(Arrays.toString(names));

    }

 

  • Enumeration getParameterNames():獲取所有引數的名字;

<form
action="/hello/ParamServlet"
method="post">

:<input
type="text"
name="p1"/><br/>

:<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();

        while(names.hasMoreElements()) {

            System.out.println(names.nextElement());

        }

    } 

 

  • 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");

        rd.forward(request, response);

    }

}

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(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 請求轉發與請求包含比較

  • 如果在AServlet中請求轉發到BServlet,那麼在AServlet中就不允許再輸出響應體,即不能再使用response.getWriter()和response.getOutputStream()向客戶端輸出,這一工作應該由BServlet來完成;如果是使用請求包含,那麼沒有這個限制;
  • 請求轉發雖然不能輸出響應體,但還是可以設定響應頭的,例如:response.setContentType("text/html;charset=utf-8");
  • 請求包含大多是應用在JSP頁面中,完成多頁面的合併;
  • 請求請求大多是應用在Servlet中,轉發目標大多是JSP頁面;

6.4 請求轉發與重定向比較

  • 請求轉發是一個請求,而重定向是兩個請求;
  • 請求轉發後瀏覽器位址列不會有變化,而重定向會有變化,因為重定向是兩個請求;
  • 請求轉發的目標只能是本應用中的資源,重定向的目標可以是其他應用;
  • 請求轉發對AServlet和BServlet的請求方法是相同的,即要麼都是GET,要麼都是POST,因為請求轉發是一個請求;
  • 重定向的第二個請求一定是GET;

路徑

 

1 與路徑相關的操作
  • 超連結
  • 表單
  • 轉發
  • 包含
  • 重定向
  • <url-pattern>
  • ServletContext獲取資源
  • Class獲取資源
  • ClassLoader獲取資源
2 客戶端路徑

超連結、表單、重定向都是客戶端路徑,客戶端路徑可以分為三種方式:

  • 絕對路徑;
  • 以"/"開頭的相對路徑;
  • 不以"/"開頭的相對路徑;

例如: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>

 

  • 連結1和表單1:沒什麼可說的,它使用絕對路徑;
  • 連結2和表單2:以"/"開頭,相對主機,與當前a.html的主機相同,即最終訪問的頁面為http://localhost:8080/hello2/index.html;
  • 連結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 伺服器端路徑

伺服器端路徑必須是相對路徑,不能是絕對路徑。但相對路徑有兩種形式:

  • 以"/"開頭;
  • 不以"/"開頭;

 

其中請求轉發、請求包含都是伺服器端路徑,伺服器端路徑與客戶端路徑的區別是:

  • 客戶端路徑以"/"開頭:相對當前主機;
  • 伺服器端路徑以"/"開頭:相對當前應用;

 

轉發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=傳智,那麼其中"傳智"是什麼編碼的呢?不同瀏覽器使用不同的編碼,所以這是不確定的!

  • IE:使用GB2312;
  • FireFox:使用GB2312;
  • 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">

</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編碼,如果使用了伺服器會自動把資料解碼,無需我們自己動手解碼。