I have a link in the jsp page, upon the link click, how can I forward the request to another jsp page.
To forward a request from one page to another JSP page we can use the <jsp:forward> action. This action has a page attribute where we can specify the target page of the forward action. If we want to pass parameter to another page we can include a <jsp:param> in the forward action.
For redirecting a page in JSP, we are calling response. sendRedirect(). By using this method, the server returns the response to the client, from where the next request comes, and it displays that URL. In order to redirect the browser to a different resource, we use the implicit object "response."
If you just want to GET a new jsp then simply
<a href="/jsp/newJsp.jsp">Click Here</a>
Note: the path to jsp will start from /
the public web space the same dir where WEB-INF
resides
if you mean forward
then
Upon click you will perform GET operation , So lets say
you click
<a href="/yourApp/ForwardServlet/">Click Here</a>
make a Servlet entry in web.xml and map it to /ForwardServlet
to ForwardServlet
and in Servlet perform
public class ForwardServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String destination = "/WEB-INF/pages/result.jsp";
RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);
}
}
Refer :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With