home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
PageHandler.java
< prev
next >
Wrap
Text File
|
1998-09-13
|
4KB
|
189 lines
/*
* Symantec Java Servlet Sample
*
* (c) 1998 Symantec
*
*
*/
package symantec.sourcebook.creditcheck;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This class is a generic SuperClass for handling
* http pages. The page handler serves the page,
* validates input data, and decides which page should be
* served next.
*
*
*/
public class PageHandler
{
/**
* String which holds the page's html.
*
*/
public String pageHtml;
/**
* Name of the file which contains the HTML for this page.
*
*/
protected static final String PAGEBASE = "private_html" +
File.separator + "symantec" + File.separator;
/**
* Create default PageHandler object.
*
*/
public PageHandler()
{
}
/**
* Return the file name which contains the HTML for this page.
*
*/
public String pageName()
{
return null;
}
/**
* Serve this page. Read the page's HTML from disk, customize it,
* and write it to the HttpServletResponse's stream.
*
*/
protected void serve(Object pageData,HttpServletResponse response) throws IOException
{
read();
customize(pageData);
write(response);
}
/**
* Read a page from disk. Place the results in
* pageHtml.
*
*/
protected void read() throws IOException
{
FileInputStream in = new FileInputStream(pageName());
byte[] b = new byte[in.available()];
in.read(b);
in.close();
pageHtml = new String(b);
}
/**
* Customize the page HTML.
*
*/
protected void customize(Object pageData) throws IOException
{
// default routine does nothing
}
/**
* Write the page to the respons output stream.
*
*
*/
protected void write(HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
response.setContentLength(pageHtml.length());
ServletOutputStream out = response.getOutputStream();
out.print(pageHtml);
}
/**
* Validate the contents of the form.
* Return and object containing the form's validated data.
*
*
*/
protected Object validate(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException
{
return null;
}
/**
* Determine which page should be served next.
* (Acutally, it's often determined in validate and just
* passed back in this call(
*
*
*/
public int nextPage()
{
return 0;
}
/**
*
* Replace special marker in the HTML page with
* the specified text.
*
*/
protected void insert(String marker,String text)
{
String replaceThis = "<!--"+marker+"-->";
int pos = pageHtml.indexOf(replaceThis);
if(pos >= 0)
{
String newpageHtml = "";
if(pos > 0) newpageHtml += pageHtml.substring(0,pos);
newpageHtml += text;
if(pos+replaceThis.length() < pageHtml.length() )
newpageHtml += pageHtml.substring(pos+replaceThis.length());
pageHtml = newpageHtml;
}
}
/**
* get request parameter, or, if no parameter, return the
* default.
*
*/
protected String getParameter(HttpServletRequest request,String name,String defaultValue)
{
String value = request.getParameter(name);
if(value != null) return value;
return defaultValue;
}
}