jsp中新属性的设置和获取

本文介绍如何在JSP中使用request对象进行属性设置与获取,实现页面间的数据传递。通过示例代码展示了如何在表单提交后,将数据保存在request对象中并转发到另一个页面进行显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  用户可以根据自己的需要在request对象中添加属性,然后在另一个jsp程序获取添加的数据。

  在页面中使用request对象的setAttribute("name",obj)方法,可以把数据obj设定在request范围(容器)内,请求转发后的页面使用getAttribute("name")就可以获得数据obj的值。

  设置数据的方法格式:

void request.setAttribute("key",Object);

  key是键,为Sring类型,属性名称。参数Object是键值,为Object类型,是需要保存在request范围内的数据。

  获取数据的方法格式:

  Object request,getAttribute(String name);

  name为键名,所获取的数据类型是由setAttribute("name",obj)中的obj类型决定的。


示例代码:

input039.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'input039.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>
  <form action="sum039.jsp" method="post">
        数据1:<input type="text" name="shuju1"><br>  //提交两个参数的页面
        数据2:<input type="text" name="shuju2"><br>
        <input type="submit" value="提交">
  </form>
  </body>
</html>
sum039.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'sum039.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 str1=request.getParameter("shuju1");  //获取表单提交的参数,转换为实数数据是s1,s2,并求和给属性s3,
                                                    再讲3个新属性保到request对象中,然后转到显示页面。
      String str2=request.getParameter("shuju2");
      Double s1=Double.parseDouble(str1);
      Double s2=Double.parseDouble(str2);
      Double s3=s1+s2;
      request.setAttribute("st1", s1);  // 保存3个属性到request对象
      request.setAttribute("st2", s2);
      request.setAttribute("st3", s3);
    %><jsp:forward page="output039.jsp"></jsp:forward>
  </body>
</html>

output039.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'output039.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>
   <% Double a1=(Double)request.getAttribute("str1");  //从request对象中获取3个属性值,并输出显示
      Double a2=(Double)request.getAttribute("str2");  // 获取的是对象类型,强制实现类型转换
      Double a3=(Double)request.getAttribute("str3");
    %><%=a1 %>+<%=a2 %>=<%=a3 %><br>
        显示<br>
        <% String s1=request.getParameter("shuju1");//获取参数
           String s2=request.getParameter("shuju2");
         %>  <%=s1 %>+<%=s2 %>=<%=a3 %><br>
  </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值