top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can we define a class in a JSP Page?

+1 vote
397 views
Can we define a class in a JSP Page?
posted Aug 23, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

It’s not a good practice though, but we can define a class inside a JSP Page. Below is the sample code for this:

<%!
private static class NestedClass { //static is better because Servlet is multi-threaded
  private final int num = 0;
  public int getNum() {
    return num;
  }
}
%>

Or

<%      
    class Person { 
        //this will go inside method body, so can't be public
    }
%>
answer Sep 7, 2017 by Manikandan J
...