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

jsp的request与forward实例

时间:2015-01-24 19:59:41      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:

下面是一个request内置对象与forward指令的实例,只要是为了说明request在请求中获取请求参数,操作request的范围属性,以及当使用forward转发用户请求的时候,客户端的请求参数和请求属性不会丢失。。

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>取钱的表单页</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>
    This is my JSP page. <br>
    <!-- 取钱的表单 -->
    <form  method= "post" action="first.jsp">
                取钱: <input type="text" name="balance">
           <input type="submit" value="提交">
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<%
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 ‘first.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>
    //  
    <%
         String str = request.getParameter("balance");
         double qian = Double.parseDouble(str);
         if(qian<500)
         {
            out.println("账户余额减少:"+qian);
         }else
         {
           List<String> info = new ArrayList<String>();
           info.add("AAAAAAAAA");
           info.add("BBBBBBBBB");
           info.add("CCCCCCCCC");
           request.setAttribute("info", info);
         
        
     %>
     <!-- 实现转发 -->
     <jsp:forward page="second.jsp"></jsp:forward>
     <%
      }
     %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<%
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 ‘second.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>
      <%
         String str = request.getParameter("balance");
         double qian = Double.parseDouble(str);
         List<String> info =(List<String>)request.getAttribute("info");
         for(String trm:info)
         {
             out.println(trm+"<br>");
         }
         out.println("取钱"+qian+"");
      %>
  </body>
</html>

表明,使用forward用户请求时,请求参数与request范围的属性都不会丢失,即forward的动作还是原来的请求,并未向服务器再次发请求。

jsp的request与forward实例

标签:

原文地址:http://www.cnblogs.com/duhuo/p/4246349.html

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