web开发中经常用到页面传参,现在就来总结一下各种传参方式。

1.a标签传参

<a>标签传值的形式--参数固定:<a href="地址?参数名=值"></a>,多个参数用&隔开<a href="地址?参数1=值&参数2=值"></a>
例:<a href="a.jsp?name=张三"></a>
单个参数:
<a href="addInfo.do?method=addUserInfo"></a>
多个参数:
<a href="addInfo.do?method=addUserInfo&username=admin"></a>

2.a标签传值的形式–参数可变:
var 变量 = 值;
document.getElementById(“id名”).href=”地址?参数=”+变量;
例子:

var 变量 = 值;
document.getElementById("id名").href="地址?参数="+变量;
例子:
<span style="font-family:Microsoft YaHei;"><script type="text/javascript" src="jquery-1.7.2.js"></script>  
<script> $(function(){<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>var name="王五"; var id=13527892092; var age = 25; $("#n").click(function(){ $(this).attr("href","a.jsp?name="+name+"&id="+id+"&age="+age);//用jquery的方法改变href属性值,</span><span style="font-family: 'Microsoft YaHei';">从而传递可变参数</span><span style="font-family:Microsoft YaHei;"> }); }); </script>  
</head>  

<body>  
   <a href="" id="n">验证</a>  
  </body>  
</html></span> 

3.form表单传参
在form表单中的action中写入参数,写法和a标签擦混参类似。

<form action="loginAction?method=UserLogin" method="post">

  用户名:<input id="username" type="text" name="username">
  密&nbsp码:<input id="password" type="password" name="password">
   <input type="submit" value="OK">
    </form>

4.jsp:param传参
param.jsp

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            param.jsp file  
        </title>  
    </head>  

    <body style="background-color:lightblue">  

        <%request.setCharacterEncoding("GB2312");%>  

        <jsp:forward page="getParam.jsp">  
            <jsp:param name="name" value="心雨"/>  
            <jsp:param name="password" value="123"/>  
        </jsp:forward>  

    </body>  
</html>  

getParam.jsp

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            getParam.jsp file  
        </title>  
    </head>  

    <body style="background-color:lightblue">  
        <% String name=request.getParameter("name"); out.print("name:"+name); %>  
        <br/>  
        <% out.print("password:"+request.getParameter("password")); %>  
    </body>  
</html>  

5.在jsp页面获取传递的参数:

${param.参数名}
${param["参数名"]}
例子:<a href="a.jsp?name=张三">点击</a>;在另一个jsp页面获取值${param.name}或者${param["name"]}