Web階段:第十章:Servlet下

HttpServletRequest類
a)HttpServletRequest類有什么作用。
HttpServletRequest類它的作用是。每次只要有請求進來 。Tomcat服務器就會把請求的協(xié)議內容封裝到HttpServletRequest對象中。
我們主要是從Request類中獲取請求的信息。它表示了請求的全部信息。

b)HttpServletRequest類的常用方法
getRequestURI() 獲取請求請求資源地址
getRequestURL() 獲取請求的絕對路徑(又叫統(tǒng)一資源定位符)
getRemoteHost() 獲取客戶端的ip地址
localhost做為ip訪問得到0:0:0:0:0:0:0:1
127.0.0.1 做為ip訪問得到 127.0.0.1
getHeader() 獲取請求頭
getParameter() 獲取請求的參數(shù)
getParameterValues() 獲取請求參數(shù)(多個值)
getMethod() 獲取請求的方式GET或POST
域對象
setAttribute(key, value); 保存數(shù)據(jù)
getAttribute(key); 獲取數(shù)據(jù)

getRequestDispatcher() 獲取請求轉發(fā)對象
getCookies() 獲取Cookie對象
getSession() 獲取Session對象

reqeust常用API:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // i.getRequestURI() 獲取請求請求資源地址
        System.out.println(request.getRequestURI());
        // ii.getRequestURL() 獲取請求的絕對路徑
        System.out.println(request.getRequestURL());
        // iii.getRemoteHost() 獲取客戶端的ip地址
        System.out.println(request.getRemoteHost());
        // iv.getHeader() 獲取請求頭
        System.out.println(request.getHeader("User-Agent"));
        System.out.println(request.getHeader("Host"));
        // vii.getMethod() 獲取請求的方式GET或POST
        System.out.println(request.getMethod());
}

c)如何獲取請求參數(shù)(重要)
頁面的表單:

   

  1. <body>
  2. <form action="http://localhost:8080/day07/parameterServlet" method="get">
  3. 用戶名:<input type="text" name="username" /><br/>
  4. 密碼:<input type="password" name="password" /><br/>
  5. 興趣愛好:
  6. <input type="checkbox" name="hobby" value="cpp">C++
  7. <input type="checkbox" name="hobby" value="C">C
  8. <input type="checkbox" name="hobby" value="VB">Visual Basic
  9. <input type="checkbox" name="hobby" value="js">JavaScript
  10. <br/>
  11. <input type="submit" />
  12. </form>
  13. </body>

 

Servlet程序的代碼:

    public class ParameterServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            // 獲取請求參數(shù),用戶名信息
            String username = request.getParameter("username");
            // 獲取請求參數(shù),密碼信息
            String password = request.getParameter("password");
            // 獲取興趣愛好
            String[] hobbies = request.getParameterValues("hobby");
            System.out.println("用戶名:" + username);
            System.out.println("密碼:" + password);
            // Arrays.asList 把數(shù)組轉換對應的List集合
            if (hobbies != null) {
                System.out.println("興趣愛好:" + Arrays.asList(hobbies));
            }
        }
    }

   

d)GET請求的中文亂碼解決
第一種方案:

1、先把收到的請求參數(shù)以iso-8859-1進行編碼
2、再以UTF-8進行解碼

// 獲取請求參數(shù),用戶名信息
        String username = request.getParameter("username");        
//        1、先把收到的請求參數(shù)以iso-8859-1進行編碼
        byte[] bytes = username.getBytes("iso-8859-1");
//        2、再以UTF-8進行解碼
    username= new String(bytes, "UTF-8");

第二種方案:
找到Tomcat服務器的配置文件server.xml配置文件,找到Connector標簽,添加URIEncoding=“UTF-8”

 <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

    1

e)POST請求的中文亂碼解決
第一種方案(不推薦使用):

//        1、先把收到的請求參數(shù)以iso-8859-1進行編碼
        byte[] bytes = username.getBytes("iso-8859-1");
//        2、再以UTF-8進行解碼
        username= new String(bytes, "UTF-8");

 

第二種方案(推薦使用):

// 解決post中文亂碼終極方案
// 一定要在調用getParameter方法之前才有效
request.setCharacterEncoding("UTF-8");

f)請求的轉發(fā)(在jsp的時候使用)

 


g)Base標簽的作用

 


h)Web中的相對路徑和絕對路徑
相對路徑:
./ 當前目錄
…/ 上一級目錄
資源名 相當于./資源名 ./可以省略
絕對路徑:
http://ip:port/工程名/資源名
在實際項目開發(fā)中,不允許簡單的使用相對路徑!??!
實際項目開發(fā)中,只能使用絕對路徑 或 base+相對

i)web中/斜杠的不同意義

斜杠/ 就是表示一個絕對路徑。
/ 在html頁面上,被瀏覽器解析得到是:http://ip:prot/
/ 在服務器代碼上。表示: http://ip:port/工程名/
在web.xml中配置url-pattern /hello ====>>>> http://ip:port/工程名/hello
請求轉發(fā)request.getRequestDispatcher("/a/b/c.html") 表示http://ip:port/工程名/a/b/c.html
servletContext.getRealPath(“/”); =====>>>>>> 表示到http://ip:prot/工程名/
服務器代碼中的特殊情況:
response.sendRedirect(“/”); =====>>>> 表示到端口號http://ip:prot/

6.HttpServletResponse類
a)HttpServletResponse類的作用
httpServletResponse類,表示響應。所有響應的http協(xié)議都可以通過HttpServletResponse類去進行設置。
每次請求進來都會創(chuàng)建一個Request對象,也會創(chuàng)建一個Response對象。Tomcat服務器負責創(chuàng)建。
b)兩個輸出流的說明。
響應有兩種流,一種是字符流,一種是字節(jié)流。
response.getOutputStream() 字節(jié)流 文件下載使用
response.getWriter() 字符流 回傳字符串信息(使用頻率最高)
你使用了字節(jié)流,就不能使用字符流,
你使用字符流,就不能使用字節(jié)流。
兩個流同時使用就會出現(xiàn)如下異常:

 


c)如何往客戶端回傳數(shù)據(jù)

 protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            // 往客戶端回傳字符串數(shù)據(jù)
            // 1、先獲取字符流
            PrintWriter writer = response.getWriter();
            // 2、調用字符流的write方法輸出字符串
            writer.write("this is the content of response!!!");
    }

d)響應的亂碼解決
第一種方案(不推薦使用):

// 設置響應的字符集為UTF-8,設置了服務器支持中文輸出
        response.setCharacterEncoding("UTF-8");
        // ISO-8859-1 是默認的字符集,它不支持中文
        System.out.println( response.getCharacterEncoding() );
        // 設置響應頭,Content-Type,
        // text/html; 表示返回的是html內容。
        // charset=UTF-8    告訴客戶端使用UTF-8字符集查看
    response.setHeader("Content-Type", "text/html; charset=UTF-8");

第二種方案是(推薦使用):

 // 同時設置服務器和客戶端都使用UTF-8字符集,
        // 并且還設置了響應頭
        // 必須在獲取流對象之前調用
    response.setContentType("text/html; charset=UTF-8");

e)如何設置響應頭和響應狀態(tài)碼

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("請求進來 了,這是Response1");
        // 設置響應狀態(tài)碼302
        response.setStatus(302);
        // 設置響應頭
        response.setHeader("Location", "http://localhost:8080/day07/response2");
}

f)請求重定向

//也是請求重定向(推薦)
    response.sendRedirect("http://localhost:8080/day07/response2");

請求重定向的特點:

 


請求轉發(fā)和重定向的對比