I am trying to print value of a function variable in JSP Page. The function variable is located in java.class(com.project.bulk). File name is bulk.class. I tried reading the variable by writing below code in JSP file and it didn’t work. Any help please?
<%@ page import="com.project.bulk.bulk" %>
<%=bulk.cellStoreVector %>
// function code is below
private static void printCellDataToConsole(Vector dataHolder) {
for (int i = 0; i < dataHolder.size(); i++) {
Vector cellStoreVector = (Vector) dataHolder.elementAt(i);
System.out.println(cellStoreVector);
for (int j = 0; j < cellStoreVector.size(); j++) {
HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j);
String stringCellValue = myCell.toString();
// System.out.print(stringCellValue + "\t\t");
}
System.out.println();
}
}
You can’t access a local variable outside that method or the block in which it is defined. The scope of local variable is confined in the block where it is defined.
Your below declaration is local to the
for-loopin which it is declared. Even in the current method, it will not be accessible outside thefor-loop. Because your loop defined ascopeof access for this variable: –To access it in a
JSPoutside yourclass, declare that field as a private instance variable in your class. And have apublicaccessor method, that will return the value of that field. Then in your JSP, you can invoke that method to get the value for a particular instance.Remember, you need to access that method on an
instanceof your class. You are accessing here directly through yourclass name. If you want to access it like that, you need astaticvariable.Here’s a simple example covering what all I said above: –