0

I want to pass JavaScript variable from one JSP page to other.

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

<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");

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

Any ideas what can be the optimal way to do this?

Aakash Goplani
  • 1,150
  • 1
  • 19
  • 36
  • If i get it right this is a get request which is simply to long. – Thomas Nov 09 '17 at 14:36
  • @ThomasKleßen - correct. – Aakash Goplani Nov 09 '17 at 14:37
  • You could consider using a javascript CSV exporter (and not have to send the whole table to the server again). Datatables has a nice built-in one, also see [this question](https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side) – James Nov 09 '17 at 15:05
  • @James - My aim is to convert HTML tables to csv and write them to server path. I did begin with Javascript - https://stackoverflow.com/questions/47188611/convert-html-table-to-excel-and-save-it-on-server but later was stuck up as any client side language cannot communicate with server file system, so I rewrote my code using jsoup and apache poi – Aakash Goplani Nov 09 '17 at 15:19

1 Answers1

1

When a form doesn't have a method, the default method used is GET. Use POST for larger payloads. 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.

Joseph
  • 117,725
  • 30
  • 181
  • 234