Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Tim Bray on 'What Sun Should Do'
JTree or JList) and the common drag-and-drop class. The technique described in this article simplified the drag-and-drop implementation.Servlet class, along with another class, needs to be extended. The application being developed is a text-based message system used
to send text messages to a mobile phone from another mobile phone, the Web, a PDA, or some other device with access to the
Web or the phone network.MessageClient class to contain all the common methods needed to communicate with the message server. The class facilitates client development
because it can be used as the base class for all the possible clients.MessageClient class, shown in Listing 1, contains three methods. The sendMessage() method sends the actual message to the server. connectToServer() connects to the message server, which, in this example, is an RMI (remote method invocation) server. The last method is getServerName(), which is an abstract method because each device that uses this class has a different way of determining the name of the
message server. This means that all the clients that extend MessageClient must implement the getServerName() method. import java.rmi.Naming;
public abstract class MessageClient
{
private MessageServer messageServer;
public MessageClient()
{
System.out.println("Initializing Message Client");
}
/**
* Method used to connect to the message server
*
* @param serverName name of the server that contains the message server
*/
protected void connectToServer()
{
String serverName = getServerName();
try
{
String name = "//" + serverName + "/MessageServer";
messageServer = ((MessageServer) Naming.lookup(name));
}
catch(Exception e)
{
System.out.println("Error connecting to Message Server. Exception is " + e);
e.printStackTrace();
}
}
/**
* Method used to send message to server
*
* @param phoneNum phone number to send message to
* @param message message to send
*/
public boolean sendMessage(String phoneNum, String message)
{
try
{
return(messageServer.sendMessage(phoneNum,message));
}
catch(Exception e)
{
System.out.println("Error Sending Message. Exception is " + e);
e.printStackTrace();
return(false);
}
}
public abstract String getServerName();
}
HttpServlet and a MessageClient. Since Java does not allow such behavior, the main class extends the HttpServlet class, as shown in Listing 2. This main class contains an inner class that extends MessageClient. The outer class then creates an instance of the inner class. 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);
}
}
}
MessageClient is extended by a member of the outer class and not the outer class itself), but the effect is the same. Although MessageClient could have been extended in a separate class, using an inner class allows it to access all the members and methods of the
outer class. This makes it easier for the two classes to interact.MessageClient class had a constructor that accepted the server name, the getServerName() method would not need to be abstract, which means the Web client would not have to extend the MessageClient class. The Web client could have used the class directly. Developers should be cautious and only use multiple inheritance
if a clear reason warrants it because multiple inheritance complicates design and is easily misused.
Archived Discussions (Read only)