requesthandler.java
来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 152 行
JAVA
152 行
package firewall.server;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import firewall.common.*;
import java.io.*;
/**
* Title: RequestHandler
* Description: Abstract base class for handling requests
* Copyright: Copyright (c) 2001
* Company:
* @author Andrew Harbourne-Thomas
* @version 1.0
*/
public abstract class RequestHandler {
/** HttpServletRequest from servlet containing request data for use
* by handlers
*/
HttpServletRequest httpServletRequest;
/** HttpServletResponse from servlet containing response data for use
* by handlers
*/
HttpServletResponse httpServletResponse;
/**
* String location of files directory
* Defaults to current location
*/
static String stringFileBase = File.separator;
/** File object representation of files directory */
File fileBase;
/**
* Method returns an array of String objects corresponding to
* files within files directory
* @return Array of filenames within files directory
*/
protected String[] getFileList() {
String[] list = fileBase.list();
return list;
}
/**
* Method to create File object from String filename
* @param fileName Name of file
*
* @return File object representing file identified by filename
*/
protected File getFile(String fileName) {
return new File(fileBase, fileName);
}
/**
* Constructor to find the "Tomcat" home directory and hence the location for
* our application and the files directory 'hidden' within the
* WEB-INF directory
*/
public RequestHandler() {
fileBase = new File(stringFileBase);
}
/**
* This method must be overridden in subclasses to perform the request
* processing.
*
* @param baseRequest The BaseRequest object that specifes the
* request type
*/
public abstract void respond(BaseRequest baseRequest);
/**
* Method sets the request and response object references for the handlers
* from the servlet
*
* @param httpServletRequest Servlets HttpServletRequest object
* @param httpServletResponse Servlets HttpServletResponse object
*/
public void setHttpObjects(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
this.httpServletRequest = httpServletRequest;
this.httpServletResponse = httpServletResponse;
}
/**
* Method to send a file from the server to the client
*
* @param sendFile File to be sent to the client
*
* @throws IOException
*/
public void sendFile(File sendFile) throws IOException {
FileReader fileReader = new FileReader(sendFile);
PrintWriter out = httpServletResponse.getWriter();
char c[] = new char[4096];
int read = 0;
//Read until end of file and send to client
while ((read = fileReader.read(c)) != -1) {
out.write(c, 0, read);
}
//Close
fileReader.close();
out.close();
}
/**
* Method to send an object from the servlet to the client
*
* @param sendObject Object to be sent to the client
*
* @throws IOException
*/
public void sendSerializedObject(Object sendObject)
throws IOException {
OutputStream os = httpServletResponse.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(sendObject);
oos.flush();
os.flush();
oos.close();
}
/**
* Sets the response HTTP status code
*/
public void setStatusCode(int statusCode) {
httpServletResponse.setStatus(statusCode);
}
/**
* Sets an error code and message for the response
*/
public void sendError(int statusCode, String message)
throws IOException {
httpServletResponse.sendError(statusCode, message);
}
/**
* Static method to set the location of the files
*/
public static void setStringFileBase(String fileBase) {
stringFileBase = fileBase;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?