Pinky Thakkar wrote:
> Hi,
>
> I want to define a variable: index, which has an application scope.
> {
> like in ASP, i would write something like :
>
> Application("index")=0
> }
>
> How do i do it in JSP?
>
> Thanks,
> Pinky
First thing - you cannot store primitive objects (like an int) in one of these
scopes -- only objects. So, you'd probably create yourself a Counter class
something like this:
public class Counter {
private int value = 0;
public Counter() {
; // No-args constructor required for instantiation from a JSP
page
}
public Counter(int initialValue) {
value = initialValue;
}
public int getValue() {
return (value);
}
public void setValue(int newValue) {
value = newValue;
}
}
Next, you can cause an object like this to be added to the application scope
from either a Servlet or a JSP page. From a servlet, it would be created like
this:
getServletContext().setAttribute("index", new Counter());
and accessed (say, to increment the value) -- ignoring synchronization issues
-- with:
Counter index = (Counter) getServletContext().getAttribute("index");
int currentValue = index.getValue();
index.setValue(currentValue + 1);
// Note -- no need to re-store the object
>From a JSP page (1.0 syntax) create it (if not present already) with:
<jsp:useBean id="index" class="Counter"
scope="application" />
and access it by using the name specified for the ID ("Index" in this
example). Essentially, the "Counter index ..." declaration above is done for
you by the JSP page compiler.
Craig McClanahan
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".