Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jsp expecting Static reference to execute java function  RSS feed

Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All;

While I am writing following code in java; and then executing this code in jsp page to have counter of every on load jsp page..

package admin;

public class pageCount {

public int counter;

public int getCounter(){
System.out.println("INSIDE *****");
return counter;



So i am calling above code in jsp page as follows..

<%@page import="admin.pageCount;"%>

The page is
out.println(pageCount.getCounter()); // this says can not make static reference to non static method getCounter() from the pageCount

To make above code of Jsp properly runnable i need to make public int counter and getCounter() method static..

My query is why we need to make variable in java code as static to access the same in jsp page..

Hope to hear on this soon...

Thanking you in advance.

Best Regards;
Author and ninkuma
Posts: 66425
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

Also, at this point in time, putting Java code into a JSP is considered an poor and antiquated practice. You'd do yourself an immense service by learning modern JSP techniques.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!