在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>
答案 0 :(得分:3)
将response.sendRedirect(...)
替换为:
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);