用Java实现可保存状态的数据库生成XML树,源码来了(11)
发表于:2007-07-01来源:作者:点击数:
标签:
3. XMLTreeXSL.java package com.sitechasia; import javax.servlet.*; import javax.servlet.http.*; import java.io.PrintWriter; import java.io.IOException; public class XMLTreeXSL extends HttpServlet { PrintWriter out; boolean flag=true; privat
3. XMLTreeXSL.java
package com.sitechasia;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
public class XMLTreeXSL extends HttpServlet {
PrintWriter out;
boolean flag=true;
private static final String CONTENT_TYPE = "text/xsl";
private static final String CONTENT_XSL = "<?xml version=\"1.0\" encoding=\"gb2312\" ?><xsl:stylesheet xmlns:xsl=\"http://www.w3.org/TR/WD-xsl\">";
/**Initialize global variables*/
public void init() throws ServletException {
}
/**Process the HTTP Get request*/
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
HttpSession session=request.getSession();
if ( session.getAttribute("ResultEditHandle")==null) {
flag=false;
}
else {
flag=true;
}
if (flag) {
javax.servlet.http.Cookie cookies[] = request.getCookies();
String t_click_id = null;
response.setContentType(CONTENT_TYPE);
out = response.getWriter();
out.println(CONTENT_XSL);
out.println("<xsl:template match=\"/\">");
out.println("<HTML>");
out.println("<HEAD><STYLE>body{FONT-SIZE: 10pt;}</STYLE></HEAD>");
out.println("<BODY><xsl:apply-templates select=\"//node\"/></BODY></HTML></xsl:template>");
if ((cookies == null)||(cookies.length == 0)){
out.println("No Clicked Node");
}
else {
for(int i = 0;i < cookies.length; i++){
javax.servlet.http.Cookie c =cookies[i];
if ( c.getName().equals("ClickId")){
t_click_id = c.getValue();
out.println("<xsl:template match=\"node\">");
out.println("<xsl:if test=\".[number(id) $eq$ "+c.getValue()+"]\">");
out.println("当前点击节点的序号:<xsl:value-of select=\"id\"/><br/>");
out.println("节点名称:<xsl:value-of select=\"value\"/><br/>");
out.println("子节点为:<hr/>");
out.println("<xsl:for-each select=\"./node[number(father) $eq$ "+t_click_id+"]\">");
out.println("序号:<xsl:value-of select=\"id\"/><br/>");
out.println("名称:<xsl:value-of select=\"value\"/><br/>");
out.println("</xsl:for-each>");
out.println("</xsl:if>");
out.println("</xsl:template>");
out.println("</xsl:stylesheet>");
}
}
}
}
}
catch(Exception e){
System.out.println("errror:"+e.getMessage());
e.printStackTrace();
}
}
/**Clean up resources
*/
public void destroy() {
}
}
4.Servlet的配置,web.xml文件
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//D
TD Web Application 1.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>
xmlservlet
</servlet-name>
<servlet-class>
com.sitechasia.XMLServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
xmlservlet
</servlet-name>
<url-pattern>
xs
</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>
RefreshServlet
</servlet-name>
<servlet-class>
com.sitechasia.RefreshServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
RefreshServlet
</servlet-name>
<url-pattern>
rs
</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>
xmltreexsl
</servlet-name>
<servlet-class>
com.sitechasia.XMLTreeXSL
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
xmltreexsl
</servlet-name>
<url-pattern>
xmltreexsl.xsl
</url-pattern>
</servlet-mapping>
</web-app>
原文转自:http://www.ltesting.net