day10 Request&Response

service(request,response)工做原理:

Response:

響應行:響應的協議   響應的狀態碼  響應的描述java

響應頭瀏覽器

響應正文服務器

狀態碼,字節流,字符流:

注意:設置錯誤的要用:sendError()post

重定向:

亂碼:

驗證碼:


Request:

得到:

請求首行

請求頭

請求空行

請求正文:

request的亂碼問題:

public static void main(String[] args) throws Exception {
		String str = "帶門";
		byte[] b = str.getBytes("utf-8");//模擬瀏覽器進行編碼
		String name = new String(b,"iso-8859-1");//模擬服務器進行解碼
		
//		byte[] by = name.getBytes("iso-8859-1");//反服務器進行編碼
//		String str1 = new String(by,"utf-8");
		String str1 = new String(name.getBytes("iso-8859-1"),"utf-8");
		System.out.println(str1);

	}
post請求的亂碼解決方案: request.setCharacterEncoding("utf-8");
get請求的亂碼解決方案:
String name = request.getParameter("username");//根據用戶的name的屬性值獲得相應的提交信息
String name1 = new String(name.getBytes("iso-8859-1"),"utf8");
System.out.println(name1);

請求轉發:

request.getRequestDispatcher("/servlet6").forward(request, response); //地址沒變,網頁中顯示的內容是sevlet6的內容編碼

請求包含:

request.getRequestDispatcher("/servlet6").include(request, response); //地址沒變,網頁中顯示的是原來servlet中的內容和servlet6的內容。spa

Request域:

request.getAttribute("鍵");code

request.setAttribute("鍵","值");生命週期

request.removeAttribute("鍵");utf-8

request.getAttributeNames();rem

生命週期:

ServletContext (域) :同生共死

Servlet  :聲明週期   :不求同生,只求同死。

request:完整的一次請求響應。

相關文章
相關標籤/搜索