252 lines
9.3 KiB
HTML
252 lines
9.3 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Example: Creating a traversable integrated file system tree (File two of three)" />
|
|
<meta name="abstract" content="" />
|
|
<meta name="description" content="" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="treenavjava" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Example: Creating a traversable integrated file system tree (File
|
|
two of three)</title>
|
|
</head>
|
|
<body id="treenavjava"><a name="treenavjava"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Creating a traversable integrated file system tree (File
|
|
two of three)</h1>
|
|
<div><p></p>
|
|
<div class="section"><p>This example code, in conjunction with the code in the other two
|
|
example files, displays an HTMLTree and FileListElement in a servlet. The
|
|
three files in the example are:</p>
|
|
</div>
|
|
<div class="section"><ul><li><a href="filetreeexamplejava.htm#filetreeexamplejava">FileTreeExample.java</a> -
|
|
generates the HTML frames and starts the servlet</li>
|
|
<li>TreeNav.java - this file, which builds and manages the tree</li>
|
|
<li><a href="treelistjava.htm#treelistjava">TreeList.java</a> - displays
|
|
the contents of selections made in the TreeNav.java class</li>
|
|
</ul>
|
|
</div>
|
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm#codedisclaimer">Code
|
|
example disclaimer</a> for important legal information.</div>
|
|
</div>
|
|
<div class="section"><div class="p"><pre>//////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// This source is an example of using the IBM Toolbox for Java HTML
|
|
// package classes, which allow you to easily build HTML and File Trees.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
import java.io.File;
|
|
import java.io.PrintWriter;
|
|
import java.io.IOException;
|
|
|
|
import javax.servlet.*;
|
|
import javax.servlet.http.*;
|
|
|
|
import com.ibm.as400.access.AS400;
|
|
import com.ibm.as400.access.IFSJavaFile;
|
|
import com.ibm.as400.util.html.HTMLMeta;
|
|
import com.ibm.as400.util.html.HTMLTree;
|
|
import com.ibm.as400.util.html.HTMLTreeElement;
|
|
import com.ibm.as400.util.html.URLParser;
|
|
import com.ibm.as400.util.html.DirFilter;
|
|
import com.ibm.as400.util.html.FileTreeElement;
|
|
import com.ibm.as400.util.servlet.ServletHyperlink;
|
|
|
|
//
|
|
// An example of using the HTMLTree and FileTreeElement classes
|
|
// in a servlet.
|
|
//
|
|
|
|
public class TreeNav extends HttpServlet
|
|
{
|
|
private AS400 sys_;
|
|
|
|
public void init(ServletConfig config)
|
|
throws ServletException
|
|
{
|
|
super.init(config);
|
|
|
|
// Create an AS400 object.
|
|
sys_ = new AS400("mySystem", "myUserID", "myPassword");
|
|
|
|
// IBM Toolbox for Java uses a set of default icons to represents expanded,
|
|
// collapsed, and documents within the HTMLTree. To enhance those icons,
|
|
// IBM Toolbox for Java ships three gifs (expanded.gif, collapsed.gif, bullet.gif)
|
|
// in the jt400Servlet.jar file. Browsers do not have the ability to find
|
|
// gifs in a jar or zip file, so you need to extract those images from the
|
|
// jar file and place them in the appropriate webserver directory (by default
|
|
// it is the /html directory). Then change the following lines of code to
|
|
// specify the correct location in the set methods. The location can be
|
|
// absolute or relative.
|
|
|
|
HTMLTreeElement.setExpandedGif("http://myServer/expanded.gif");
|
|
HTMLTreeElement.setCollapsedGif("http://myServer/collapsed.gif");
|
|
HTMLTreeElement.setDocGif("http://myServer/bullet.gif");
|
|
}
|
|
|
|
/**
|
|
* Process the GET request.
|
|
* @param req The request.
|
|
* @param res The response.
|
|
**/
|
|
|
|
public void doGet (HttpServletRequest req, HttpServletResponse resp)
|
|
throws ServletException, IOException
|
|
{
|
|
// Use session data to remember the state of the tree.
|
|
HttpSession session = req.getSession(true);
|
|
HTMLTree fileTree = (HTMLTree)session.getValue("filetree");
|
|
|
|
// If this session does not already have a file tree, then
|
|
// create the initial tree.
|
|
if (fileTree == null)
|
|
fileTree = createTree(req, resp, req.getRequestURI());
|
|
|
|
// Set the Http servlet request on the HTMLTree.
|
|
fileTree.setHttpServletRequest(req);
|
|
|
|
resp.setContentType("text/html");
|
|
|
|
PrintWriter out = resp.getWriter();
|
|
out.println("<html>\n");
|
|
out.println(new HTMLMeta("Expires","Mon, 03 Jan 1990 13:00:00 GMT"));
|
|
out.println("<body>\n");
|
|
|
|
// Get the tag for the HTMLTree.
|
|
out.println(fileTree.getTag());
|
|
|
|
out.println("</body>\n");
|
|
out.println("</html>\n");
|
|
out.close();
|
|
|
|
// Set the session tree value, so when entering this servlet for
|
|
// the second time, the FileTree object will be reused.
|
|
session.putValue("filetree", fileTree);
|
|
}
|
|
|
|
/**
|
|
* Process the POST request.
|
|
* @param req The request.
|
|
* @param res The response.
|
|
**/
|
|
|
|
public void doPost (HttpServletRequest req, HttpServletResponse res)
|
|
throws ServletException, IOException
|
|
{
|
|
res.setContentType("text/html");
|
|
ServletOutputStream out = res.getOutputStream();
|
|
}
|
|
|
|
/**
|
|
* This method will create the initial HTMLTree.
|
|
**/
|
|
|
|
private HTMLTree createTree(HttpServletRequest req,
|
|
HttpServletResponse resp, String uri)
|
|
{
|
|
// Create an HTMLTree object.
|
|
HTMLTree tree = new HTMLTree(req);
|
|
|
|
try
|
|
{
|
|
// Create a URLParser object.
|
|
URLParser urlParser = new URLParser(uri);
|
|
|
|
// Create a File object and set the root IFS directory.
|
|
IFSJavaFile root = new IFSJavaFile(sys_, "/QIBM");
|
|
|
|
// Create a Filter.
|
|
DirFilter filter = new DirFilter();
|
|
|
|
// Get the list of files that satisfy the directory filter.
|
|
String[] list = root.list(filter);
|
|
|
|
File[] dirList = new File[list.length];
|
|
|
|
// We don't want to require webservers to use JDK1.2 because
|
|
// most webserver JVM's are slower to upgrade to the latest
|
|
// JDK level. The most efficient way to create these file objects
|
|
// is to use the listFiles(filter) method in JDK1.2 which would
|
|
// be done like the following, instead of using the list(filter)
|
|
// method and then converting the returned string arrary into the
|
|
// appropriate File array.
|
|
// File[] dirList = root.listFiles(filter);
|
|
|
|
for (int j=0; j<dirList.length; ++j)
|
|
{
|
|
if (root instanceof IFSJavaFile)
|
|
dirList[j] = new IFSJavaFile((IFSJavaFile)root, list[j]);
|
|
else
|
|
dirList[j] = new File(list[j]);
|
|
}
|
|
|
|
for (int i=0; i<dirList.length; i++)
|
|
{
|
|
// Create a FileTreeElement for each directory in the list.
|
|
FileTreeElement node = new FileTreeElement(dirList[i]);
|
|
|
|
// Create a ServletHyperlink for the expand/collapse icons.
|
|
ServletHyperlink sl = new ServletHyperlink(urlParser.getURI());
|
|
sl.setHttpServletResponse(resp);
|
|
node.setIconUrl(sl);
|
|
|
|
// Create a ServletHyperlink to the TreeList servlet, which will
|
|
// display the contents of thie FileTreeElement (directory).
|
|
ServletHyperlink tl = new ServletHyperlink("/servlet/TreeList");
|
|
tl.setTarget("list");
|
|
|
|
// If the ServletHyperlink doesn't have a name, then set it to the
|
|
// name of the directory.
|
|
if (tl.getText() == null)
|
|
tl.setText(dirList[i].getName());
|
|
|
|
// Set the TextUrl for the FileTreeElement.
|
|
node.setTextUrl(tl);
|
|
|
|
// Add the FileTreeElement to the HTMLTree.
|
|
tree.addElement(node);
|
|
}
|
|
|
|
sys_.disconnectAllServices();
|
|
}
|
|
|
|
catch (Exception e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return tree;
|
|
}
|
|
|
|
public void destroy(ServletConfig config)
|
|
{
|
|
// do nothing
|
|
}
|
|
|
|
public String getServletInfo()
|
|
{
|
|
return "FileTree Navigation";
|
|
}
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |