• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP variable declarations  RSS feed

 
Laurens Vergeynst
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm relatively new to JSP (J2EE).
While I was reading my course I encountered the following: JSP declarations. (<%!>)

the purpose of this is, that when you declare a variable between <%! > brackets, the variable would save its sate between multiple requests.
In the course I was reading there was a simple counter example.
The counter variable was declared between <%! int counter=0> and a bit futher there was a scriptlett that increments the counters value <% =counter++>.

Now is my question:
1. how does JSP save that value?
2. what happens when an other client requests the same page will the counter start again from 0?

Thanks!

 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hellow Laurens, welcome to JavaRanche.

The <%! works like a static attribute.

Imagine if you need a counter, so if you do like:


Every client that access the page will increase the counter.
 
Victor Hugo M Machado
Greenhorn
Posts: 22
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

JSP Declarations

The JSP you write turns into a class definition. All the scriptlets you write are placed inside a single method of this class.
You can also add variable and method declarations to this class. You can then use these variables and methods from your scriptlets and expressions.

To add a declaration, you must use the <%! and %> sequences to enclose your declarations, as shown below.

<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%!
Date theDate = new Date();
Date getDate()
{
System.out.println( "In getDate() method" );
return theDate;
}
%>
Hello! The time is now <%= getDate() %>
</BODY>
</HTML>
The example has been created a little contrived, to show variable and method declarations.
Here we are declaring a Date variable theDate, and the method getDate. Both of these are available now in our scriptlets and expressions.

But this example no longer works! The date will be the same, no matter how often you reload the page. This is because these are declarations, and will only be evaluated once when the page is loaded! (Just as if you were creating a class and had variable initialization declared in it.)

Declarations
 
Laurens Vergeynst
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aha,first of all thanks for replying.
So in other words the container recognizes the <%! ...... %> tag , next he puts the variable its value into the Application scope of the web application?

Correct me if I understood it wrong.

 
Victor Hugo M Machado
Greenhorn
Posts: 22
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is because these are declarations, and will only be evaluated once when the page is loaded! (Just as if you were creating a class and had variable initialization declared in it.)
Now that you know how to do this -- it is in general not a good idea to use variables as shown here. The JSP usually will run as multiple threads of one single instance. Different threads would interfere with variable access, because it will be the same variable for all of them. If you do have to use variables in JSP, you should use synchronized access, but that hurts the performance. In general, any data you need should go either in the session object or the request object (these are introduced a little later) if passing data between different JSP pages. Variables you declare inside scriptlets are fine, e.g. <% int i = 45; %> because these are declared inside the local scope and are not shared.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66152
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hebert Coelho wrote:The <%! works like a static attribute.

Not correct. The JSP declaration syntax defines class-level members. They can be static or not.

In any case, Laurens, if you are just starting out with JSP, it's important to start out learning good habits. All scriptlet syntax, including the declaration syntax, that puts Java code in JSPs are now considered very bad practice, and have been discredited for over 9 years now.

Rather than old-fashioned and discredited scriptlets, you should be learning about the JSTL (JSP Standard Tag Library) and the EL (Expression Language).

Perhaps this article regarding proper web application structure can help set you on track.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Now is my question:
>1. how does JSP save that value?
The value gets saved as an instance variable of the servlet created from the JSP.

>2. what happens when an other client requests the same page will the counter start again from 0?
No. The counter is shared across all requests from all users.


A JSP page gets translated into a servlet, which gets compiled into a java class.
Conceptually the code



would turn into something like this in a servlet:



According to the Servlet Specification (SRV.2.2) only one instance of this class should ever exist, and all requests should go to it.
Every time a request is made, it will call the _jspService method.


So the counter gets set to 0 at servlet startup time.
Every request from every user will execute the jspService method, and thus increment the shared counter.

 
Laurens Vergeynst
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I see it, thanks for the explanations!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!