5

After an hour of solid research I still can't do this.

This is my Servlet code:

package com.fdm.ProjectWeb.RedirectServlets;

import java.awt.List;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.sql.ResultSupport;

import com.fdm.ProjectWeb.Controller.ValidateRegisterInputController;
import com.fdm.ProjectWeb.Model.OraclePullListOfUsers;
import com.fdm.ProjectWeb.Model.OracleUserManagement;

public class VerifyRedirect extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        OraclePullListOfUsers pull = new OraclePullListOfUsers();
        ResultSet rs = pull.unverifiedUsers();
        List list = new List();

    try {
        while (rs.next()){
            list.add(rs.getString(1));
    }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        req.setAttribute("list", list);
        RequestDispatcher rd = req.getRequestDispatcher("./WEB-INF/VerifyUser.jsp");
        rd.forward(req, resp);
    }
}

And this is my .JSP code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Verify Users</title>
</head>
<body>

<table>
  <c:forEach items="${list}" var="item">
    <tr>
      <td><c:out value="${item}" /></td>
    </tr>
  </c:forEach>
</table>

    <h2>Please enter the Username of the user you want to verify</h2>
    <form action="loginform" method="POST">
        <label>User To Verify: <input type="text" name="userToVerify" id="userToVerify" /></label><br />
        <input type="submit" value="Submit" name="submit" />
    </form>

</body>

The Result Set definitely has data in it as if I system.out.println in the while loop it shows all the right values.

And I get this error message:

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;

Any help would be appreciated!

DeaIss
  • 2,525
  • 7
  • 27
  • 37
  • Possible duplicate of [How to Display List Contents in tabular format in a JSP file?](https://stackoverflow.com/questions/8646347/how-to-display-list-contents-in-tabular-format-in-a-jsp-file) – Nayantara Jeyaraj Oct 21 '17 at 10:46

3 Answers3

9

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

This exception occur when your <c:forEach items> does not refer to an Object, that can be iterated upon. The Object should either be Iterable, a Map, or an array.
So clearly, your list attribute refers to the type which does not come under any of the above category. Though the type is actually a List, but not java.util.List.

Check your import statement:

import java.awt.List;   // Here is the fault

It should be:

import java.util.List;

Also, you should use generic type List instead of raw type. Change:

List list = new List();

to:

List<String> list = new List<String>();

Also, it seems like you are doing the pre-processing task in doPost() method. Don't. doPost() is used for post-processing a request. You should use doget() method for pre-processing.

Move all your code in doPost() to doGet() method.

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Thanks a lot man (and to everyone else). Especially for the doGet tip :) – DeaIss Aug 06 '13 at 21:10
  • One more quick question! What if I want to send the information the user entered as a parameter to another servlet - do I still use doGet() instead of doPost() I'm reading the page you reccommend as we speak! – DeaIss Aug 06 '13 at 21:13
  • 1
    @oOTesterOo. No, that is what we call post-processing a request. We use `doPost` for that. – Rohit Jain Aug 06 '13 at 21:14
2

Change:

List list = new List();

To:

List<String> list = new ArrayList<String>();

From

java.util.List;
Benoit Wickramarachi
  • 6,096
  • 5
  • 36
  • 46
1

try instantiating the list like this:

List<String> list = new ArrayList<String>();

or if it does not help maybe supply the list as an array such as:

req.setAttribute("list", list.toArray());
benny
  • 11
  • 1