代码:
JspFilter.java:
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import javax.servlet.Filter
import javax.servlet.FilterChain
import javax.servlet.FilterConfig
import javax.servlet.ServletException
import javax.servlet.ServletRequest
import javax.servlet.ServletResponse
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
public class JspFilter implements Filter {
public void destroy() {
// TODO 自动生成方法存根
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq = (HttpServletRequest) request
HttpServletResponse hresp = (HttpServletResponse) response
String name = hreq.getRequestURL().substring(
hreq.getRequestURL().lastIndexOf("/") + 1,
hreq.getRequestURL().lastIndexOf("."))
if (hreq.getRequestURL().indexOf(".jsp") != -1 &&(null == hreq.getParameter("type") || hreq.getParameter("type").equals(""))) {
hresp.sendRedirect(hreq.getContextPath()+"/conversion?name="+hreq.getRequestURL())
return
}
chain.doFilter(request, response)
}
public void init(FilterConfig arg0) throws ServletException {
// TODO 自动生成方法存根
}
}
ConversionServlet.java:
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.net.HttpURLConnection
import java.net.URL
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
public class ConversionServlet extends HttpServlet {
public ConversionServlet () {
super()
}
public void destroy() {
super.destroy()// Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html")
PrintWriter out = response.getWriter()
//----获取请求的URL
String reqTarget = request.getParameter("name")
//----指定对应JSP的HTML名称
String name = reqTarget.substring(reqTarget.lastIndexOf("/") + 1,reqTarget.lastIndexOf("."))
//---判断是否存在对应的HTML文件
File file = new File(request.getRealPath("/") + name + ".html")
if (!file.exists()) { //--------如果不存在对应的HTML文件
try {
file.createNewFile() //--------创建HTML文件
//-------将JSP的内容写入对应的HTML文件内
InputStream in
StringBuffer sb = new StringBuffer("")
//----注意这里,不能直接访问请求的URL,如果直接访问的话,会陷入死循环
URL url = new java.net.URL(reqTarget.toString()+"?type=11")
HttpURLConnection connection = (HttpURLConnection) url
.openConnection()
connection.setRequestProperty("User-Agent", "Mozilla/4.0")
connection.connect()
in = connection.getInputStream()
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "GBK"))
String currentLine
FileOutputStream fos = new FileOutputStream(file)
while ((currentLine = breader.readLine()) != null) {
sb.append(currentLine)
fos.write(currentLine.getBytes())
}
if (null != breader)
breader.close()
if (null != fos)
fos.close()
//---------------转到与JSP对应的HTML页
response.sendRedirect(request.getContextPath()+"/"+name + ".html")
} catch (Exception e) {
e.printStackTrace()
}
}else{
//----------如果存在指定的HTML,直接跳转到指定的HTML页
response.sendRedirect(request.getContextPath()+"/"+name + ".html")
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response)
}
public void init() throws ServletException {
// Put your code here
}
}
WEB.XML配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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-app_2_4.xsd">
<filter>
<filter-name>jspfilter</filter-name>
<filter-class>com.beanutil.JspFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jspfilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>conversion</servlet-name>
<servlet-class>com.beanutil.ConversionServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>conversion</servlet-name>
<url-pattern>/conversion</url-pattern>
</servlet-mapping>
</web-app>
import java.io.*import java.util.*
import org.jdom.*
import org.jdom.input.*
public class MyXMLReader2JDOM {
public static void main(String arge[]) {
long lasting = System.currentTimeMillis()
try {
SAXBuilder builder = new SAXBuilder()
Document doc = builder.build(new File("xmlcount.xml"))
Element foo = doc.getRootElement()
// List allChildren = foo.getChildren()
System.out.print( foo.getAttributeValue("month-count"))
System.out.println(foo.getAttributeValue("total-count"))
} catch (Exception e) {
e.printStackTrace()
}
}
}
补充:
System.out.print(foo.getChild("date").getText())
补充:
测试通过:
import java.io.*
import java.util.*
import org.jdom.*
import org.jdom.input.*
public class MyXMLReader2JDOM {
public static void main(String arge[]) {
try {
SAXBuilder builder = new SAXBuilder()
Document doc = builder.build(new File("D:/xmlcount.xml"))
Element foo = doc.getRootElement()
System.out.println("month-count : " + foo.getAttributeValue("month-count"))
System.out.println("total-count : " + foo.getAttributeValue("total-count"))
System.out.println("date : " + foo.getChild("date").getText())
} catch (Exception e) {
e.printStackTrace()
}
}
}
java实现 在jsp页面中调用方法//读xml文件
public DataSrcStruct getDataSrcByID(String strID)throws Exception{
String strSQL="select id,sname,filename,ctime,datasrc_name " +
" from datasource" +
" where id='"+strID+"'"
ArrayList lst=geList(strSQL,0,1)
DataSrcStruct pDataSrcStruct=null
if (lst.size()>0){
pDataSrcStruct=(DataSrcStruct)lst.get(0)
//得到xml文件
ServerMgr pServerMgr=new ServerMgr()
String savepath = pServerMgr.getRSCollectDataPath()+ "/" + pDataSrcStruct.getId() + ".xml"
SAXReader saxReader = new SAXReader()
Document document = saxReader.read(new File(savepath))
//使用输出流来进行转化
ByteArrayOutputStream output = new ByteArrayOutputStream()
//使用GB2312编码
OutputFormat format = new OutputFormat(" ", true, "GB2312")
XMLWriter writer = new XMLWriter(output, format)
writer.write(document)
String sxml = output.toString("GB2312")
pDataSrcStruct.setXml_info(sxml)
}
return pDataSrcStruct
}
//保存xml格式文件
public boolean saveXml()throws Exception{
// DocumentFactory factory = new DocumentFactory()
Document doc = DocumentHelper.parseText(pDataSrcStruct.getXml_info())
OutputFormat format = OutputFormat.createPrettyPrint()
format.setEncoding("GB2312")
ServerMgr pServerMgr=new ServerMgr()
String savepath = pServerMgr.getRSCollectDataPath()+ "/" + pDataSrcStruct.getId() + ".xml"
XMLWriter writer = new XMLWriter(new FileWriter(new File(savepath)),format)
writer.write(doc)
writer.close()
return true
}