Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward the request to another JSP page upon click of a link in a JSP page?

Tags:

java

jsp

servlets

I have a link in the jsp page, upon the link click, how can I forward the request to another jsp page.

like image 726
John Smith Avatar asked Jan 05 '11 19:01

John Smith


People also ask

How do I forward a JSP request to another JSP?

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.

How will you perform a browser redirect from a JSP page?

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."


1 Answers

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 :

  • Servlet
like image 95
jmj Avatar answered Oct 19 '22 23:10

jmj