Request adapter.

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


public class RequestAdapter extends HttpServlet implements
SingleThreadModel {
  static private String SVR_ADDRESS = "localhost";
  final static int      REQ_PORT = 2050;

  public void doGet(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    try {
      OutputStream outputStream = new Socket(SVR_ADDRESS, REQ_PORT).getOutputStream();
      outputStream.write(Integer.parseInt(req.getParameter
("interval")));
      outputStream.flush();
      out.println("Success");
    }catch(Exception e) {
      out.println("Failure");
    }finally {
      out.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].