How to use static variables in JSP ?


Web tier: servlets, JSP, Web frameworks: How to use static variables in JSP ?

  1. How to use static variables in JSP ? (4 messages)

    I'm trying to use static variables into a JSP page (with Tomcat) but I get strange errors...isn't it possible to declare static variables into JSP pages?
  2. As far as i know, the jsp is translated by servlet engine into <some class depends upon the jsp file name>._jspService() method. I think we cannot have static variable declarations in any method in java.

    Please correct me if i am wrong.

  3. Yes it's sure, in Java you cannot declare static variables
    into methods, so when JSP are built it seems they're all placed inside "service", so that's the problem...
    Anyway I was investigating about bean properties, maybe it's possible to achieve a static-like behaviour setting
    the bean scope as "application" in this case the bean properties will persist between clients....
  4. hey there... try this in a new jsp file called static.jsp:

      static int counter = 0;

      out.print("I've been hit: " + counter + " times!");

  5. Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

    when I am declaring statement as above shown. then jsp page showing errors that ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY are undefined variables. how to solve this problems. please send a mail to sngolla at rediffmail dot com.