jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.cgmcc.com/jsp/readfile" prefix="my3"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tagreadfile.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<my3:readFile file="/WEB-INF/tld/readfile.tld"/>
</body>
</html>
java
package com.cgm.readFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
public class TagReadFile implements SimpleTag {
private String file;
private PageContext pageContext;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public void doTag() throws JspException, IOException { //可以动态生成jsp啊..
//处理逻辑
//开始读文件.
System.out.println("-----------------"+file);
InputStream in= pageContext.getServletContext().getResourceAsStream(file);
Scanner scanner=new Scanner(in,"UTF-8");
while (scanner.hasNext()) {
String line=scanner.nextLine();
line=line.replace("<", "<");
line=line.replace(">", ">");
pageContext.getOut().print(line);
pageContext.getOut().println("</br>"); //换行
}
}
public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}
public void setJspBody(JspFragment jspBody) {
}
public void setJspContext(JspContext pc) {
this.pageContext=(PageContext) pc;
}
public void setParent(JspTag parent) {
}
}
tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.3</tlib-version>
<short-name>my3</short-name>
<uri>http://www.cgmcc.com/jsp/readfile</uri>
<tag>
<name>readFile</name>
<tag-class>com.cgm.readFile.TagReadFile</tag-class>
<body-content>empty</body-content>
<attribute>
<name>file</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.cgmcc.com/jsp/readfile" prefix="my3"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tagreadfile.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<my3:readFile file="/WEB-INF/tld/readfile.tld"/>
</body>
</html>
java
package com.cgm.readFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
public class TagReadFile implements SimpleTag {
private String file;
private PageContext pageContext;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public void doTag() throws JspException, IOException { //可以动态生成jsp啊..
//处理逻辑
//开始读文件.
System.out.println("-----------------"+file);
InputStream in= pageContext.getServletContext().getResourceAsStream(file);
Scanner scanner=new Scanner(in,"UTF-8");
while (scanner.hasNext()) {
String line=scanner.nextLine();
line=line.replace("<", "<");
line=line.replace(">", ">");
pageContext.getOut().print(line);
pageContext.getOut().println("</br>"); //换行
}
}
public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}
public void setJspBody(JspFragment jspBody) {
}
public void setJspContext(JspContext pc) {
this.pageContext=(PageContext) pc;
}
public void setParent(JspTag parent) {
}
}
tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.3</tlib-version>
<short-name>my3</short-name>
<uri>http://www.cgmcc.com/jsp/readfile</uri>
<tag>
<name>readFile</name>
<tag-class>com.cgm.readFile.TagReadFile</tag-class>
<body-content>empty</body-content>
<attribute>
<name>file</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>