includetag.java
来自「jakarta-taglibs」· Java 代码 · 共 131 行
JAVA
131 行
/*
* Copyright 1999,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.
*/
package org.apache.taglibs.xsl;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
/**
* Include the contents of the specified page at this point in our output.
* This tag is similar to <code><jsp:include></code>, but does not
* cause the output to be sent directly to the servlet response. Therefore,
* it can be used to capture the content of the page as body content of
* a surrounding tag in which we are nested.
*
* @author Craig R. McClanahan
* @version $Revision: 1.2 $ $Date: 2004/03/08 02:41:31 $
*/
public class IncludeTag extends TagSupport {
// ------------------------------------------------------------- Properties
/**
* The URI of the page or servlet to be included.
*/
private String page = null;
public String getPage() {
return (this.page);
}
public void setPage(String page) {
this.page = page;
}
// --------------------------------------------------------- Public Methods
/**
* Include the specified page or servlet's output at this point.
*
* @exception JspException if a JSP error occurs
*/
public int doStartTag() throws JspException {
// Validate the format of the "page" attribute
// FIXME - deal with relative URIs like <jsp:include> does
if (!page.startsWith("/"))
throw new JspException("Page value must start with '/'");
// Set up the output stream to which we will write
JspWriter out = pageContext.getOut();
// Set up a URLConnection to read the requested page. We cannot use
// PageContext.include() because it writes directly to the output
// stream that goes back to the client. :-(
HttpServletRequest request =
(HttpServletRequest) pageContext.getRequest();
StringBuffer url = new StringBuffer();
url.append(request.getScheme());
url.append("://");
url.append(request.getServerName());
if (request.getServerPort() != 80) {
url.append(':');
url.append(request.getServerPort());
}
if (request.getContextPath() != null)
url.append(request.getContextPath());
url.append(page); // FIXME - deal with relative page references
URLConnection conn = null;
try {
conn = (new URL(url.toString())).openConnection();
conn.setAllowUserInteraction(false);
conn.setDoInput(true);
conn.setDoOutput(false);
conn.connect();
} catch (Exception e) {
throw new JspException("Error opening connection: " +
e.toString());
}
// Copy the contents of this URL
try {
BufferedInputStream is =
new BufferedInputStream(conn.getInputStream());
InputStreamReader in = new InputStreamReader(is);
while (true) {
int ch = in.read();
if (ch < 0)
break;
out.write(ch);
}
} catch (Exception e) {
throw new JspException("Error reading connection: " +
e.toString());
}
// Skip the body of this tag (which should be empty anyway)
return (SKIP_BODY);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?