如何在javascript中获取请求转发后的request对象传递的数据

来源:互联网 发布:纸箱价格算法 编辑:程序博客网 时间:2024/05/17 20:30

下面的例子:在 index.jsp 页面通过   request.setAttribute("param","nihao"); 来给变量param赋值,

                     并通过   <jsp:forward page="deal.jsp"/>将页面请求转发到deal.jsp,

                     然后在deal.jsp页面单击按钮,通过onclick事件调用show()方法,

                     show()方法的  var str="<%=request.getAttribute("param") %>"语句就是在javascript中通过jsp语句

             获取request范围内的变量的值,

                     最后通过  document.getElementById("showText").value=str把值显示在文本框中

index.jsp:

    .

    .

    .

    <body>
    <%
        String str="param";
        request.setAttribute("param","nihao");//保存执行结果
    %>
    <jsp:forward page="deal.jsp"/>
  </body>

    .

    .

    .

deal.jsp:

    .

    .

    .

  <head>

    <script language="javascript">
      function show(){
        var str="<%=request.getAttribute("param") %>";
        document.getElementById("showText").value=str;
      }
    </script>
  </head>
 
  <body>
    <input type="button" value="Shot Me" onclick="show()">
    <input type="text" id="showText"/>
  </body>

    .

    .

    .

1 0
原创粉丝点击