码迷,mamicode.com
首页 > Web开发 > 详细

Jsp重定向(response.sendRedirect())和转发(request.getRequestDispatcher().forward(request,r)的区别

时间:2015-08-21 13:50:19      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="sun.awt.RepaintArea"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'check_login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	<%
  		request.setCharacterEncoding("utf-8");
  		String uname = request.getParameter("username");
  		String pwd = request.getParameter("password");
  		
  		if("sa".equals(uname) && "123".equals(pwd)){
  			//response.sendRedirect("welcome.jsp");
  			request.getRequestDispatcher("welcome.jsp").forward(request,response);
  		}else{
  			response.sendRedirect("login.jsp");
  		}
  	%>
  
  </body>
</html>

区别1:request.getRequestDispatcher().forward()是转发,是在同一个请求里。response.sendRedirect()是重定向,因此放在request里的数据不会被传过去。

区别2:浏览器的地址栏变化,转发地址栏不会发生变化,重定向会发生变化。

版权声明:本文为博主原创文章,未经博主允许不得转载。

Jsp重定向(response.sendRedirect())和转发(request.getRequestDispatcher().forward(request,r)的区别

标签:jsp

原文地址:http://blog.csdn.net/u014357995/article/details/47831271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!