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,082,665
  • 372
  • 3,610
  • 3,555
James
  • 3,957
  • 4
  • 37
  • 82
  • 2
    It's a long time since I used jsp's, but don't you need to include the jstl library in the jsp? `<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>` – DaveH Feb 08 '17 at 13:42
  • 1
    see http://stackoverflow.com/questions/4912690/how-to-access-at-request-attributes-in-jsp – chaim Feb 08 '17 at 13:48
  • A combination of both of your suggestions fixed the problem – James Feb 08 '17 at 13:51

2 Answers2

0

Maybe something like this will work?

    <c:forEach items="${intList}" var="item">
        <c:out value="${item}"/>
    </c:forEach>
theodosis
  • 79
  • 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,957
  • 4
  • 37
  • 82