Saturday 14 March 2015

jsp:forward : Forward requests


By using <jsp:forward> tag we can forward the requests from one jsp page to other jsp page or servlet.

Syntax
<jsp:forward page="page" />

Whenever you forward the request to other page using <jsp:forward> action tag, then new page has access to the request and response objects.

common.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Area of Circle</title>
    </head>
    <body>
        <%!
            final double PI = 3.14;

            double getArea(int r){
                return PI*r*r;
            }
        %>
        <%
            int radius = Integer.parseInt(request.getParameter("radius"));
        %>

        <h1>Area Of the circle is
            <%= getArea(radius)%>
        </h1>

</body>


circle.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Area Of Circle</title>
    </head>
    <body>
        <jsp:forward page="common.jsp" />
    </body>
</html>


index.html
<!DOCTYPE html>

<html>
    <head>
        <title>Area Of Circle</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form method="get" action="circle.jsp">
            Enter radius <input type="text" name="radius" required="required"/><br />
            <input type="submit" />
        </form>
    </body>
</html>


Output

<jsp:forward page="common.jsp" />
Above statement forward the request to 'common.jsp', It is the common.jsp responsibility to process the request and sent the response back to client.

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment