aspose file tools*
The moose likes JSP and the fly likes How to declare a static variable in JSP  ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to declare a static variable in JSP  ?" Watch "How to declare a static variable in JSP  ?" New topic
Author

How to declare a static variable in JSP ?

Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

hi friends i want to use a variable which is static like we use in java. i mean if j=0 at begining and if j++ then it will be j=1 like wise value of j will keep increasing should not assign j=0 again. i know in java i write public static int j=0;
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Not a good idea. Firstly, modern JSPs should have no JAva code in them at all -- let alone static variables.

If you need to store a value that needs to persist across the application use application scope to store it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

@Bear , no actually i don't want to share that variable with other jsp pages but i want that variable to keep increasing


see in jsp <% int j=0%> on click method of anchor tag i want to increase this j variable but should not assign 0 to it again so what should i have to do ?
Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

for an example assume you have a counter initialized as cnt=0 and want to count how many times user clicked on anchor tag and print that counter values
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Again, no Java code in a JSP. That's a poor and obsolete practice from 12 years ago.

If it's a user-specific value it should be placed in the session.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
If you REALLY want to declare a static variable in a JSP, you just need to use the declaration syntax.
<%! static int myStatic = 0 %>
However, as already mentioned, it is not really recommended to use java code in a JSP.

If what you want is a hit counter, you might consider implementing a filter that intercepts all requests, and examines the request url to keep track.
The nice thing about that approach is that all of your counting code is just in that filter, and not polluting your JSP pages.

Almalyn caguioa
Greenhorn

Joined: Feb 01, 2013
Posts: 8
yes we declare the varible as static in a jsp.we declare the
static varible in the declaration tag.
ex:-
<%! static int a=10;%>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Bear in mind that using any Java in a JSP page is obsolete and considered a very poor practice. Also bear in mind that introducing declarations in a JSP is a great way to introduce thread-safety problems.

There are many good reasons that Java scriptlets have been discredited and obsolete for over 11 years!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to declare a static variable in JSP ?