Include Page and Pass Parameter Through Request Object : Include « JSP « Java Tutorial






index.jsp

<%--
  Copyright (c) 2002 by Phil Hanna
  All rights reserved.
  
  You may study, use, modify, and distribute this
  software for any purpose provided that this
  copyright notice appears in all copies.
  
  This software is provided without warranty
  either expressed or implied.
--%>
<%
   // Diameter of the earth in kilometers

   int distance = 12756;
%>
<%@ page session="false" %>
<h4>Diameter of the Earth in SI (Metric) Units</h4>
<jsp:include page="ShowDiameter.jsp" flush="true">
   <jsp:param name="dist" value="<%= distance %>" />
   <jsp:param name="units" value="SI" />
</jsp:include>

<h4>Diameter of the Earth in U.S. Customary Units</h4>
<jsp:include page="ShowDiameter.jsp" flush="true">
   <jsp:param name="dist" value="<%= distance %>" />
   <jsp:param name="units" value="US" />
</jsp:include>

ShowDiameter.jsp

<%--
  Copyright (c) 2002 by Phil Hanna
  All rights reserved.
  
  You may study, use, modify, and distribute this
  software for any purpose provided that this
  copyright notice appears in all copies.
  
  This software is provided without warranty
  either expressed or implied.
--%>
<%@ page session="false"%>
<%
   String dist = request.getParameter("dist");
   if (dist == null)
      throw new ServletException
         ("No distance parameter specified");

   int kilometers = Integer.parseInt(dist);
   double miles = kilometers / 1.609344;

   String units = request.getParameter("units");
   if (units == null)
      throw new ServletException
         ("No units parameter specified");

   if (units.equals("SI")) {
   %> Diameter = <%= kilometers %> km <%
   }
   else {
   %> Diameter = <%= miles %> miles <%
   }
%>
  Download:  JSPIncludePageAndPassParameterThroughRequestObject.zip( 941 k)








23.37.Include
23.37.1.Include Page by Path
23.37.2.Include Page and Pass Parameter Through Request Object
23.37.3.Include File