1. 程式人生 > >後臺傳送前臺顯示

後臺傳送前臺顯示

注意路徑問題:最好使用 request.getContextPath()+"/index.jsp"

①重定向

後臺比如登入成功了。傳送資料。域物件標準方法:get/set/remove Attribute()

request.getSession().setAttribute("loginUser", user);
response.sendRedirect(request.getContextPath()+"/index.jsp");

//request.getSession().removeAttribute("loginUser");         移除session

前臺進行獲取,這裡存資料使用的是session,所以可以使用重定向的方式來指定。

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

			<c:if test="${empty loginUser}">
				<li><a href="login.jsp">登入</a></li>
				<li><a href="register.jsp">註冊</a></li>
			</c:if>
			<c:if test="${not empty loginUser}">
				歡迎:${loginUser.name}
			</c:if>

②轉發,在request中放了資料,需要將request一起轉發出去

request.setAttribute("msg", "使用者名稱密碼輸入錯誤!");
request.getRequestDispatcher("/login.jsp").forward(request, response);

前臺只需要使用 ${msg} 來進行獲取就可以了