简体   繁体   English

如何将对象传递给Java servlet响应?

[英]how to put the object to Java servlet response?

In a Java servlet, how might I put the object to servlet response? 在Java servlet中,我如何将对象放到servlet响应中? In the code below, the name is shown on the page. 在下面的代码中,名称显示在页面上。

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>

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM