0

I am trying to pass values from the servlet to the JSP file. I already confirmed that the data is going from the JSP to the Servlet, but not the other way around. Here is the Java snippet

//Here is where I get a List<String> of the column names
List<String> columns = DataAccess.getColumns(query);

//Turn the List<String> into a 1D array of Strings
for(int i = 0 ; i < numArrays ; i++)
    request.setAttribute("rows["+i+"]", rows[i]);

//Set the attribute to the key "columns"
request.setAttribute("columns", arColumns);

//Launch result.jsp
request.getRequestDispatcher("result.jsp").forward(request, response);

There I am expecting to have the 1D array of Strings linked to the key "columns". When I get it from the JSP file, I get null. Here is how I retrieve it and confirm that it is null:

<%  String[] columns = (String[])request.getParameterValues("columns"); 
    if(columns == null){
        System.out.print("columns is null\n");
    }
    int colNum = columns.length; //How many columns we have
%>

In Eclipse, when I run the code I get the string "columns is null" ont he console, followed by a NullPointerException when I tried to get the length of columns.

I confirmed that arColumns is not null in the java file, it does print the column headers when I try to print them to console.

What am I doing wrong here?

Thank you for your help.

Sammy I.
  • 2,523
  • 4
  • 29
  • 49

2 Answers2

1
String[] columns = (String[]) request.getAttribute("columns"); 

getParameterValues() is typically used when you're using HTML checkboxes.

<form action="someServlet" method="POST">
<input type="checkbox" name="delete" value="1">
<input type="checkbox" name="delete" value="2">
<input type="checkbox" name="delete" value="3">
</form>

//in someServlet:
String[] itemsToBeDeleted = request.getParameterValues("delete");

for(String s : itemsToBeDeleted) {
    System.out.println(s); //prints 1,2,3 if they're checked
 }
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
zmf
  • 9,095
  • 2
  • 26
  • 28
0

I believe you should instead try:

String[] columns = (String[]) request.getAttribute("columns"); 
CubeJockey
  • 2,209
  • 8
  • 24
  • 31