This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 61084
    
  66

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: 61084
    
  66

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 :)
 
Don't get me started about those stupid light bulbs.
 
subject: access a variale in all jsp files