简体   繁体   English

如何将JavaScript变量从一个JSP页面传递到另一个

[英]How do I pass JavaScript variable from one JSP page to another

I want to pass JavaScript variable from one JSP page to other. 我想将JavaScript变量从一个JSP页面传递到另一页面。

In my first JSP page, I create a dynamic table based on database queries and I want to send this table to second JSP page, what I have done so far is 在我的第一个JSP页面中,我基于数据库查询创建了一个动态表,我想将此表发送到第二个JSP页面,到目前为止,我所做的是

<form id="excelForm">
    <input type="submit" value="Export to CSV" onclick="myFun();"/>
    <input type="hidden" name="pagename" value="Practice/AfterSubmit" />
</form>
<table id="tableContent" border="1">
    .... approx 50-3000 rows
</table>
<script>
    function myFun(){
        console.log("Event sucess");
        var tableData = document.getElementById("tableContent").outerHTML;
        var input = document.createElement("input");        
        input.setAttribute("type", "hidden");       
        input.setAttribute("name", "tableData");        
        input.setAttribute("value", tableData);
        document.getElementById("excelForm").appendChild(input);
    }
</script>

And in second JSP page I do, String myData = request.getParameter("tableData"); 在第二个JSP页面中, String myData = request.getParameter("tableData");

It works fine when the table has upto 50 rows, but when the table size grows, so the size of URL grows and I get 400: Bad Request error 当表最多包含50行时,它工作正常,但是当表大小增加时,URL的大小增加,我得到400:错误的请求错误

Any ideas what can be the optimal way to do this? 有什么想法可以做到这一点的最佳方法是什么?

When a form doesn't have a method , the default method used is GET . 如果表单没有method ,则默认使用的方法是GET Use POST for larger payloads. POST用于较大的有效负载。 You might also want to check your server settings for the maximum request size, if that applies. 您可能还需要检查服务器设置中的最大请求大小(如果适用)。 Some servers ignore requests that are larger than the set limit, and some fail silently. 一些服务器会忽略大于设置限制的请求,而另一些服务器会静默失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM