File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Jsp expecting Static reference to execute java function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Jsp expecting Static reference to execute java function" Watch "Jsp expecting Static reference to execute java function" New topic

Jsp expecting Static reference to execute java function

Brijesh shah
Ranch Hand

Joined: Mar 16, 2007
Posts: 90
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;
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62279

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
It is sorta covered in the JavaRanch Style Guide.
subject: Jsp expecting Static reference to execute java function