jsp服务器内部跳转与客户端跳转

内部跳转,可以带参数。客户端浏览器的地址没有变

用户访问当前jsp跳转到target.jsp的页面 带参数

<body>
<jsp:forward page="target.jsp">
	<jsp:param value="java1234" name="userName"/>
	<jsp:param value="123456" name="password"/>
</jsp:forward>
</body>

target.jsp 接收参数

<body>
服务器内部跳转后的页面<br/>
userName:<%=request.getParameter("userName") %><br/>
password:<%=request.getParameter("password") %><br/>
</body>

客户端跳转

request的信息带不过去,只能带过去session和application的信息

response.sendRedirect("")

重定向

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("requestKey", "request值");
		HttpSession session=request.getSession();  // 获取session
		session.setAttribute("sessionKey", "session值");
		ServletContext application=this.getServletContext(); // 获取application
		application.setAttribute("applicationKey", "application值");
		response.sendRedirect("target.jsp"); // 客户端跳转/重定向
	}

target.jsp

<body>
<h1>目标地址</h1>
request值:<%=request.getAttribute("requestKey") %><br/>
session值:<%=session.getAttribute("sessionKey") %><br/>
application值:<%=application.getAttribute("applicationKey") %><br/>
</body>

服务器跳转

转发可以获取request的值

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("requestKey", "request值");
		HttpSession session=request.getSession();  // 获取session
		session.setAttribute("sessionKey", "session值");
		ServletContext application=this.getServletContext(); // 获取application
		application.setAttribute("applicationKey", "application值");
		RequestDispatcher rd=request.getRequestDispatcher("target.jsp");
		rd.forward(request, response); // 服务器调转/转发
	}

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/81433449