web开发jsp之间页面传递参数的7种方式



1.利用javabean

Javabean类:

package entity;

public class User {
    private String username="";
    private String gender="";
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public User() {
    }
}

传参数的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <jsp:useBean id="user" class="entity.User" scope="session" />
    <center>
        <h1>传参页面</h1>
    </center>
    <hr>
    <%
        user.setUsername("绅士");
        user.setGender("男");
    %>
    <center>
点击我,<a href="receive.jsp">跳转</a>
    </center>

</body>
</html>

接收参数的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
        <hr>
        <jsp:useBean id="user" class="entity.User" scope="session"></jsp:useBean>
        <p>使用JSP动作接收传参</p>
        <h4>
            性别:<jsp:getProperty name="user" property="username" /><br> 密码:<jsp:getProperty
                name="user" property="gender" /><br>
        </h4>
        <hr>
        <p>使用JSP普通方式接收参数</p>
        <h4>
            性别:<%=user.getUsername()%><br> 密码:<%=user.getGender()%><br>
        </h4>
    </center>
</body>
</html>

2.绑定到session对象

传参页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    </center>
    <hr>
    <%
    session.setAttribute("username", "绅士");
    session.setAttribute("gender", "男");

    %>
    <center>
         <a href="receive.jsp">传递参数</a>  
    </center>
</body>
</html>
 
接收参数页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>
        <%  

            out.print("姓名:"+session.getAttribute("username"));  
        %>  
        <br/>  
        <%  
            out.print("性别:"+session.getAttribute("gender"));  
        %>  
    </center>
</body>
</html>

3.绑定到application

传参页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    </center>
    <hr>
    <%
    application.setAttribute("username", "绅士");
    application.setAttribute("gender", "男");

    %>
    <center>
         <a href="receive.jsp">传递参数</a>  
    </center>
</body>
</html>

接收参数页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>
        <%  

            out.print("姓名:"+application.getAttribute("username"));  
        %>  
        <br/>  
        <%  
            out.print("性别:"+application.getAttribute("gender"));  
        %>  
    </center>
</body>
</html>

4.绑定到request对象

这里用采用的是请求转发的方式 传参页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    </center>
    <hr>

        <%  
            request.setAttribute("name","绅士");  
        %>  
        <jsp:forward page="receive.jsp"/>  

</body>
</html>

接收参数页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>
        <%  
            out.println("传递过来的参数是:"+request.getAttribute("name"));  
        %>  
    </center>
</body>
</html>

5.使用JSP动作指令传参

传参页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    </center>
    <%
    String username="绅士";
    String gender="男";
    %>
    <hr>
    <jsp:forward page="receive.jsp">
        <jsp:param name="name" value="Jakc"  />
        <jsp:param name="gender" value="man" />
    </jsp:forward>
</body>
</html>

接收参数页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>
        <%
        request.setCharacterEncoding("utf-8");
            String name = request.getParameter("name");
            out.print("姓名:" + name);
        %>
        <br />
        <%
            out.print("性别:" + request.getParameter("gender"));
        %>
    </center>
</body>
</html>

6. 表单传参

传参页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    </center>
    <form action="receive.jsp" method="get" align="center">
        姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br />

        密码:<input type="password" name="password" size="20" value=""
            maxlength="20"><br /> <br /> <input type="submit"
            name="submit" value="登录"> <input type="reset" name="reset"
            value="重置"><br />
    </form>
</body>
</html>

接收参数页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>
        <%
            request.setCharacterEncoding("utf-8");
            String name = request.getParameter("name");
            out.print("姓名:" + name);
        %>
        <br />
        <%
            out.print("性别:" + request.getParameter("password"));
        %>
    </center>
</body>
</html>

7.URL传参

传参页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    </center>
    <hr>
    <%!

    String username="绅士";
    String gender="男";

    %>
    <center>
         <a href="receive.jsp?username=<%=username %>&gender=<%=gender%>">传递参数</a>  
    </center>
</body>
</html>

接收参数页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>
        <%  
            String name=request.getParameter("username");  

            out.print("姓名:"+name);  
        %>  
        <br/>  
        <%  
            out.print("性别:"+request.getParameter("gender"));  
        %>  
    </center>
</body>
</html>

URL传参的时候还有一个特别的用处,是当你在JSP页面中使用for循环输出某些信息的时候,URL传参的独特之处就体现出来了 
 假设我使用for循环5次,每次浏览器输出点击我跳转,并同时向session绑定参数(方便起见就用循环变量吧)
像这样:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    <%
        for (int i = 0; i < 5; i++) {
            session.setAttribute("index", i);
    %>
    <a href="receive.jsp">点击我跳转 </a><hr>


    <%
        }
    %>
    </center>
</body>
</html>

接收页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>

        接收到的顺序:<%=session.getAttribute("index") %>

    </center>
</body>
</html>
没测试之前误以为点击不同的连接会传递不同的参数,但是事实上却是每一次穿过来的参数都是4,这就很奇怪!

为什么是这样呢? 这里博主做出解释:因为session要等到输出所有的内容,也就是说等到递增到4之后,才将最后一个i的值传给下一个页面(也就是说,i=0,时候,session绑定的对象的值是0,i=1的时候,绑定的对象的值是1,…….) 但是呢要等到i递增到4之后整个JSP页面输出完毕之后才传到下一个JSP页面,所以每一次传到去的参数就是4,接收到的参数自然每一次都是4了!

那我们要怎么做才能做到当我点击不同的连接的时候传递参数是其对应的参数呢??(比如说我们点击第一个链接的时候,传递过去的参数是0;第二个参数的时候,参数是1….)

这个时候使用URL传参就很好用了

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
    <center>
        <h1>传参页面</h1>
    <%
        for (int i = 0; i < 5; i++) {
    %>
    <a href="receive.jsp?index=<%= i %>">点击我跳转 </a><hr>


    <%
        }
    %>
    </center>
</body>
</html>
接收页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
    <center>
        <h1>接收参数页面</h1>
        <hr>
        <%--接收参数的时候必须使用这个方法否则会找不到传递过来的参数 --%>
        使用request.getParameter接收到的参数 接收到的顺序:<%=request.getParameter("index")%>
        <hr>
        使用request.getAttribute接收到的参数
        <%=request.getAttribute("index")%>
    </center>
</body>
</html>

这里请注意了,一定要使用request.getParameter(string name)这个方法才能得到参数,否则是不能得到参数的!




  • 8
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以为您提供以下的解答: 1. 在MyEclipse中建立Web站点 在MyEclipse中建立Web站点,可以按照以下步骤进行: 1) 在MyEclipse中选择File -> New -> Web Project,输入项目名称,选择Web Application,点击Finish。 2) 选择Web Content目录,右键选择New -> JSP File,输入jsp文件名,点击Finish。 3) 在JSP文件中编写HTML代码,也可以使用JSP指令元素和脚本语法。 2. 熟悉JSP指令元素与脚本语法 JSP指令元素和脚本语法是JSP开发中常用的语法,可以进行页面的数据处理和显示。 JSP指令元素包括page指令、include指令、taglib指令等。其中,page指令用于设置页面的属性和导入Java类和包;include指令用于包含其他JSP页面或HTML页面的内容;taglib指令用于导入标签库。 JSP脚本语法包括表达式、声明、脚本段等。其中,表达式用于输出表达式的值;声明用于定义变量和方法;脚本段用于编写Java代码。 3. 实现页面间数据传递JSP中,可以使用request对象、session对象、application对象等进行页面间数据传递。 request对象用于在同一次请求中传递数据,可以通过request.getParameter()方法获取表单数据或URL中的参数,也可以通过request.setAttribute()方法将数据存储在request对象中,然后传递给其他JSP页面或Servlet。 session对象用于在不同请求之间传递数据,可以通过session.setAttribute()方法将数据存储在session对象中,然后传递给其他JSP页面或Servlet。 application对象用于在整个Web应用中传递数据,可以通过application.setAttribute()方法将数据存储在application对象中,然后传递给其他JSP页面或Servlet。 以上是关于MyEclipse建立Web站点、JSP指令元素与脚本语法以及页面间数据传递的简单介绍,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值