8

I have a function defined inside the script tags of head.(in a JSP) I want to declare a string variable in the JSP and pass it as a parameter to this function

<%  String uname ="multiple"; %>
<form action="ExampleServlet" method="post" onclick="pagetype(${uname});"><br>
    <input type="submit" name="Log in" value="Login" />
</form>

But this doesn't work. need Help

reevesy
  • 3,452
  • 1
  • 26
  • 23
Naveen
  • 1,193
  • 4
  • 13
  • 17

2 Answers2

15

You have to use like this

<% String uname ="multiple"; %>
<form action="ExampleServlet" method="post" onclick="pagetype('<%=uname%>');"><br>
    <input type="submit" name="Log in" value="Login" />
</form>
reevesy
  • 3,452
  • 1
  • 26
  • 23
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
8

If you want to avoid scirplet, you can use expression language by putting it between single quotes.

onclick="pagetype('${uname}')";

without the quotes, it tries to look for a variable with name same as the value of uname.

PS: Used the chrome/firefox dev-tools debugging to find out what's going wrong.

Chayan Ghosh
  • 718
  • 5
  • 18