简体   繁体   English

如何使用 JSP 中的参数编写 response.sendRedirect() 方法

[英]How to write response.sendRedirect() method with paramters in JSP

I have the following code in one of my JSP:我的 JSP 之一中有以下代码:

<c:redirect url="someFile.jsp">
    <c:param name="source" value="${param.source}" />
    <c:param name="target" value="${param.target}" />
    <c:param name="value" value="${param.value}" />
</c:redirect>

The requested JSP [ someFile.jsp ] access the parameters by the below way:请求的 JSP [ someFile.jsp ] 通过以下方式访问参数:

String source = request.getParameter("source");
String target = request.getParameter("target");
String value  = request.getParameter("value");

But instead of <c:redirect url I need to use response.sendRedirect("someFile.jsp") method.但不是<c:redirect url我需要使用response.sendRedirect("someFile.jsp")方法。 But I am not sure how to use the above parameters along with the jsp file name in the sendRedirect method.但我不确定如何在 sendRedirect 方法中使用上述参数以及 jsp 文件名。

Can someone help me out how should I write my redirect method.Can we do something like this:有人可以帮我看看我应该如何编写我的重定向方法。我们可以这样做:

response.sendRedirect("someFile.jsp?source=" + source +  "?target=" +target + "?value=" +value); 

OR或者

session.setAttribute("source",source)
session.setAttribute("target",target)
session.setAttribute("value",value)

response.sendRedirect("someFile.jsp")

You can not do it using response.sendRedirect because response.sendRedirect simply asks the browser to send a new request to the specified path which means the new request won't get any information from the old request.您不能使用response.sendRedirect来做到这一点,因为response.sendRedirect只是要求浏览器向指定路径发送请求,这意味着新请求不会从旧请求中获取任何信息。

You can use do it using RequestDispatcher#forward which forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.您可以使用RequestDispatcher#forward将请求从 servlet 转发到服务器上的另一个资源(servlet、JSP 文件或 HTML 文件)。

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

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

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