最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - access javascript variable value in jsp - Stack Overflow

programmeradmin5浏览0评论

I want to pass a javascript variable to my servlet, where I need to use it.

In javascript, the variable count returns the rows of my table and I can show count in the jsp, using $('#counter').html(count); , but I cannot pass count's value to my servlet. I tried document.getElementById("hiddenField").value=count; but it doesn't work.

Javascript

<script>
    var count = 3;
    $(function() {
        $('#counter').html(count);
        $('#addButton').bind('click', function() {
             count = document.getElementById("dataTable").getElementsByTagName("tr").length;
            $('#counter').html(count);
        });
        $('#deleteButton').bind('click', function() {
            count = document.getElementById("dataTable").getElementsByTagName("tr").length;
            $('#counter').html(count);
        });
    });
    document.getElementById("hiddenField").value=count; // ???
</script>

JSP

Count: <span id="counter"></span> <%-- it works --%>

<form method="post" action="newteamsubmit"> 
...
<input type="hidden" id="hiddenField" name ="countRows" />
<input type="submit" name ="button1" value=" Submit " />
<input type="submit" name = "button1" value=" Cancel " />
</form>

Servlet

String cr = request.getParameter("countRows"); //I' ve tried also to convert it 
// to int, but that's not my problem, since I cannot pass the value as a start

I've spent many hours, trying to figure out how I can access a javascript variable in jsp, but I haven't found any solution. Thanks in advance.

I want to pass a javascript variable to my servlet, where I need to use it.

In javascript, the variable count returns the rows of my table and I can show count in the jsp, using $('#counter').html(count); , but I cannot pass count's value to my servlet. I tried document.getElementById("hiddenField").value=count; but it doesn't work.

Javascript

<script>
    var count = 3;
    $(function() {
        $('#counter').html(count);
        $('#addButton').bind('click', function() {
             count = document.getElementById("dataTable").getElementsByTagName("tr").length;
            $('#counter').html(count);
        });
        $('#deleteButton').bind('click', function() {
            count = document.getElementById("dataTable").getElementsByTagName("tr").length;
            $('#counter').html(count);
        });
    });
    document.getElementById("hiddenField").value=count; // ???
</script>

JSP

Count: <span id="counter"></span> <%-- it works --%>

<form method="post" action="newteamsubmit"> 
...
<input type="hidden" id="hiddenField" name ="countRows" />
<input type="submit" name ="button1" value=" Submit " />
<input type="submit" name = "button1" value=" Cancel " />
</form>

Servlet

String cr = request.getParameter("countRows"); //I' ve tried also to convert it 
// to int, but that's not my problem, since I cannot pass the value as a start

I've spent many hours, trying to figure out how I can access a javascript variable in jsp, but I haven't found any solution. Thanks in advance.

Share Improve this question edited Dec 23, 2013 at 18:25 Jaak Kütt 2,6564 gold badges33 silver badges40 bronze badges asked Dec 23, 2013 at 16:45 D-LefD-Lef 1,3496 gold badges14 silver badges21 bronze badges 1
  • How are you submitting the form? – AbhinavRanjan Commented Dec 23, 2013 at 16:48
Add a ment  | 

5 Answers 5

Reset to default 1

The count is puted each time the add button or the delete button are clicked. But you only set the hidden field value once, when the page is loaded (and its value is thus hard-coded to 3).

You must set it, as you're doing for the #counter element, in your click handlers:

$('#addButton').bind('click', function() {
    count = document.getElementById("dataTable").getElementsByTagName("tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});
$('#deleteButton').bind('click', function() {
    count = document.getElementById("dataTable").getElementsByTagName("tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});

Also note that you're repeating exactly the same code in two click handlers here. You should do that only once, for the two buttons:

$('#addButton, #deleteButton').bind('click', function() {
    count = document.getElementById("dataTable").getElementsByTagName("tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});

or even, since you're using jQuery:

$('#addButton, #deleteButton').bind('click', function() {
    count = $("#dataTable tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});

document.getElementById('hiddenField').value is not set because it is outside your document.ready. Put it inside your click handler.

Make sure of 2 things -

  1. There is only one element with id "hiddenField" on your page.
  2. Make sure that the following code

document.getElementById("hiddenField").value=count;

is after in the page.

Just make sure that js sets the hiddenField after the element has been loaded. 3. check for any JS errors using Javascript console.

Rest it looks good

The main issue here is that you are trying to access from the server, a variable that only exists at the client. To access that variable you have to send it from the client to the server using AJAX to trigger some form of API in the backend. REST, SOAP or XML-RPC are mon technologies used for this sort of thing. The server side code is used for generating the UI and providing it with data from a database or such. Commonly the UI is generated only once, and then the client calls the server asking for more data in response to user actions, like clicking a button.

Imagine a table filled with information about books: title, author, publish date etc. This table can get quite large, and traditionally this table will be split up over several pages and possibly a dynamic filter. To save bandwidth and increase the user experience by not loading the entire page from scratch you can use AJAX to ask the server for just the relevant data. Doing so the page updates dynamically and smoothly for the user.

In your case, you can use this technique to update the server every time the user clicks the button.

If however you are really just looking to update a hidden field in a form with a value as the user performs actions, and the server wont do anything with it except show it you can just use javascript.

Remember also that the request variable contains the data you post to the server when you submit the form. The servlet will get the data after the client has posted it, which is after the JSP has generated the page. The sequence of the code execution is JSP -> Javascript -> Servlet.

Hope this helps!

You can use this way:

document.forms[0].countRows.value = counter

Hope this will help you

发布评论

评论列表(0)

  1. 暂无评论