fileexchangeservlet.java
来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 109 行
JAVA
109 行
package firewall.server;
import firewall.common.*;
import javax.servlet.http.HttpServlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Title: FileExchangeServlet
* Description: Servlet that handles the client requests and delegates
* processing to the appropriate handler
* Copyright: Copyright (c) 2001
* Company:
* @author Andrew Harbourne-Thomas
* @version 1.0
*/
public class FileExchangeServlet extends HttpServlet {
/** Reference to the RequestHandlerFactory singleton instance */
private RequestHandlerFactory requestHandlerFactory = null;
/**
* Initialise the RequestHandlerFactory singleton
*
* @throws ServletException
*/
public void init() throws ServletException {
//this is the location of our files directory
String directory = getServletContext().getRealPath("/") +
"WEB-INF" + File.separator + "files" + File.separator ;
//ensure that the RequestHandler knows the correct directory
//where the files are located
RequestHandler.setStringFileBase(directory);
requestHandlerFactory = RequestHandlerFactory.getInstance();
}
/**
* Post method handles post requests from application, by activating the
* appropriate handler and calling the handler.respond(..) method to
* process the request
*
* @param request The HTTP request object
* @param response The HTTP response object
*
* @throws IOException
* @throws ServletException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//read the request object
BaseRequest baseObject = (BaseRequest) readSerializedObject(request);
//get the appropriate handler
RequestHandler handler =
(RequestHandler) requestHandlerFactory
.getHandler(baseObject.getRequestType());
//pass in the request, response objects to the handler
handler.setHttpObjects(request, response);
//process the request
handler.respond(baseObject);
}
catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Get method forwards request to the doPost method.
*
* @param request The HTTP request object
* @param response The HTTP response object
*
* @throws IOException
* @throws ServletException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* Method to extract an object sent by the client as part of its request to
* the server
*
* @param httpServletRequest Request to extract object from
*
* @return Object read in from the clients request
*
* @throws ClassNotFoundException
* @throws IOException
*/
private Object readSerializedObject(HttpServletRequest httpServletRequest)
throws IOException, ClassNotFoundException {
ObjectInputStream ois =
new ObjectInputStream(httpServletRequest.getInputStream());
Object readObject = ois.readObject();
return readObject;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?