1

this function is written inside the HEAD tag of a jsp file and i call it from inside the jsp BODY tag:

<%!
public void func1(String var1, String var2)
{
    String Name = var1 + "," + var2;
%>
    <input type='image' name=<%=Name%>
     src='somePath' onclick="submit()"/></br>
<%!
}
%>

i'm getting an error message: "cannot find symbol symbol: variable Name location: class SimplifiedJSPServlet"

any suggestion why Name is not recognized ? did i write it properly and placed it in the right place?

Udi Livne
  • 127
  • 2
  • 3
  • 11
  • Obligatory reading: [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) – Reimeus May 11 '14 at 14:12

3 Answers3

3

It looks like your variable Name is only defined within the first scriplet segment. To declare as a class member variable use:

<%!
      String name;
%>

outside of any method scoping.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1
<%!
public void func1(String var1, String var2)
{
    String Name = var1 + "," + var2;
    out.println("<input type='image' name='" + Name + "' src='somePath' onclick='submit()'/><br>");
}
%>

Then you can call the above method wherever you want in the JSP.

Ex: <%= func1("Text1", "Text2") %>
TheOne
  • 31
  • 3
0

Your variable is only in scope for the duration of the function call. To access the value you will need to do something like

request.setAttribute("name", name);

Inside your function (you could use pageContext or request - I don't know what scope you want.

Then outside your function you can simple do

<input type='image' name="${name}" />
Brian
  • 13,412
  • 10
  • 56
  • 82