<%!
class father {
static int s = 0;
}
%>
<%
father f1 = new father();
father f2 = new father();
f1.s++;
out.println(f2.s); // It must print "1"
%>
When I run the file, I got this error. Can anybody explain?
The field s cannot be declared static; static fields can only be declared in static or top level types.
Don’t do this in a JSP. Create a real Java class, if need be in flavor of a Javabean.
and use a Servlet class to do the business task:
which you map in
web.xmlas follows:and create
/WEB-INF/father.jspas follows:and invoke the
FatherServletbyhttp://localhost:8080/contextname/father. The${father2.count}will display the return value offather2.getCount().To learn more about programming JSP/Servlets the right way, I recommend you to get yourself through those tutorials or this book. Good luck.