aspose file tools*
The moose likes JSP and the fly likes access a variale in all jsp files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "access a variale in all jsp files" Watch "access a variale in all jsp files" New topic
Author

access a variale in all jsp files

Joao Sitoe
Greenhorn

Joined: Sep 12, 2012
Posts: 4
Hi,

I'm a newbie in JSP and I'm stuck in a question.

I have a matrix int[][] and I want it to be accessible to all jsp files.

The code is:

int[][] matrix = methodThatGeneratesTheMatrix;

How can I access/use this matrix in all jsp files?

Any help I really appreciate.

thaks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Place the variable in application scope (via servletContext.setAttribute()).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joao Sitoe
Greenhorn

Joined: Sep 12, 2012
Posts: 4
Thank you Bear Bibeault,

I'm searching for hours on how to accomplish that and can't figure it out. Can you give me some examples?

Thanks a lot
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

I already gave you the method in ServletContext: see here
Joao Sitoe
Greenhorn

Joined: Sep 12, 2012
Posts: 4
In the file "setvalue.jsp" I set a variable like this:

Integer t = 10;
application.setAttribute("tvalue", t);

and in another file "getvalue.jsp" I'm trying to get the stored value like this:

Integer m = (Integer)application.getAttribute("tvalue");
if (m == null)
{
out.print("null found");
}
else
{
out.println("Found"+m);
}


When running getvalue.jsp is returned this error: org.apache.jasper.JasperException: PWC6049: Attempted a bean operation on a null object. But when I use getAttribute("tvalue") in the same file ("setvalue.jsp"), it works fine.

What is it happening ?

Thanks for your patience :)
Joao Sitoe
Greenhorn

Joined: Sep 12, 2012
Posts: 4
I got it, the error was caused by remains of previous attempts :)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: access a variale in all jsp files