HttpServletRequest 和 HttpServletResponse
Servlet配置方式
-
- 全路徑匹配
以 / 開始 /a /aa/bb
localhost:8080/項目名稱/aa/bb
-
- 路徑匹配 , 前半段匹配
以 / 開始 , 但是以 * 結束 /a/* /*
- 其實是一個通配符,匹配任意文字
localhost:8080/項目名稱/aa/bb
-
- 以擴展名匹配
寫法: 沒有/ 以 * 開始 *.擴展名 *.aa *.bb
ServletContext
Servlet 上下文
每個web工程都只有一個ServletContext對象。 說白了也就是不管在哪個servlet裏面,獲取到的這個類的對象都是同一個。
如何得到對象
//1. 獲取對象
ServletContext context = getServletContext();
有什麼作用
- 獲取全局配置參數
- 獲取web工程中的資源
- 存取數據,servlet間共享數據 域對象
可以獲取全局配置參數
獲取全局參數
可以獲取Web應用中的資源
1. 獲取資源在tomcat裏面的絕對路徑
先得到路徑,然後自己new InpuStream
context.getRealPath("") //這裏得到的是項目在tomcat裏面的根目錄。
D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\
String path = context.getRealPath("file/config.properties");
D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\file\config.properties
2. getResourceAsStream 獲取資源 流對象
直接給相對的路徑,然後獲取流對象。
通過classloader去獲取web工程下的資源
使用ServletContext存取數據。
- 定義一個登陸的html頁面, 定義一個form表單
- 定義一個Servlet,名爲LoginServlet
- 針對成功或者失敗,進行判斷,然後跳轉到不一樣的網頁
ServletContext存取值分析
細節:
<!--
A路徑: Servlet的路徑
http://localhost:8080/Demo4/login
B路徑: 當前這個html的路徑:
http://localhost:8080/Demo4/login.html -->
<form action="login" method="get">
賬號:<input type="text" name="username"/><br>
密碼:<input type="text" name="password"/><br>
<input type="submit" value="登錄"/>
</form>
ServletContext 何時創建, 何時銷燬?
服務器啓動的時候,會爲託管的每一個web應用程序,創建一個ServletContext對象
從服務器移除託管,或者是關閉服務器。
- ServletContext 的作用範圍
只要在這個項目裏面,都可以取。 只要同一個項目。 A項目 存, 在B項目取,是取不到的? ServletContext對象不同。
HttpServletRequest
這個對象封裝了客戶端提交過來的一切數據。
-
可以獲取客戶端請求頭信息
//得到一個枚舉集合 Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = (String) headerNames.nextElement(); String value = request.getHeader(name); System.out.println(name+"="+value); }
-
獲取客戶端提交過來的數據
String name = request.getParameter("name"); String address = request.getParameter("address"); System.out.println("name="+name); System.out.println("address="+address); ------------------------------------------------- //name=zhangsan&name=lisi&name=wangwu 一個key可以對應多個值。 Map<String, String[]> map = request.getParameterMap(); Set<String> keySet = map.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = (String) iterator.next(); System.out.println("key="+key + "--的值總數有:"+map.get(key).length); String value = map.get(key)[0]; String value1 = map.get(key)[1]; String value2 = map.get(key)[2]; System.out.println(key+" ======= "+ value + "=" + value1 + "="+ value2); }
-
獲取中文數據
客戶端提交數據給服務器端,如果數據中帶有中文的話,有可能會出現亂碼情況,那麼可以參照以下方法解決。
-
如果是GET方式
-
代碼轉碼
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);System.out.println("userName="+username+"==password="+password); //get請求過來的數據,在url地址欄上就已經經過編碼了,所以我們取到的就是亂碼, //tomcat收到了這批數據,getParameter 默認使用ISO-8859-1去解碼 //先讓文字回到ISO-8859-1對應的字節數組 , 然後再按utf-8組拼字符串 username = new String(username.getBytes("ISO-8859-1") , "UTF-8"); System.out.println("userName="+username+"==password="+password); 直接在tomcat裏面做配置,以後get請求過來的數據永遠都是用UTF-8編碼。
-
可以在tomcat裏面做設置處理 conf/server.xml 加上URIEncoding=“utf-8”
-
-
如果是POST方式
這個說的是設置請求體裏面的文字編碼。 get方式,用這行,有用嗎? ---> 沒用 request.setCharacterEncoding("UTF-8"); 這行設置一定要寫在getParameter之前。
HttpServletResponse
負責返回數據給客戶端。
-
輸出數據到頁面上
//以字符流的方式寫數據 //response.getWriter().write("<h1>hello response...</h1>"); //以字節流的方式寫數據 response.getOutputStream().write("hello response2222...".getBytes());
響應的數據中有中文,那麼有可能出現中文亂碼
- 以字符流輸出
response.getWriter()
//1. 指定輸出到客戶端的時候,這些文字使用UTF-8編碼
response.setCharacterEncoding("UTF-8");
//2. 直接規定瀏覽器看這份數據的時候,使用什麼編碼來看。
response.setHeader("Content-Type", "text/html; charset=UTF-8");
response.getWriter().write("我愛黑馬訓練營...");
- 以字節流輸出
response.getOutputStream()
//1. 指定瀏覽器看這份數據使用的碼錶
response.setHeader("Content-Type", "text/html;charset=UTF-8");
//2. 指定輸出的中文用的碼錶
response.getOutputStream().write("我愛深圳黑馬訓練營..".getBytes("UTF-8"));
--------------------------------------------
不管是字節流還是字符流,直接使用一行代碼就可以了。
response.setContentType("text/html;charset=UTF-8");
然後再寫數據即可。
演練下載資源。
- 直接以超鏈接的方式下載,不寫任何代碼。 也能夠下載東西下來。
讓tomcat的默認servlet去提供下載:<br>
<a href="download/aa.jpg">aa.jpg</a><br>
<a href="download/bb.txt">bb.txt</a><br>
<a href="download/cc.rar">cc.rar</a><br>
原因是tomcat裏面有一個默認的Servlet – DefaultServlet 。這個DefaultServlet 專門用於處理放在tomcat服務器上的靜態資源。
- 手動編碼下載
讓tomcat的默認servlet去提供下載:<br>
<a href="Demo01?filename=aa.jpg">aa.jpg</a><br>
<a href="Demo01?filename=bb.txt">bb.txt</a><br>
<a href="Demo01?filename=cc.rar">cc.rar</a><br>
總結
-
Servlet註冊方式
-
ServletContext【重點】
作用: 1. 獲取全局參數 2. 獲取工程裏面的資源。 3. 資源共享。 ServletContext 域對象 有幾個 一個 什麼時候創建 ? 什麼時候銷燬 服務器啓動的時候給每一個應用都創建一個ServletContext對象, 服務器關閉的時候銷燬
簡單登錄
-
HttpServletRequest【重點】
1. 獲取請求頭 2. 獲取提交過來的數據
-
HttpServletResponse【重點】
負責輸出數據到客戶端,其實就是對之前的請求作出響應
-
中文亂碼問題。【重點】
-
下載
作業:
1. 完成註冊
2. 完成登錄
V1.1 最好配合上數據庫,完成註冊和登錄的功能。