0

I'm new to Java server programming, and I'm trying to use Google app engine.

The following code is in a servlet:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    req.setAttribute("message", "Hi from servlet!");
    req.getRequestDispatcher("/my_page.jsp").forward(req, resp);
}

And the following code is in my_page.jsp:

<%= request.getAttribute("message") %>

Where I would expect to see Hi from servlet! on the resulting page, I see null.

(If I try using ${message}, I get no output at all)

What is the correct way to get data from a servlet to a JSP?

andypaxo
  • 6,171
  • 3
  • 38
  • 53

1 Answers1

1

You need to let the request URL (the one as you see in browser's address bar) point to an URL which matches the URL pattern of the servlet as configured in web.xml, not to the URL of the JSP file itself. Best would be to put the JSP in /WEB-INF folder so that you can't "accidently" invoke it without invoking the servlet.

By the way, the ${message} is the correct way and should be preferred over old fashioned scriptlets.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555