[Jweb] JSP-编程 03 静态, 动态包含

 JSP-静态,动态包含
  @3-1  静态包含

       TestBar.jsp  TitleBar.jsp  两个合在一起生成一个servlet class, 
       但是 TitleBar.jsp(被包含页面)这样无法传参数,因为编译之前传参数无意义。
  @3-2  动态包含
       被包含的 request 对象比包含的 request 对象能取出的东西还要多点。分别产生两个class可以传参数,怎么传,怎么取都可以。
                   包含页面 : date.jsp 源码如下 :

 <%@ page import="java.util.*" %>
 <%= (new Date()).toLocaleString() %>
 <%= request.getParameter("user") %>
    (1), 被包含页面 可以取得 包含页面(主页面)传递的参数 http://127.0.0.1:8888/my/include/include.jsp?user=456789
    (2), 包含页面与被包含页面的 request 是不同的。不是同一对象。虽然不是一个对象,但是可以看成是。设计成这样,是因为为了Action可以传递参数.
    (3), include.jsp 中 <jsp:include page="date.jsp?dept=80" flush="true"/>date.jsp 中可以把 dept 取出来。
    在包含页面传递的参数,被包含页面也可以将其取出。静态包含则不允许。
总结 : 只要当前页面能取到的参数,date.jsp页面也可以取到。包含页面传的参数下一页面(date.jsp)还能取到。
       被包含的页面,request对象要大一些。

include.jsp

<html>
<head>
<title> include test</title>
</head>
<body bgcolor="white">
<font color="red">
The current date and time are 

<jsp:include page="date.jsp?dept=80" flush="true"/>
</font>
</body>
</html>

Compute.html

<html>
<head>
<title>Compute</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF">
<div align="center">
  <form method="post" action="Compute.jsp">
    <p>选择要做的运算
      <input type="radio" name="compute" value="division" checked>
      除法
      <input type="radio" name="compute" value="multiplication">
      乘法</p>
    <p>被除数(被乘数)
      <input type="text" name="value1">
      除数(乘数)
      <input type="text" name="value2">
    </p>
    <p>
      <input type="submit" name="Submit" value="计算结果">
    </p>
  </form>
</div>
</body>
</html>
Compute.jsp
<%@ page language="java" %>

<%
    String value1 = request.getParameter("value1");
    String value2 = request.getParameter("value2");
%>

<% if( request.getParameter("compute").equals("division") ) { %>
    <jsp:include page="divide.jsp" flush="true">
        <jsp:param name="v1" value="<%=value1%>"/>
        <jsp:param name="v2" value="<%=value2%>"/>
    </jsp:include>
<% } else { %>
    <%@ include file="multiply.jsp"%>
<% } %> 
divide.jsp
<html>
<head>
<title>Divide</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<center>
<h1>
<% 
    try{
        float dividend = Float.parseFloat( request.getParameter( "value1" ) );
        float divisor = Float.parseFloat( request.getParameter( "value2" ) );
        double result=dividend/divisor;
        %>
        <%= result%>
        <%
        //out.println( dividend + " / " + divisor + " = " + result );
    }
    catch( Exception ex ){
        out.println( "不合法的被乘数或除数!" );
    }
%>   
</h1>
</center>   
</body>
</html>
multiply.jsp
<html>
<head>
<title>Devide</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<center>
<h1>
<% 
	try{
	//System.out.println(request.getParameter("v1"));
			float multiplicand = Float.parseFloat( request.getParameter( "value1" ) );
   		float multiplicator = Float.parseFloat( request.getParameter( "value2" ) );
   		double result=multiplicand*multiplicator;
   		out.println( multiplicand + " * " + multiplicator + " = " + result );
   	}
   	catch( Exception ex ){
   		out.println( "不合法的被乘数或乘数!" );
   	}
%>   
</h1>
</center>	
</body>
</html>



原文地址:https://www.cnblogs.com/robbychan/p/3786863.html