Response adapter.

Architect's Corner
Servlet adapters can solve your socket problems
by Andrei Nazariev
Listing 5. Response adapter.


public class ResponseAdapter extends HttpServlet implements
SingleThreadModel {
  final static String SVR_ADDRESS = "localhost";
  final static int    RES_PORT = 2051;

  public void doGet(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {
    OutputStream toClient = res.getOutputStream();
    InputStream fromServer = new Socket(SVR_ADDRESS, RES_PORT).getInputStream();
    byte[] buffer = new byte[256];
    try {
      while(true) {
        toClient.write(buffer, 0, fromServer.read(buffer));
        toClient.flush();
      }
    }catch(IOException e) { e.printStackTrace(); }
    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 nazariev@pobox.com.

Featured

Upcoming Events

AppTrends

Sign up for our newsletter.

I agree to this site's Privacy Policy.