Java/JSP/Include
Содержание
Forward a request
// getrequest.jsp
anything here will now appear in the browser
<jsp:forward page="gotForwardedRequest.jsp"/>
anything here will not appear
//File: gotForwardedRequest.jsp
<html>
<head><title>Request forwarded here</title></head>
<body>
This page received a forwarded request from <b>getRequest.jsp</b>,
<br>This page is the output from <b>gotForwardedRequest.jsp</b>,
<br>but the URL is for <b>getRequest.jsp</b>.
</body>
</html>
Include another file
<html>
<HEAD><TITLE>Include another file</TITLE></HEAD>
<BODY>
<%@ include file="welcomeText.html" %>
<P>
This text appears after the included file
</BODY>
</HTML>
// File:welcomeText.html
<TABLE WIDTH="100%" border="0">
<TR><TD align="right" bgcolor="#FFFF99"><i><b>Welcome to your online account</b></i></TD></TR>
</TABLE>
JSP Include
//File: index.html
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>Welcome to JSP</title>
</head>
<body>
<table width="100%">
<tr>
<td>
<%@ include file="titlebar.jsp" %>
</td>
</tr>
<tr>
<td>
<%
out.println("<center><b>This is the client area.</b></center>");
%>
</td>
</tr>
</table>
</body>
</html>
//////////////////////////////////////////////////////////////
//File: titleBar.jsp
<table>
<tr>
<td>
<%
// Get the User"s Name from the session
out.println("<b>Hello: " + request.getParameter("user") + "</b>");
%>
</td>
</tr>
</table>
Jsp Include Static HTML
//File: includeTest.jsp
<HTML>
<HEAD><TITLE> Include Test </TITLE></HEAD>
<BODY>
I should see this <br>
<%@ include file="insert.html" %>
<br><br>
If I don"t, then the URL is not working...
</BODY>
</HTML>
/////////////////////////////////////////////
//File: insert.html
right below the line that reads: "I should see this"
Jsp page includes another Jsp page
//File: index.jsp
<html>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<body bgcolor="white">
<font color="red">
<%@ page buffer="5kb" autoFlush="false" %>
<p>In place evaluation of another JSP which gives you the current time:
<%@ include file="foo.jsp" %>
<p> <jsp:include page="foo.html" flush="true"/> by including the output of another JSP:
<jsp:include page="foo.jsp" flush="true"/>
:-)
</html>
////////////////////////////////////////////////////////////////////
//File: foo.jsp
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<body bgcolor="white">
<font color="red">
<%= System.currentTimeMillis() %>
////////////////////////////////////////////////////////////////////
//File: foo.html
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
To get the current time in ms
JSP standard action: include
/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/
Passing parameters during including
<html>
<head>
<title>This page passes parameters</title>
</head>
<body>
<!-- header from include-->
<jsp:include page="includeFileNeedingAParameter.jsp">
<jsp:param name="department" value="Electrical"/>
</jsp:include>
Above text is from includeFileNeedingAParameter.jsp.
</body>
</html>
//File : includeFileNeedingAParameter.jsp
<!-- the included file -->
<h1><%=request.getParameter("department") %> Department</h1>
Using Include And Pass Value
//File: index.jsp
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>Employee Information</title>
</head>
<body>
<table width="100%" cellspacing="0">
<tr>
<td>
<jsp:include page="header.jsp" flush="true">
<jsp:param name="employee" value="Bob"/>
<jsp:param name="title" value="Engineer"/>
</jsp:include>
</td>
</tr>
</body>
</html>
///////////////////////////////////////////////////////////////
//File: header.jsp
<%
out.println("<b>Employee: </b>" +
request.getParameter("employee"));
out.println("<br><b>Title: </b>" +
request.getParameter("title"));
%>
Using Includes
<html>
<head>
<title>Using Includes</title>
</head>
<body>
<!-- header page inserted here -->
<jsp:include page="tableheader.html" flush="true"/>
<!-- main content inserted here-->
<jsp:include page="maincontent.jsp" flush="true"/>
<!-- insert the footer here -->
<jsp:include page="tablefooter.html" flush="true"/>
</body>
</html>