您好,登錄后才能下訂單哦!
pageContext, request,session,application 對象都有這些方法,稱為域對象
Object getAttribute(String name)
Enumeration getAttributeNames()
removeAttribute(String name)
void setAttribute(String name,Object o)
pageContext:屬性的作用范圍僅限于當前JSP頁面
request:僅限于同一個請求
session:限于一次會話(瀏覽器打開直到關閉是一次會話)
application:屬性的作用范圍限于當前web應用。只要在一處設置屬性,在其他各處的JSP或Servlet中都可以獲取到
請求的轉發和重定向:
1.區別:請求的轉發只發出了一次請求,而重定向則發出了兩次請求
//轉發:地址欄為/forwardServlet import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ForwardServlet */ @WebServlet("/forwardServlet") public class ForwardServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); //請求的轉發 //1.調用HttpServletRequest的getRequestDispatcher()方法獲取RequestDispatcher對象 //調用getRequestDispatcher()需要傳入要轉發的地址 String path = "testServlet"; RequestDispatcher requestDispatcher = request.getRequestDispatcher("/" + path); //2.調用HttpServletRequest的forward(request,response)進行請求的轉發 requestDispatcher.forward(request, response); } } //重定向:地址欄為testServlet import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class RedirectServlet */ @WebServlet("/redirectServlet") public class RedirectServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); //執行請求的重定向,直接調用response.sendRedirect(path)方法,path為重定向的地址 String path = "testServlet"; response.sendRedirect(path); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
在最終的Servlet中,轉發的request對象和中轉的那個request是同一個對象,重定向的則不是。
轉發:/ 代表的是當前web應用的根目錄;重定向:代表的是當前web站點的根目錄
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。