ClassicFileWriteTagpublic class ClassicFileWriteTag extends BodyTagSupport implements TryCatchFinallyThis class is a custom action for writing the content of the
action element's body to a file specified by an attribute,
or to System.out if no file is specified. If the file name
"log" is specified, the standard application log file is
used. |
Fields Summary |
---|
private String | fileName | private PrintWriter | pw |
Methods Summary |
---|
public int | doAfterBody()Writes the content accumulated in bodyContent to the
file.
String content = bodyContent.getString();
if (fileName == null) {
System.out.println(content);
}
else if ("log".equals(fileName)) {
ServletContext application = pageContext.getServletContext();
application.log(content);
}
else {
pw.print(bodyContent.getString());
}
return SKIP_BODY;
| public void | doCatch(java.lang.Throwable t)Log the problem and rethrow the exception.
ServletContext application = pageContext.getServletContext();
application.log("Exception in body of " +
this.getClass().getName(), t);
throw t;
| public void | doFinally()Closes the file, no matter if an exception was thrown
by the body evaluation or not. Note that this method is
called even if an exception is thrown by doStartTag(), or
any other doXXX() method. In this case, the file may not
have been opened at all.
if (pw != null) {
pw.close();
}
| public int | doStartTag()Makes sure the specified file can be opened for writing.
If so, tells the container to evaluate the body.
if (fileName != null && !"log".equals(fileName)) {
try{
pw = new PrintWriter(new FileWriter(fileName, true));
}
catch (IOException e) {
throw new JspException("Can not open file " + fileName +
" for writing", e);
}
}
return EVAL_BODY_BUFFERED;
| public void | setFileName(java.lang.String fileName)
this.fileName = fileName;
|
|