Servlet Adapter.

Architect's Corner
Servlet adapters can solve your socket problems
by Andrei Nazariev
Listing 1. Servlet Adapter.


  public class Adapter extends HttpServlet implements Single ThreadModel {
  final static String SVR_ADDRESS = "localhost";
  final static int    SVR_PORT = 2050;

  public void doGet(HttpServletRequest req, HttpServlet Response res)
  throws ServletException, IOException {
    InputStream fromServer = new Socket(SVR_ADDRESS, SVR_PORT).getInputStream();
    OutputStream toClient = res.getOutputStream();
    
    byte[] buffer = new byte[4096];
    toClient.write(buffer, 0, fromServer.read(buffer));
    toClient.flush();
    
    fromServer.close();
    toClient.close();
  }
}

About the Author

Andrei Nazariev is a senior Java architect at Sun Microsystems' Sun Java Center in Orlando, FL. He can be contacted at [email protected].