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:
Note: Read the Code example disclaimer for important legal information.
////////////////////////////////////////////////////////////////////////////////
//
// This source is an example of using the IBM Toolbox for Java HTML
// package classes, which allow you to easily build HTML and File Lists.
//
////////////////////////////////////////////////////////////////////////////////
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import com.ibm.as400.access.AS400;
import com.ibm.as400.access.Trace;
import com.ibm.as400.access.IFSJavaFile;
import com.ibm.as400.util.html.HTMLMeta;
import com.ibm.as400.util.html.HTMLHeading;
import com.ibm.as400.util.html.HTMLConstants;
import com.ibm.as400.util.html.FileListElement;
import com.ibm.as400.util.html.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* An example of using the FileListElement class in a servlet.
**/
public class TreeList extends HttpServlet
{
private AS400 sys_;
/**
* Process the GET request.
* @param req The request.
* @param res The response.
**/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
try
{
PrintWriter out = resp.getWriter();
out.println("<html>\n");
out.println(new HTMLMeta("Expires", "Mon, 02 Jan 1990 13:00:00 GMT"));
out.println("<body>\n");
// If the path parameter is not null, then the user has selected an element from
// the FileTreeElement list in the navigation frame.
if (req.getPathInfo() != null)
{
// Create a FileListElement passing in an AS400 system object and the Http servlet request.
// The request will contain the necessary path information to list out the contents of
// the FileTreeElement (directory) selected.
FileListElement fileList = new FileListElement(sys_, req);
// Alternately, create a FileListElement from a NetServer share name and share path.
//
// FileListElement fileList = new FileListElement(sys_, req, "TreeShare", "/QIBM/ProdData/HTTP/Public/jt400");
// Display the FileListElement contents.
out.println(fileList.list());
}
else // Display this HTMLHeading if no FileTreeElement has been selected.
{
HTMLHeading heading = new HTMLHeading(1,"An HTML File List Example");
heading.setAlign(HTMLConstants.CENTER);
out.println(heading.getTag());
}
out.println("</body>\n");
out.println("</html>\n");
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 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();
}
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
// Create an AS400 object.
sys_ = new AS400("mySystem", "myUID", "myPWD");
}
}