这个类包含3个方法:sendMessage()方法将实际信息发送到服务器;connectServer()方法负责连接服务。在本例中,服务是一个RMI服务;getServerName()是一个抽象类,因为不同设备决定服务名字的方式不同。也就是说,任何继承自MessageClient的类都必须实现getServerName()方法。在开发与信息服务通信的客户端程序时,我们需要多重继承。我们的网络客户端是一个简单的用来接收来自表单的信息并将其传送到服务器的Servlet。为了完成上述任务,这个Servlet必须既继承HttpServlet又继承MessageClient。由于Java不允许这种继承行为,我让主类继承HttpServlet,让主类中的内部类继承MessageClient,然后外部类创建了内部类的一个实例。代码如下:public class SendMessageServlet extends HttpServlet{ private MessageClient m_messageClient; private String m_serverName; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ try{ //Get server name m_serverName = request.getServerName(); System.out.println("ServerName is " + m_serverName); //Create message client to communicate with message server m_messageClient = new ServletMessageClient(); System.out.println("Created Message Client"); m_messageClient.connectToServer(); //Get message and phone number String phoneNum = (String) request.getParameter("PhoneNum"); String message = (String) request.getParameter("Message"); //Send message m_messageClient.sendMessage(phoneNum,message); //Display page to tell user message was sent response.setContentType("text/html"); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/SendMessageForm.jsp"); dispatcher.include(request, response); }catch (Exception e){ e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** *//** Inner class used to extend MessageClient */ public class ServletMessageClient extends MessageClient { public ServletMessageClient(){ super(); } public String getServerName(){ System.out.println("Returning ServerName " + m_serverName); return(m_serverName); } }}