0

I'm learning how to use Java Servlets and I've setup a toy example.

I have a servlet with the following logic in the doGet method:


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

        //HttpSession session = request.getSession();
        List theList = new ArrayList();
        theList.add(1);
        theList.add(2);
        theList.add(3);
        request.setAttribute("intList", theList);

        RequestDispatcher dispatcher = request.getRequestDispatcher("hello.jsp");        
        dispatcher.forward(request, response);
    }

Then I have the following code in hello.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>

<h1>Redirect Worked</h1>
<c:forEach items="${intList}" var="item">
    ${item}<br>
</c:forEach>
</body>
</html>

I expect my browser to show:

Redirect Worked
1
2
3

But all I see is:

Redirect Worked

What am I doing wrong?

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
James
  • 3,759
  • 3
  • 33
  • 73

2 Answers2

0

Maybe something like this will work?

    <c:forEach items="${intList}" var="item">
        <c:out value="${item}"/>
    </c:forEach>
theodosis
  • 69
  • 1
  • 1
  • 13
0

Thanks to DaveH, theodosis and Chaim I fixed my JSP file:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>redirect worked!</h1>
    <c:forEach var="item" items="${requestScope.intList}">
        <c:out value="${item}"/>
    </c:forEach>

</body>
</html>
James
  • 3,759
  • 3
  • 33
  • 73