admin管理员组

文章数量:1410730

I am new here. I am trying to store a JavaScript variable in a session by using hidden fields and submitting a form:

function populateInput()
{
    rowClicked = true;
    var key = document.getElementById("key").innerText;
    var value = document.getElementById("value").innerText;

    document.getElementById("key2").value = key;
    document.getElementById("value2").value = value;

    var form = document.getElementById("output");
    form.submit();
}

which will be called by:

<html>
     <tr id="row" onclick="populateInput()"
</html>

The object is to take the values from a row, which contains two columns, a key and a value (stored in a HashMap), and pass them to another jsp, so that they can be displayed in two text boxes (one for key and one for value) by clicking a row on another jsp.

The trouble I am having is with setting the attribute within scriptlets. I am getting the innerText of a td element, and assigning it to a variable in JavaScript and then passing it to the value of an HTML DOM object.

Then I want to submit this to another form using scriptlets (<%request.setAttribute%>), but how? I realize that by using scriptlets, you're using Java code, which is not an HTML attribute.

Any help would be highly appreciated.

Here is the whole table:

<%if (m != null){%>
    <%for (Map.Entry<String, String> e : m.entrySet()){ %>
<tr id="row" onclick="populateInput()">
<td id="key"><%=e.getKey() %></td>
<td id="value"><%=e.getValue() %></td>
</tr>
<%} %>
<%} %>

I want to get the innerText of the td elements, store them into the vars, key, and value, and then store those vars into the value of:

<input type="hidden" id="key2">
<input type="hidden" id="value2">

The trouble I am having is with setting the value of the hidden fields using request.setAttribute("", obj) in scriptlets, and submitting them to the other form.

I am new here. I am trying to store a JavaScript variable in a session by using hidden fields and submitting a form:

function populateInput()
{
    rowClicked = true;
    var key = document.getElementById("key").innerText;
    var value = document.getElementById("value").innerText;

    document.getElementById("key2").value = key;
    document.getElementById("value2").value = value;

    var form = document.getElementById("output");
    form.submit();
}

which will be called by:

<html>
     <tr id="row" onclick="populateInput()"
</html>

The object is to take the values from a row, which contains two columns, a key and a value (stored in a HashMap), and pass them to another jsp, so that they can be displayed in two text boxes (one for key and one for value) by clicking a row on another jsp.

The trouble I am having is with setting the attribute within scriptlets. I am getting the innerText of a td element, and assigning it to a variable in JavaScript and then passing it to the value of an HTML DOM object.

Then I want to submit this to another form using scriptlets (<%request.setAttribute%>), but how? I realize that by using scriptlets, you're using Java code, which is not an HTML attribute.

Any help would be highly appreciated.

Here is the whole table:

<%if (m != null){%>
    <%for (Map.Entry<String, String> e : m.entrySet()){ %>
<tr id="row" onclick="populateInput()">
<td id="key"><%=e.getKey() %></td>
<td id="value"><%=e.getValue() %></td>
</tr>
<%} %>
<%} %>

I want to get the innerText of the td elements, store them into the vars, key, and value, and then store those vars into the value of:

<input type="hidden" id="key2">
<input type="hidden" id="value2">

The trouble I am having is with setting the value of the hidden fields using request.setAttribute("", obj) in scriptlets, and submitting them to the other form.

Share Improve this question edited Oct 4, 2012 at 3:44 Braz Mann asked Oct 4, 2012 at 3:19 Braz MannBraz Mann 251 gold badge1 silver badge5 bronze badges 2
  • 1 Scriptlets run on the server. JavaScript runs in the browser. How exactly are you trying to "submit this to another form?" Show an SSCCE please. – Matt Ball Commented Oct 4, 2012 at 3:23
  • Oy, that's pletely unreadable. As a general rule, please edit your question instead of menting on it – especially when adding code. – Matt Ball Commented Oct 4, 2012 at 3:36
Add a ment  | 

2 Answers 2

Reset to default 2

You CANNOT set a Session using JavaScript the reason being as explained in Matt Ball's ment.

You can either

  1. Use AJAX and send required data using POST/GET method and then set it to the session.
  2. Or You can use a cookie.

You cannot store a session variable directly from javascript because you can only set the value of session in the server side.So you need some AJAX to municate with the server from javascript and then manipulate the session value.

本文标签: htmlHow to store a JavaScript variable in session using scriptletsStack Overflow