File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access a variale in all jsp files

 
Joao Sitoe
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place the variable in application scope (via servletContext.setAttribute()).
 
Joao Sitoe
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already gave you the method in ServletContext: see here
 
Joao Sitoe
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it, the error was caused by remains of previous attempts :)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic