0

I want to use javascript variables in JSP code . I know there is issueof Client side and server side variables , but still any way to do so without interacting with server or servlet??

code:

<form>
    <select name="select" id="form_events">
        <%
            for (int i = 1; i <= 5; i = i + 1) {
        %>
        <option value=<%=i%>>
            <%=i%></option>
        <%
            }
        %>

    </select>

</form>

Can any one help me in assigning value to variable "val" ??

kartik
  • 110
  • 1
  • 1
  • 10

1 Answers1

1

You can't, JSP is on the server side, JS is on the client size. Also, you should not use scriptlets, it makes your code similar to a bowl of spaghetti.

If you only need to generate JavaScript code you can do that e.g. by using this syntax:

var variable = '<%=myJspVariable.toString()%>';

This renders the myJspVariable as a string, and it can be used in your JavaScript code.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • 1
    Don't throw variables into JavaScript without escaping them unless you are very, very sure that they only contain safe characters. You risk breaking your code or introducing XSS (especially if you are dealing with user input). – Quentin Jun 09 '15 at 14:29