1

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.

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. Commented Oct 4, 2012 at 3:23
  • Oy, that's completely unreadable. As a general rule, please edit your question instead of commenting on it – especially when adding code. Commented Oct 4, 2012 at 3:36

2 Answers 2

2

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

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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 communicate with the server from javascript and then manipulate the session value.

4 Comments

This is what I was thinking last night, but I wasn't sure. I'm so new to all of this. So even if the javascript variable was set within the value attribute of an HTML element, you couldn't set the session of it using scriptlets and then get the parameter from the other page?
You can get the value of session in your HTML using server side code,but you cannot set its value from the client side.It can only set in the server side.Just think about the securtiy if we are able to set some critical info like session from client side.:)
Right, not good. But I didn't know if when you set a JS variable to an HTML attribute, if it became server-side. Guess not though.
Yes you can set some javascript value to an HTML element,but this all is happening in the client side only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.