1

this is my code, it seems right to me! i don't know why it keeps saying this:

"Cannot instantiate the type Iterator"

this is the servlet:

package uges.servlets;


import jess.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Iterator;

public class Catalog extends BaseServlet {

public void doGet(HttpServletRequest request,
                   HttpServletResponse response)
    throws IOException, ServletException {
    checkInitialized();

    try {
        String customerId =
            (String) request.getParameter("customerId");
        if (customerId == null || customerId.length() == 0) {
            dispatch(request, response, "/index.html");
            return;
        }

        request.getSession().invalidate();
        HttpSession session = request.getSession();

        session.setAttribute("customerId", customerId);
        session.setAttribute("orderNumber",
                             String.valueOf(getNewOrderNumber()));

        ServletContext servletContext = getServletContext();
        Rete engine = (Rete) servletContext.getAttribute("engine");
        Iterator result =
            engine.runQuery("all-products", new ValueVector());
        request.setAttribute("queryResult", result);


    } catch (JessException je) {
        throw new ServletException(je);
    }

    dispatch(request, response, "/catalog.jsp");
}

this is the dispatch method, it 's in a servlet called BaseServlet:

protected void dispatch(HttpServletRequest request,
                        HttpServletResponse response,
                        String page)
    throws IOException, ServletException {

    ServletContext servletContext = getServletContext();
    RequestDispatcher dispatcher =
        servletContext.getRequestDispatcher(page);
    dispatcher.forward(request, response);
}

and this the JSP code:

<HTML>
<%@ page import="jess.*" %>
<jsp:useBean id="queryResult" class="java.util.Iterator" scope="request"/>

the error is about the java.util.Iterator in the class type of useBean tag!

the exception says : The value for the useBean class attribute java.util.Iterator is invalid

any help please ..

Thanks in advance!

4

2 回答 2

2

java.util.Iterator 是一个接口,而不是一个类。你要

<jsp:useBean id="queryResult" type="java.util.Iterator" scope="request"/>

为了确认这一点,我使用了以下测试代码:

package org.apache.markt.so;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Q001Servlet")
public class Q001Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        List<String> list = new ArrayList<String>();
        list.add("item1");
        list.add("item2");

        request.setAttribute("list", list.iterator());

        RequestDispatcher rd = request.getRequestDispatcher("/so/q001.jsp");
        rd.forward(request, response);
    }
}

使用以下 /so/q001.jsp

<html>
<jsp:useBean id="list" type="java.util.Iterator" scope="request" />
<body>
  <p><%=list.next()%></p>
</body>
</html>

这是使用最新开发分支中的 HEAD,但您会看到与最新 Tomcat 7 版本相同的结果。

于 2012-04-30T08:51:46.443 回答
0

这是一个演示我的想法的 JSP。

<%@ page import="java.util.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%
   //for testing
   List<String> engineList = new ArrayList<String>();
   engineList.add("zero");
   engineList.add("one");
   engineList.add("two");
   engineList.add("three");
   Iterator<String> result = engineList.iterator();
   // create a list from engine result
   List<String> list = new ArrayList<String>();
   while(result.hasNext())list.add(result.next());
   request.setAttribute("list", list);
%>
<html>
<body>
Complete list is${list}
The first item is ${list[0]}. 
The second item is ${list[1]}. 
If you don't know how many items there are in result, then use JSTL 
<c:forEach var="item" items="${list}" >
        ${item}
</c:forEach>
</body>
</html>
于 2012-05-01T02:24:38.077 回答