请求转发、包含、重定向 getAttribute 和 setAttribute POST和GET编码
?一、請求轉發? 請求包含? 請求重定向
Demo5.java??
注意:doPost()方法中別忘寫doGet(request, response);
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");String str = "aaaaa";System.out.println("A:我想辦事");System.out.println("B:我辦不了,但我可以找人幫你辦");//將非表單的數據添加到request的域中request.setAttribute("s", str);//將請求轉發到demo6中//request.getRequestDispatcher("/servlet/demo6").forward(request, response);//注:請求轉發不能跳轉到其它應用,只能在本web project下跳轉(例如下面的跳轉到百度是不行的)//request.getRequestDispatcher("http://www.baidu.com").forward(request, response);//使用重定向//response.sendRedirect(request.getContextPath()+"/servlet/demo6");//可以跳轉到其它應用//response.sendRedirect("http://www.baidu.com"); System.out.println("B:事辦完了");//請求包含request.getRequestDispatcher("/servlet/demo6").include(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}Demo6.java?
注意:doPost()方法中別忘寫doGet(request, response);
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");System.out.println("這個事我能辦");//將request對象中的移除//request.removeAttribute("s");//從request對象中獲取s的值String s = (String) request.getAttribute("s");System.out.println(s);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}瀏覽器輸入:
http://localhost:8080/day9/servlet/demo5
請求轉發和請求包含的區別:
(1)相同點:
??????都是多個Servlet之間共同處理一個請求,并且在請求之間公用一個request對象和response對象
(2)不同點:
??????如果是請求轉發的話,那么前者將不能向客戶端發出響應,這一工作就由后者來完成。
??????請求包含大多應用在jsp頁面中,完成多頁面的合并。
?
?
?
請求轉發就是Servlet將請求轉送給其他的Servlet或服務器資源
請求包含就是指多個Servlet共同處理一個請求。
?
?
?
請求轉發和請求重定向的區別:
轉發
(1)客戶端只發送一次請求
(2)地址欄不變
(3)servlet1和servlet2兩個應用程序之間能夠通過request對象共享數據
(4)不可以跳轉到其它項目的應用
重定向?
(1)客戶端發送兩次請求
(2)地址欄會發生變化
(3)servlet1和servlet2兩個應用程序之間不能通過request對象共享數據
(4)可以跳轉到其它應用
?
?
使用請求轉發,減少瀏覽器對服務器的訪問次數,減輕服務器的壓力。
如果明確的希望瀏覽器的地址欄發生變化則使用請求重定向,例如登錄頁面,登錄成功回到主頁的過程。
?
?
?
二、getAttribute 和?setAttribute
request.getAttribute表示從request范圍取得設置的屬性,必須要先setAttribute設置屬性,才能通過getAttribute來取得,設置與取得的為Object對象類型 。
?request.getParameter表示接收參數,參數為頁面提交的參數,包括:表單提交的參數、URL重寫(就是xxx?id=1中的id)傳的參數等,因此這個并沒有設置參數的方法(沒有setParameter),而且接收參數返回的不是Object,而是String類型。
?
?setAttribute的參數是String 和 Object ,
1.放的時候:Double res = new Double(result);//包裝
request.setAttribute("result", res);//再設置進去
2.取的時候:Double res = (Double)request.getAttribute("result");
double result = res.doublue();
另外,需要注意的是使用request.setAttribute時不能使redirect而是forward。即是將請求轉發而不是重定向
?
?
?
三、java中getAttribute和getParameter的區別?
getAttribute表示從request范圍取得設置的屬性,必須要先setAttribute設置屬性,才能通過getAttribute來取得,設置與取得的為Object對象類型?
getParameter表示接收參數,參數為頁面提交的參數,包括:表單提交的參數、URL重寫(就是xxx?id=1中的id)傳的參數等,因此這個并沒有設置參數的方法(沒有setParameter),而且接收參數返回的不是Object,而是String類型
?
HttpServletRequest類既有getAttribute()方法,也由getParameter()方法,這兩個方法有以下區別:
(1)HttpServletRequest類有setAttribute()方法,而沒有setParameter()方法
(2)當兩個Web組件之間為鏈接關系時,被鏈接的組件通過getParameter()方法來獲得請求參數,
?
?例如:假定welcome.jsp和authenticate.jsp之間為鏈接關系,welcome.jsp中有以下代碼:
<a href="authenticate.jsp?username=weiqin">authenticate.jsp </a>或者:<form name="form1" method="post" action="authenticate.jsp">請輸入用戶姓名:<input type="text" name="username"><input type="submit" name="Submit" value="提交"> </form>在authenticate.jsp中通過request.getParameter("username")方法來獲得請求參數username:
<% String username=request.getParameter("username"); %>?
(3)當兩個Web組件之間為轉發關系時,轉發目標組件通過getAttribute()方法來和轉發源組件共享request范圍內的數據。
?
例如:? authenticate.jsp和hello.jsp之間為轉發關系。authenticate.jsp希望向hello.jsp傳遞當前的用戶名字,? 如何傳遞這一數據呢?
?
先在authenticate.jsp中調用setAttribute()方法:
<% String username=request.getParameter("username"); request.setAttribute("username",username); %> <jsp:forward page="hello.jsp" />在hello.jsp中通過getAttribute()方法獲得用戶名字:
從更深的層次考慮,request.getParameter()方法傳遞的數據,會從Web客戶端傳到Web服務器端,代表HTTP請求數據。request.getParameter()方法返回String類型的數據。
request.setAttribute()和getAttribute()方法傳遞的數據只會存在于Web容器內部,在具有轉發關系的Web組件之間共享。這兩個方法能夠設置Object類型的共享數據。
request.getParameter()取得是通過容器的實現來取得通過類似post,get等方式傳入的數據,,? request.setAttribute()和getAttribute()只是在web容器內部流轉,僅僅是請求處理階段,這個的確是正解.
getAttribute是返回對象,getParameter返回字符串
request.getAttribute()方法返回request范圍內存在的對象,而request.getParameter()方法是獲取http提交過來的數據。
?
?
?
?
四、POST和GET編碼
get方式有四種:
post只知道有一種:form中method屬性為post。
?
????doGet()方法:處理GET方式請求?
????doPost()方法:處理POST方式請求?
?
轉載于:https://www.cnblogs.com/expedition/p/11213370.html
總結
以上是生活随笔為你收集整理的请求转发、包含、重定向 getAttribute 和 setAttribute POST和GET编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html链接代码分析
- 下一篇: 前端面试总结--JS