简体   繁体   English

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

[英]Pass Hidden parameters using response.sendRedirect()

How would I pass hidden parameters?我将如何传递隐藏参数? I want to call a page (test.jsp) but also pass 2 hidden parameters like a post.我想调用一个页面(test.jsp),但也像一个帖子一样传递 2 个隐藏参数。

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

TheNewIdiot's answer successfully explains the problem and the reason why you can't send attributes in request through a redirect. TheNewIdiot 的回答成功地解释了问题以及您无法通过重定向在请求中发送属性的原因。 Possible solutions:可能的解决方案:

  1. Using forwarding.使用转发。 This will enable that request attributes could be passed to the view and you can use them in form of ServletRequest#getAttribute or by using Expression Language and JSTL .这将使请求属性可以传递给视图,您可以以ServletRequest#getAttribute形式或通过使用表达式语言JSTL使用它们。 Short example (reusing TheNewIdiot's answer] code).简短示例(重用 TheNewIdiot 的答案] 代码)。

    Controller (your servlet)控制器(您的 servlet)

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

    View (your JSP)查看(您的 JSP)

    Using scriptlets:使用脚本:

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

    This is just for information purposes .这仅供参考 Scriptlets usage must be avoided : How to avoid Java code in JSP files?必须避免使用 Scriptlets:如何避免 JSP 文件中的 Java 代码? . . Below there is the example using EL and JSTL.下面是使用 EL 和 JSTL 的示例。

     <c:out value="${message}" />
  2. If you can't use forwarding (because you don't like it or you don't feel it that way or because you must use a redirect) then an option would be saving a message as a session attribute, then redirect to your view, recover the session attribute in your view and remove it from session.如果您不能使用转发(因为您不喜欢它或您不喜欢它或因为您必须使用重定向),则可以选择将消息保存为会话属性,然后重定向到您的视图,恢复视图中的会话属性并将其从会话中删除 Remember to always have your user session with only relevant data.请记住始终让您的用户会话仅包含相关数据。 Code example代码示例

    Controller控制器

    //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");

    View看法

    Again, showing this using scriptlets and then EL + JSTL:同样,使用 scriptlet 和 EL + JSTL 显示:

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

Generally, you cannot send a POST request using sendRedirect() method.通常,您不能使用sendRedirect()方法发送 POST 请求。 You can use RequestDispatcher to forward() requests with parameters within the same web application, same context.您可以使用RequestDispatcher在同一 Web 应用程序、同一上下文中使用参数转发()请求。

RequestDispatcher dispatcher = servletContext().getRequestDispatcher("test.jsp");
dispatcher.forward(request, response);

The HTTP spec states that all redirects must be in the form of a GET (or HEAD). HTTP 规范规定所有重定向都必须采用 GET(或 HEAD)的形式。 You can consider encrypting your query string parameters if security is an issue.如果存在安全问题,您可以考虑加密查询字符串参数。 Another way is you can POST to the target by having a hidden form with method POST and submitting it with javascript when the page is loaded.另一种方法是您可以通过具有带有方法 POST 的隐藏表单并在页面加载时使用 javascript 提交它来 POST 到目标。

Using session , I successfully passed a parameter ( name ) from servlet #1 to servlet #2, using response.sendRedirect in servlet #1.使用session ,我成功地将参数( name )从 servlet #1 传递到了 servlet #2,使用了 servlet #1 中的response.sendRedirect Servlet #1 code: 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");

In Servlet #2, you don't need to get name back.在 Servlet #2 中,您不需要取回name It's already connected to the session.它已经连接到会话。 You could do String name = (String) request.getSession().getAttribute("name");你可以做String name = (String) request.getSession().getAttribute("name"); ---but you don't need this. ---但你不需要这个。

If Servlet #2 calls a JSP, you can show name this way on the JSP webpage:如果 Servlet #2 调用 JSP,您可以在 JSP 网页上name这种方式显示name

<h1>Welcome ${name}</h1>

To send a variable value through URL in response.sendRedirect().在 response.sendRedirect() 中通过 URL 发送变量值。 I have used it for one variable, you can also use it for two variable by proper concatenation.我已经将它用于一个变量,您也可以通过适当的连接将它用于两个变量。

String value="xyz";字符串值="xyz";

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

用这个:

out.println("<script>window.location.href = \"hoadon?OrderId=" + getIdOrder + "\"\n</script>>");

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

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