如何将对象放到Java servlet响应中?

时间:2011-11-13 03:59:36

标签: java servlets

在Java servlet中,我如何将对象置于servlet响应中?在下面的代码中,名称显示在页面上。

servlet:
private void process(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String name = "John";
        System.out.println("main servlet");
        request.setAttribute("name", name);
        response.sendRedirect("index.jsp");     
    }

index.jsp in JSTL
<body>
name: ${requestScope.name}
</body>

1 个答案:

答案 0 :(得分:3)

response.sendRedirect(...)替换为:

RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);