使用response.sendRedirect()传递隐藏参数

28

我该如何传递隐藏参数?我想调用一个页面(test.jsp),但是也想像post一样传递2个隐藏参数。

response.sendRedirect("/content/test.jsp");

@informatik01 我的回答涵盖了你所提到的内容,除了我的回答还添加了使用JSTL删除会话属性的视图部分。 - Luiggi Mendoza
@LuiggiMendoza 没关系,老铁,我会删除我的评论以确保你没事。顺便说一下,那个答案是大约5个月前发布的。 - informatik01
1
@informatik01不知道你的帖子,但看起来我们(以及世界各地的其他人)都知道如何处理这个问题。 - Luiggi Mendoza
5个回答

55

TheNewIdiot的答案成功解释了这个问题以及为什么在重定向时无法发送请求属性。可能的解决方案:

  1. 使用转发。这将使请求属性能够传递到视图中,您可以通过ServletRequest#getAttribute或使用表达式语言JSTL来使用它们。下面是一个简短的示例(重用TheNewIdiot的代码)。

    控制器(您的servlet)

    request.setAttribute("message", "Hello world");
    RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url);
    dispatcher.forward(request, response);
    

    查看(您的JSP)

    使用脚本片段:

    <%
        out.println(request.getAttribute("message"));
    %>
    

    这仅供信息目的。 应避免使用脚本片段:如何避免在JSP文件中使用Java代码? 下面是使用EL和JSTL的示例。

    <c:out value="${message}" />
    
  2. 如果您不能使用转发(因为您不喜欢它或者您觉得不舒服,或者您必须使用重定向),那么一个选项是将消息保存为会话属性,然后重定向到您的视图,在视图中恢复会话属性并从会话中移除它。记住始终只保留与用户相关的数据。 代码示例

    控制器

  3. //if request is not from HttpServletRequest, you should do a typecast before
    HttpSession session = request.getSession(false);
    //save message in session
    session.setAttribute("helloWorld", "Hello world");
    response.sendRedirect("/content/test.jsp");
    

    视图

    再次展示使用脚本片段以及EL + JSTL:

    <%
        out.println(session.getAttribute("message"));
        session.removeAttribute("message");
    %>
    
    <c:out value="${sessionScope.message}" />
    <c:remove var="message" scope="session" />
    

6
一般情况下,您不能使用sendRedirect()方法发送POST请求。您可以使用RequestDispatcher将带有参数的请求在同一个Web应用程序、同一个上下文中进行forward()
RequestDispatcher dispatcher = servletContext().getRequestDispatcher("test.jsp");
dispatcher.forward(request, response);

HTTP规范规定所有重定向必须采用GET(或HEAD)形式。如果安全性是一个问题,可以考虑加密查询字符串参数。另一种方法是,可以通过具有POST方法的隐藏表单提交到目标,并在页面加载时使用JavaScript提交。

@AshishAnand 请看我的编辑答案,您可以在同一个Web应用程序中使用RequestDispatcher - AllTooSir
1
应该是 getServletContext().getRequestDispatcher(...) 吗? - access_granted

1

使用session,我成功地从servlet #1传递了一个参数(name)到servlet #2,在servlet #1中使用response.sendRedirect。Servlet #1代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    String name = request.getParameter("name");
    String password = request.getParameter("password");
    ...
    request.getSession().setAttribute("name", name);
    response.sendRedirect("/todo.do");

在Servlet #2中,您不需要获取name。它已经连接到会话。您可以执行String name = (String) request.getSession().getAttribute("name"); ---但您不需要这样做。

如果Servlet #2调用JSP,则可以通过以下方式在JSP网页上显示name

<h1>欢迎 ${name}</h1>


-2
使用这个:
out.println("<script>window.location.href = \"hoadon?OrderId=" + getIdOrder + "\"\n</script>>");

1
感谢您提供这段代码片段,它可能会提供一些有限的、即时的帮助。一个适当的解释将极大地提高其长期价值,因为它可以展示为什么这是一个好的问题解决方案,并使其对未来读者有其他类似问题的人更有用。请[编辑]您的答案以添加一些解释,包括您所做的假设。 - helvete

-2

在response.sendRedirect()中通过URL发送变量值。我已经用它来处理一个变量,你也可以通过适当的连接将其用于两个变量。

字符串值="xyz";

response.sendRedirect("/content/test.jsp?var="+value);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接