0

I have written the following jsp code in a jsp page for testing

<%!
    public String sayHello(String myName)
    {
          out.println("Hello Java"); // this line shows error
          return "Hello"+myName;
    }
%>

<%
  String str="William";
%>

   <%=sayHello(str)%>

If i remove the following line the page runs without error

         out.println("Hello Java");

Again, if i run a jsp file with only the following line included (not above codes) then also it runs well

         out.println("Hello Java");

Please help me where am i doing mistakes

user1673627
  • 271
  • 3
  • 6
  • 13

1 Answers1

1

You can't use JSP implicit objects in methods. In order to access implicit object in custom methods, you have pass the reference of implicit (JspWriter) object to the method but use of Java code in JSP is highly discouraged.

Fore more information read wiki and How to avoid Java Code in JSP-Files?.

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186