Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - How to write response.sendRedirect() method with paramters in JSP

I have the following code in one of my 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 :

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. But I am not sure how to use the above parameters along with the jsp file name in the sendRedirect method.

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")
question from:https://stackoverflow.com/questions/65920628/how-to-write-response-sendredirect-method-with-paramters-in-jsp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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 dispatcher = request.getRequestDispatcher("someFile.jsp");
dispatcher.forward(request, response);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...