The applet communicates with a servlet through a URLConnection. In effect, the servlet functions as a Controller for the applet, performing the usual functions of obtaining user data and an intended action, preparing the data, calling the model, receiving results, etc. The main difference is that the servlet must read a RequestBean from the URLConnection and, at the end of theprocess, prepare a ResponseBean to carry results back to the applet and write that bean to the URLConnection.These functions are illustrated in the code fragments, below.
Process RequestBean
// get request bean requestBean = getRequestBean( request ); if ( requestBean == null ) { sendError( response, "Error reading Request" ); return; } // get DataBean from RequestBean BeanPerson dataBean = (BeanPerson)(requestBean.getDataBean()); dataBean.setOwner( loginName ); BeanPerson dataBeanNew;// get action requested from ReqeustBean String userAction = ( requestBean.getUserAction() ).trim(); int userActionSwitch = UserAction.mapUserAction( userAction );
private RequestBean getRequestBean( HttpServletRequest req ) { RequestBean requestBean; try {
ServletInputStream is = req.getInputStream(); ObjectInputStream ois = new ObjectInputStream( is ); Object object = (ois.readObject());
requestBean = (RequestBean)object;
ois.close();
}catch (IOException e) { return null; } catch (ClassNotFoundException e) { return null; } return requestBean; }
private boolean sendResponseBean( HttpServletResponse resp, ResponseBean responesBean ) { try {
ServletOutputStream os = resp.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( os ); oos.writeObject(responesBean); oos.flush(); oos.close();
} catch (IOException e) {
return false;
}
return true;
}