Click to See Complete Forum and Search --> : applet chat room work on Netscape but not IE


mhc
September 17th, 2000, 06:22 AM
I write a simple Chat room servlet program (Servlet with TomCat on Windows 2000 server)
Besides, i write a applet to connect to the servlet, netscape work well, however, IE cannot.
I don't know why.(I am quite sure that setting on IIS and tomcat are correct)
The code are the following:
(servlet)
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ChatServlet extends HttpServlet
{
private UserList users=new UserList();
private Message message;
private final static String defaultHKIDpage="http://www.hkid.com/";
//private String gid;
static final int TIMEOUT=5*60*1000;
ServerThread reaper;

public void init(ServletConfig config) throws ServletException
{
super.init(config);
reaper=new ServerThread();
Thread t=new Thread(reaper);
t.start();
return;
}

public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
String user,mode,usermsg;

if(req.getQueryString()==null)
{
res.sendRedirect(defaultHKIDpage);
return;
}

user=req.getParameter("user");
mode=req.getParameter("mode");

if(mode==null)
{
sendError(res,"Mode not set");
return;
}

if(user==null &&!mode.equals("list"))
{
sendError(res,"User not set");
return;
}

if(mode.equals("login"))
{
if(users.exists(user))
{
sendError(res,"User exists");
return;
}
if(!users.add(user))
{
sendError(res,"Problem adding user");
return;
}

users.addMessage(new Message("Chat",user+" has joined the chat."));
sendResp(res,user + " logged in");
return;
}
else if(mode.equals("logout"))
{
if(!users.exists(user))
{
sendError(res,"User does not exist");
return;
}
if(!users.drop(user))
{
sendError(res,"Problem deleting user");
return;
}
users.addMessage(new Message("Chat",user+"has left the chat."));
sendResp(res,user+ " logged out.");
return;
}
else if(mode.equals("poll"))
{
if(!users.exists(user))
{
sendError(res,"Invalid user");
return;
}
returnMessages(res,users.getMessages(user));
users.resetUser(user);
return;
}
else if(mode.equals("send"))
{
usermsg=req.getParameter("message");
if(usermsg==null)
{
sendError(res,"Message not set for send");
return;
}
users.addMessage(new Message(user,usermsg));
sendResp(res,"Message Added.");
return;
}
else if(mode.equals("list"))
{
returnUserList(res);
return;
}
else
{
sendError(res,"Invalid Mode.");
return;
}
}

class ServerThread implements Runnable
{
public void run()
{
while(true)
{
Enumeration inactive=users.inactive(TIMEOUT);
while(inactive.hasMoreElements())
{
String user=(String)inactive.nextElement();
users.drop(user);
users.addMessage(new Message("Chat",user+ " has timed out."));
users.addMessage(new Message("Chat","Dropping" +user+ " from the chat."));
log("User"+user+"timed out. Dropped from chat.");
}

}
}
}

protected void sendError(HttpServletResponse res,String error)
{
try
{
PrintStream pout=new PrintStream(res.getOutputStream());
pout.print("ERR:"+error+"\r\n");
pout.flush();
pout.close();
}
catch(IOException e)
{
}
}

protected void sendResp(HttpServletResponse res,String resp)
{
try
{
res.setDateHeader("Expires",System.currentTimeMillis());
PrintStream pout=new PrintStream(res.getOutputStream());
pout.print(resp + "\r\n");
pout.flush();
pout.close();
}
catch(IOException e)
{
}
}

protected void returnMessages(HttpServletResponse res,Enumeration messagelist)
{
try
{
res.setDateHeader("Expires",System.currentTimeMillis());
PrintStream pout=new PrintStream(res.getOutputStream());
while(messagelist.hasMoreElements())
{
Message message=(Message)messagelist.nextElement();
pout.print(message+"\r\n");
}
pout.flush();
pout.close();
}
catch(IOException e)
{
}
}

protected void returnUserList(HttpServletResponse res)
{
try
{
res.setDateHeader("Expires",System.currentTimeMillis());
PrintStream pout=new PrintStream(res.getOutputStream());

Enumeration userlist=users.list();
while(userlist.hasMoreElements())
{
pout.print((String)userlist.nextElement());
pout.print("\r\n");
}
pout.flush();
}
catch(IOException e)
{
}
}
}

(applet)

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.awt.event.*;

public class ChatApplet extends Applet implements ActionListener
{
private Label title;
private Label Msg;
private Label userlist;
private Label defaultMsg;
private Label defaultgreating;
private Label logo;
private Label fontsetting;
private Label messagelabel;
private TextArea MsgArea;
private TextField message;
private Choice greating;
private Choice colour;
private GridBagLayout gbLayout;
private GridBagConstraints gbConstraints;
private String mymessage;

private List chatusers;
URL chatURL;
URLConnection connect;
volatile private boolean loggedin=false;
String username;
buildThread mythread;

public void init()
{
username=getParameter("user");

title=new Label("title");
userlist=new Label("User list");

Msg=new Label("Messages");
MsgArea=new TextArea("",5,50);

userlist=new Label("User List");
chatusers=new List(10,false);

defaultgreating=new Label("Default greating");
logo=new Label("Greating Logo");
fontsetting=new Label("Font Setting");

greating=new Choice();
message=new TextField();
message.addActionListener(this);
messagelabel=new Label("Input:");
colour=new Choice();
colour.add("blue");
colour.add("green");

gbLayout=new GridBagLayout();
gbConstraints=new GridBagConstraints();
setLayout(gbLayout);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(title,0,0,3,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(Msg,1,0,2,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(userlist,1,2,1,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(MsgArea,2,0,2,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(chatusers,2,2,1,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(defaultgreating,3,0,2,1);


gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(fontsetting,3,2,1,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(greating,4,0,2,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(colour,4,2,1,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(messagelabel,5,0,1,1);

gbConstraints.fill=GridBagConstraints.BOTH;
addComponent(message,5,1,2,1);
if(!username.equals(""))
{
mythread=new buildThread();
Thread t=new Thread(mythread);
t.start();
}
}


private void addComponent(Component c,int row,int column,int width,int height)
{
gbConstraints.gridx=column;
gbConstraints.gridy=row;
gbConstraints.gridwidth=width;
gbConstraints.gridheight=height;
gbLayout.setConstraints(c,gbConstraints);
this.add(c);
}

public void actionPerformed(ActionEvent e)
{
mymessage=e.getActionCommand();
message.setText("");
send();
}

private void send()
{
String gotmessage=mymessage;

if(gotmessage.equals(""))
return;
else
{
String queryString="http://127.0.0.1/examples/servlet/ChatServlet?mode=send&user="+URLEncoder.encode(username);
queryString=queryString+"&message="+URLEncoder.encode(gotmessage);
try
{
connect=(new URL(queryString)).openConnection();
connect.connect();
DataInputStream in=new DataInputStream(connect.getInputStream());
String response=in.readLine();

if(response.startsWith("Message Added"))
{
System.out.println("Message sent");
}
else
{
System.out.println("Error sending message: "+response);
}
}
catch(MalformedURLException e2)
{
System.out.println("MalformedURLException");
}
catch(IOException e)
{
System.out.println("send:IOException");
}
}
}

class buildThread implements Runnable
{
boolean success=false;

public buildThread()
{
success=login();
}

public void run()
{
while(success==true)
{
pollList();
poll();
System.out.println("Polling");
}
}

private boolean login()
{

String queryString="http://127.0.0.1/examples/servlet/ChatServlet?mode=login&user="+URLEncoder.encode(username);
System.out.println("Attempting login as " +username);
try
{
connect=(new URL(queryString)).openConnection();
connect.setDoInput(true);
connect.setDoOutput(false);
connect.connect();
DataInputStream in=new DataInputStream(connect.getInputStream());
String response=in.readLine();
System.out.println(response);
return true;
}
catch(MalformedURLException e)
{
System.out.println("MalformedURLException");
return false;
}
catch(IOException e)
{
System.out.println("IOException");
return false;
}

}

private void logout()
{
String queryString="http://127.0.0.1/examples/servlet/ChatServlet?mode=logout&user="+URLEncoder.encode(username);
try
{
connect=(new URL(queryString)).openConnection();
connect.setDoOutput(false);
connect.connect();
DataInputStream in=new DataInputStream(connect.getInputStream());
String response=in.readLine();

}
catch(MalformedURLException e)
{
System.out.println("logout: MalformedURLException");
}
catch(IOException e)
{
System.out.println("logout: IOException");
}
}

private void poll()
{
String queryString="http://127.0.0.1/examples/servlet/ChatServlet?mode=poll&user="+URLEncoder.encode(username);
try
{
DataInputStream in=new DataInputStream(new URL(queryString).openStream());

String nextLine=in.readLine();

while(nextLine!=null)
{
MsgArea.appendText(nextLine+"\r\n");
repaint();
nextLine=in.readLine();
}
}
catch(IOException e)
{
System.out.println("poll:IOException");
}
}

private void pollList()
{
String queryString="http://127.0.0.1/examples/servlet/ChatServlet?mode=list";
Vector users=new Vector();
try
{
URL listURL=new URL(queryString);
URLConnection listConn=listURL.openConnection();
listConn.setDefaultUseCaches(false);
listConn.setUseCaches(false);
listConn.connect();
DataInputStream in=new DataInputStream(listConn.getInputStream());

String nextLine=in.readLine();
while(nextLine!=null)
{
users.addElement(nextLine);
nextLine=in.readLine();
}

if(!users.isEmpty())
{
chatusers.clear();
int size=users.size();
for(int i=0;i<size;i++)
{
chatusers.addItem((String)users.elementAt(i));
}
}
else
{
chatusers.clear();
chatusers.addItem("None logged in");
}
repaint();
}
catch(IOException e)
{
System.err.println("polllist: Error Checking users");

}
}
}

}