Saturday 14 March 2015

jsp:forward : pass parameters to the new JSP : pass parameters to the new JSP


Just like <jsp:include> we can pass parameters to the new jsp file using <jsp:forward> action tag.
Synatx
<jsp:forward page="page">
<jsp:param name="paranName1" value="paramValue1" />
<jsp:param name="paramName2" value="paranValue2" />
</jsp:forward>
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("r"));
            String msg = request.getParameter("message");
        %>

        <h1><%= msg %>
            <%= 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>
        <%
            int radius = Integer.parseInt(request.getParameter("radius"));
        %>
        <jsp:forward page="common.jsp">
            <jsp:param name="message" value="Area of the Circle is :" />
            <jsp:param name="r" value="<%=radius%>" />
        </jsp:forward>
    </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">
      <jsp:param name="message" value="Area of the Circle is :" />
      <jsp:param name="r" value="<%=radius%>" />
</jsp:forward>

Above statements pass the parameters 'message' and 'r' to the page 'common.jsp'.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment