Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to declare a static variable in JSP ?

 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic