0

I didn't have trouble with this before, and I actually have a working implementation with a different class. But for some reason, this example fails.

I have a class called InfoServlet.java:

@WebServlet("/info_servlet")
public class InfoServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      request.setAttribute("test", "hello world.");
      request.getRequestDispatcher("info_servlet.jsp").forward(request, response);
  }
}

And my jsp page info_servlet.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
  <head>
     <title>Test</title>
  </head>
  <body>
    <h1>HELLO</h1>
    <p><c:out value="${test}" /></p>
  </body>
</html>

Now when I go to localhost:8080/MySite/info_servlet.jsp my output is only

HELLO

For reference, I'm using tomcat 7 and servlet 3.0 in java. I didn't have trouble passing in an object so I'm unsure why all the sudden it won't let me display the value in info_servlet.jsp

Alex
  • 2,145
  • 6
  • 36
  • 72

1 Answers1

2

Everything works as expected. You are setting the "test" attribute within the servlet: if you go directly to the .jsp, bypassing the servlet, the attribute simply won't be set. Its value therefore will be null and the expression language will silently ignore it.

Angelo Oparah
  • 673
  • 1
  • 7
  • 18