public abstract class CsvWriter extends HttpServlet
The methods getResultSet and makeConnection are abstract and will need to be overridden before this class can be used. The user may also wish to override several of the methods for formatting information.
This code needs to be changed since many of the fields that belong to the instance must be moved within the service method since there can be multiple copies of the service method running at the same time.
getResultSet(HttpServletRequest, DatabaseProperties)
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected class |
CsvWriter.Instance
Encapsulates methods and properties for a given instance of the
service method.
|
Modifier and Type | Field and Description |
---|---|
protected ServletConfig |
config
ServletConfig object as passed to the init method.
|
private static long |
serialVersionUID
Provided for compliance with Serializable interface.
|
Constructor and Description |
---|
CsvWriter() |
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Called by the servlet container when the servlet object is being taken out
of service.
|
protected String |
formatDateTime(Date valueIn)
Alternate calling sequence, leaving out the column number
|
protected String |
formatDateTime(Date valueIn,
int column)
Formatter for values involving date and time.
|
protected String |
formatDouble(double valueIn)
Alternate calling sequence, leaving out the column number.
|
protected String |
formatDouble(double valueIn,
int column)
Formatter for floating point numbers.
|
protected String |
formatInteger(int valueIn)
Alternate calling sequence, omitting the column number.
|
protected String |
formatInteger(int valueIn,
int column)
Formatter for integer values.
|
protected String |
formatString(String valueIn)
Method of calling formatString when it doesn't
depend on the column in the result set.
|
protected String |
formatString(String valueIn,
int column)
Double up on all double quotes in string and surround the string with
double quotes so that it will be in the proper format for the CSV file.
|
protected abstract ResultSet |
getResultSet(HttpServletRequest req,
DatabaseProperties data)
Will be overridden with the method for obtaining the SQL
result set.
|
void |
init(ServletConfig configIn)
Called by the servlet container when the servlet object is first placed
in service.
|
protected abstract DatabaseProperties |
makeConnection()
Obtain the database connection information.
|
protected void |
printHeader(CsvWriter.Instance instance)
Will print header line for CSV file.
|
protected void |
printLine(CsvWriter.Instance instance)
Will print one line of CSV file.
|
void |
service(HttpServletRequest req,
HttpServletResponse res)
Called by servlet container when an HTTP transaction requests a response
from the server.
|
protected void |
setValues(CsvWriter.Instance instance)
Sets values in Instance object.
|
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
private static final long serialVersionUID
protected ServletConfig config
Must be moved to inner class
public CsvWriter()
public void init(ServletConfig configIn) throws ServletException
init
in interface Servlet
init
in class GenericServlet
ServletException
public void destroy()
destroy
in interface Servlet
destroy
in class GenericServlet
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
service
in class HttpServlet
req
- Object containing request informationres
- Object containing response informationIOException
protected abstract DatabaseProperties makeConnection() throws IOException
IOException
protected abstract ResultSet getResultSet(HttpServletRequest req, DatabaseProperties data) throws IOException
This should be kept separate from the getResultSet method within the
Instance class. The CsvWriter.Instance.getResultSet()
method returns the
ResultSet object stored in the Instance object. This method actually generates
the ResultSet object. The service method then stores the result of this method
in the Instance object.
req
- Request informationIOException
CsvWriter.Instance.message
,
CsvWriter.Instance.outputFilename
protected void setValues(CsvWriter.Instance instance) throws IOException
This must be overridden in a subclass in order to set parameters.
instance
- Instance object being affectedIOException
protected void printHeader(CsvWriter.Instance instance) throws IOException
IOException
protected void printLine(CsvWriter.Instance instance) throws IOException
This method will be changed at a later time to allow the specification of overriding methods for formatting timestamps, integers, floating point numbers, character strings, etc.
IOException
protected final String formatString(String valueIn)
valueIn
- String to be processedprotected String formatString(String valueIn, int column)
valueIn
- String to be processedcolumn
- Column number of item - This will allow different formatting to be used
for each column. This parameter is not used unless the method is
overridden.protected final String formatDouble(double valueIn)
valueIn
- Floating point number to be formattedprotected String formatDouble(double valueIn, int column)
This method can be overridden to control printing of the values in the CSV file.
valueIn
- Floating point number to be processedcolumn
- Column number - This will allow different formatting to be used
for each column. This parameter is not used unless the method
is overridden.protected final String formatInteger(int valueIn)
valueIn
- Integer value to be formattedprotected String formatInteger(int valueIn, int column)
This method can be overridden to control printing of the values in the CSV file.
valueIn
- Integer to be formattedcolumn
- Column number - This will allow different formatting to be used
for each column. This parameter is not used unless the method is
overridden.protected final String formatDateTime(Date valueIn)
valueIn
- java.util.Date object containing date and timeprotected String formatDateTime(Date valueIn, int column)
This method can be overridden to control printing of the values in the CSV file.
valueIn
- java.util.Date object containing date and timecolumn
- Column number - This will allow different formatting to be used
for each column. This parameter is not used unless the method is
overridden.