请求重定向与请求转发的区别

一 请求重定向与请求转发的区别


 

 
 
二 生活小案例


 

 
 
三 请求转发实例(reg.jsp->response.jsp->request.jsp)
1、reg.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
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 'index.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 >
     < h1 > 用户注册 </ h1 >
     < hr >
     <%
        int  number=-1;
        //说明用户第一次访问页面,计数器对象还未创建
        if (application.getAttribute( "counter" )== null )
       {
           application.setAttribute( "counter" , 0);
       }
       number = Integer.parseInt(application.getAttribute( "counter" ).toString());
       number++;
       application.setAttribute( "counter" , number);
     %>
     <!-- <form name="regForm" action="request.jsp" method="post"> -->
     < form  name = "regForm"  action = "response.jsp"  method = "post" >
     < table >
       < tr >
         < td > 用户名: </ td >
         < td >< input  type = "text"  name = "username" /></ td >
       </ tr >
       < tr >
         < td > 爱好: </ td >
         < td >
            < input  type = "checkbox"  name = "favorite"  value = "read" > 读书
            < input  type = "checkbox"  name = "favorite"  value = "music" > 音乐
            < input  type = "checkbox"  name = "favorite"  value = "movie" > 电影
            < input  type = "checkbox"  name = "favorite"  value = "internet" > 上网
         </ td >
       </ tr >
       < tr >
          < td  colspan = "2" >< input  type = "submit"  value = "提交" /></ td >
       </ tr >
     </ table >
     </ form >
     < br >
     < br >
     < a  href = "request.jsp?username=李四" > 测试URL传参数 </ a >
   
     < br >
     < br >
     < center >
             您是第 <%= number  %> 位访问本页面的用户。
     </ center >
   </ body >
</ html >
2、response.jsp
<%@  page  language = "java"  import = "java.util.*,java.io.*"  contentType = "text/html; charset=utf-8" %>
<%
    response.setContentType( "text/html;charset=utf-8" );  //设置响应的MIMI类型
   
    out.println( "<h1>response内置对象</h1>" );
    out.println( "<hr>" );
     //out.flush();  //解决PrintWrite对象的浏览器输出总是先于out对象输出这一问题
   
    PrintWriter outer = response.getWriter();  //获得输出流对象
    outer.println( "大家好,我是response对象生成的输出流outer对象" );
     //response.sendRedirect("reg.jsp");//请求重定向
     //请求重定向
     //response.sendRedirect("request.jsp");
     //请求转发
    request.getRequestDispatcher( "request.jsp" ).forward(request, response);
%>
3、request .jsp
<%@   page   language = "java"   import = "java.util.*"   contentType = "text/html; charset=utf-8" %>
<%
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 'index.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 >
     < h1 > request内置对象 </ h1 >
     <%
       request.setCharacterEncoding( "utf-8" );  //解决中文乱码问题,无法解决URL传递中文出现的乱码问题。
       request.setAttribute( "password" ,  "123456" );
   
     %>
        用户名: <%= request.getParameter( "username" )  %> < br >   
        爱好 : <%
            if (request.getParameterValues( "favorite" )!= null )
           {
                   String[] favorites = request.getParameterValues( "favorite" );
                    for ( int  i=0;i<favorites.length;i++)
                   {
                      out.println(favorites[i]+ "&nbsp;&nbsp;" );
                   }
                }
         %>  < br >
         密码: <%= request.getAttribute( "password" )  %> < br >
         请求体的MIME类型: <%= request.getContentType()  %> < br >
         协议类型及版本号:   <%= request.getProtocol()  %> < br >
         服务器主机名 : <%= request.getServerName()  %> < br >
         服务器端口号: <%= request.getServerPort()  %> < BR >
         请求文件的长度 : <%= request.getContentLength()  %> < BR >
         请求客户端的IP地址: <%= request.getRemoteAddr()  %> < BR >
         请求的真实路径: <%= request. getRealPath( "request.jsp" )  %> < br >
         请求的上下文路径: <%= request.getContextPath()  %> < BR >                          
   </ body >
</ html >
 
四 运行结果


 

 
  • 大小: 183.4 KB
  • 大小: 168.8 KB
  • 大小: 58.4 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值