后台发送前台显示

xiaoxiao2025-04-30  13

注意路径问题:最好使用 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} 来进行获取就可以了

转载请注明原文地址: https://www.6miu.com/read-5029502.html

最新回复(0)