Saturday, 14 March 2015

Implicit objects available to included jsp page


As already discussed, if you include a page using <jsp:include> action tag, then the implicit objects available to this page are also available to included page also.

common.jsp
<%!
    final double PI = 3.14;
    
    double getArea(int r){
        return PI*r*r;
    }
%>

<h1>
    Request comes from <%= request.getRequestURI() %>
</h1>
<%
    int radius = Integer.parseInt(request.getParameter("r"));
    String msg = request.getParameter("message");
%>

<h1><%=msg%>
    <%= getArea(radius)%>
</h1>


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:include page="common.jsp" flush="true">
            <jsp:param name="message" value="Area of the Circle is :" />
            <jsp:param name="r" value="<%=radius%>" />
        </jsp:include>
    </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

'common.jsp' page includes below statement.

Request comes from <%= request.getRequestURI() %>

As you observe the output screen, it displays message like 'Request comes from /jsp/circle.jsp', since included page uses the same request object as its including page.

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment