Sir,
This is my code for the chat that I've manipulated
according to what runs on to my system.I've use dthe
same code for chat that is available in wrox book
chapter-09.I've use jdk1.3/jsdk2.1/javawebserver.the
chat updates room,creates room but in the room where
it has to chat doesn't works........Kindly plz compile
the java files and send the solution asap....
Amit
******************index.java**************************
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class index extends HttpServlet
{
String n1 = "/servlet/frontlines.ChatAdminServlet";
String n2 = "/servlet/frontlines.ListRoomsServlet";
String n3 = "/servlet/frontlines.ChatRoomServlet";
public void init(ServletConfig conf) throws
ServletException
{
super.init(conf);
}
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException,IOException
{
try
{
HttpSession session = req.getSession(true);
Vector v1 = new Vector();
Vector v2 = new Vector();
Vector v3 = new Vector();
Vector v4 = new Vector();
Vector v5 = new Vector();
res.setContentType("text/html");
PrintWriter pn=res.getWriter();
pn.println("<html><head><title>Chat
Application</title></head><body>");
pn.println("<h1>Chat Application</h1>");
pn.println("<p>Click <a
href=/servlet/frontlines.ChatAdminServlet>here</a>to
administer chat rooms.</p>");
pn.println("<p>Click <a
href=/servlet/frontlines.ListRoomsServlet>here</a>to
view current chat rooms,and to join a chat
room.</p>");
v1.addElement(n1);
v2.addElement(n2);
v3.addElement(n3);
session.putValue("sv1",v1);
session.putValue("sv2",v2);
session.putValue("sv3",v3);
System.out.println("V1 " +v1);
System.out.println("V2 " +v2);
System.out.println("V3 " +v3);
pn.println("</body></html>");
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
*********************ChatEntry.java********************import
java.util.*;
public class ChatEntry
{
private String profileName;
private String message;
public ChatEntry(String profileName , String message)
{
this.profileName = profileName;
this.message = message;
}
public String getProfileName()
{
return profileName;
}
public String getMessage()
{
return message;
}
}
************************ChatRoom.java******************
import java.util.*;
import java.util.Stack;
public class ChatRoom extends Stack
{
private String name;
private String description;
public ChatRoom(String name, String description , int
maxEntries)
{
this.name = name;
this.description = description;
this.setSize(maxEntries);
}
public void addChatEntry(ChatEntry entry)
{
push(entry);
}
public int getChatEntriesSize()
{
return elementCount;
}
public Enumeration getChatEntries()
{
return elements();
}
public String getDescription()
{
return description;
}
public String getName()
{
return name;
}
}
*******************ChatAdminServlet.java***************import
javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
public class ChatAdminServlet extends HttpServlet
{
String chatRoomPath;
String listRoomsPath;
String chatAdminPath;
public void init(ServletConfig config)throws
ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest
req,HttpServletResponse res)
throws IOException,ServletException
{
try
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession();
Vector v1 = new Vector();
Vector v2 = new Vector();
Vector v3 = new Vector();
v1 = (Vector)session.getValue("sv1");
v2 = (Vector)session.getValue("sv2");
v3 = (Vector)session.getValue("sv3");
chatAdminPath = (String)v1.elementAt(0);
listRoomsPath = (String)v2.elementAt(0);
chatRoomPath = (String)v3.elementAt(0);
out.println("<html>");
out.println("<head><title>Chat Room
Administration</title></head>");
out.println("<body>");
out.println("<h1>Chat room administration</h1>");
out.println("<form method = \"Post\" action =\""
+ res.encodeURL(chatAdminPath) + "\">");
Hashtable roomList
(Hashtable)getServletContext().getAttribute("roomList");
if(roomList!=null)
{
Enumeration rooms = roomList.keys();
if(!rooms.hasMoreElements())
{
out.println("<p>There are no rooms</p>");
}
else
{
out.println("<p>Check the rooms you would like to
remove and press update List.</p>");
while(rooms.hasMoreElements())
{
String roomName = (String)rooms.nextElement();
ChatRoom room = (ChatRoom)roomList.get(roomName);
out.println("<input type = checkbox name = remove
value = '" + room.getName() +"'>"+room.getName()
+"<br>");
}
}
}
out.println("<p> Enter a new room and the
description</p>");
out.println("<table>");
out.println("<tr><td>Name: <input type=text
name=roomname size=20></td></tr>");
out.println("<tr><td>Description<textarea rows=5
name=roomdescr cols=40></textarea></td>");
out.println("</tr></table>");
out.println("<p><input type=submit value='Update
List' name=B1></p>");
out.println("<a href =\"" + listRoomsPath + "\">Chat
Now </a>");
out.println("</form></body></html>");
out.close();
}catch(Exception e)
{
System.out.println(e);
}
}
public void doPost(HttpServletRequest
req,HttpServletResponse res)
throws IOException,ServletException
{
Hashtable roomList = null;
synchronized (getServletContext())
{
roomList = (Hashtable)
getServletContext().getAttribute("roomList");
if(roomList == null)
{
roomList = new Hashtable();
getServletContext().setAttribute("roomList",roomList);
}
}
String[] removeList
req.getParameterValues("remove");
synchronized(roomList)
{
if(removeList!=null)
{
for(int i=0;i<removeList.length;i++)
{
roomList.remove(removeList[i]);
}
}
}
String roomName = req.getParameter("roomname");
String roomDescr = req.getParameter("roomdescr");
if(roomName!=null && roomName.length()>0)
{
synchronized(roomList)
{
roomList.put(roomName,new ChatRoom
roomName,roomDescr,4));
}
}
doGet(req,res);
}
}
****************ListRoomsServlet.java******************
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.net.URLEncoder;
public class ListRoomsServlet extends HttpServlet
{
String chatAdminPath;
String listRoomsPath;
String chatRoomPath;
ServletContext context;
public void init(ServletConfig config)throws
ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest
req,HttpServletResponse res)
throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String expand = req.getParameter("expand");
System.out.println("List Expand " + expand);
HttpSession session = req.getSession();
String profileName = (String)
session.getValue("profileName");
System.out.println("List Profilename " + profileName);
Vector v1 = new Vector();
Vector v2 = new Vector();
Vector v3 = new Vector();
v1 = (Vector)session.getValue("sv1");
v2 = (Vector)session.getValue("sv2");
v3 = (Vector)session.getValue("sv3");
System.out.println(v1);
chatAdminPath = (String)v1.elementAt(0);
listRoomsPath = (String)v2.elementAt(0);
chatRoomPath = (String)v3.elementAt(0);
System.out.println(chatRoomPath);
if(profileName==null)
{
profileName = "";
}
out.println("<html>");
out.println("<head><title>Chat
Rooms</title></head>");
out.println("<body>");
out.println("<h1>Chat rooms</h1>");
out.println("<form method = \"Post\" action =\""
+chatRoomPath + "\">");
out.println("<a href=\"" + chatAdminPath + "\">Create
Rooms</a></p>");
Hashtable roomList
(Hashtable)getServletContext().getAttribute("roomList");
if(roomList==null)
{
out.println("<p>There are no rooms available right
now.</P>");
}
else
{
out.println("Select the room you like to enter or
click to see the description:</P>");
Enumeration rooms = roomList.keys();
boolean isFirst = true;
while(rooms.hasMoreElements())
{
String roomName = (String)rooms.nextElement();
ChatRoom room = (ChatRoom)roomList.get(roomName);
String listRoomsURL = listRoomsPath + "/?expand="
+ URLEncoder.encode(roomName);
listRoomsURL = res.encodeURL(listRoomsURL);
out.println("<input type = radio name = roomname
value=\"" +roomName+"\"" +(isFirst ? "CHECKED" : "")
+">"
+"<a href =\"" + listRoomsURL + "\">" + roomName
+"</a><br>");
isFirst = false;
if(expand !=null && expand.equals(roomName))
{
out.println("<BlockQuote>");
if(room.getDescription().length()==0)
{
out.println("No description available.");
}
else
{
out.println(room.getDescription());
}
out.println("</BlockQuote>");
}
}
out.println("<p> Enter your name:</p>");
out.println("<input name=profileName value
'"+profileName+"' size=30>");
out.println("<input type=submit value='Enter'>");
out.println("</form>");
}
out.println("</body></html>");
out.close();
}
}
********************ChatRoomServlet.java***************
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.net.URLEncoder;
public class ChatRoomServlet extends HttpServlet
{ String chatAdminPath;
String listRoomsPath;
String chatRoomPath;
public void init(ServletConfig config)throws
ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest
req,HttpServletResponse res)
throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession();
Vector v1 = new Vector();
Vector v2 = new Vector();
Vector v3 = new Vector();
v1 = (Vector)session.getValue("sv1");
v2 = (Vector)session.getValue("sv2");
v3 = (Vector)session.getValue("sv3");
chatAdminPath = (String)v1.elementAt(0);
listRoomsPath = (String)v2.elementAt(0);
chatRoomPath = (String)v3.elementAt(0);
ChatRoom room = getRoom(req,res);
if(room == null)
{
return;
}
String listPar = req.getParameter("list");
if(listPar !=null && listPar.equals("true"));
{
writeMessages(out,room,getProfileName(req));
}
out.println("<body>");
out.println("<form method = \"Post\" action =\""+
res.encodeURL(chatRoomPath) + "\" Target=\"_top\">");
out.println("<p> Enter your message:</p>");
out.println("<input name=\"msg\" size=\"30\">");
out.println("<p><input type=submit value='Send
Message'>");
out.println("</form>");
out.println("<form action=\"" +
res.encodeURL(listRoomsPath)+ "\" Method = \"Get\"
Target=\"_top\">");
out.println("<input type=submit value=Exit>");
out.println("</form>");
out.println("</body></html>");
out.close();
}
public void doPost(HttpServletRequest
req,HttpServletResponse res)
throws IOException,ServletException
{
res.setContentType("text/html");
ChatRoom room = getRoom(req,res);
if(room == null)
{
return;
}
String profileName = getProfileName(req);
String msg = req.getParameter("msg");
if(msg!=null && msg.length()!=0)
{
room.addChatEntry(new ChatEntry(profileName,msg));
}
writeFrame(res,room);
}
private String getProfileName(HttpServletRequest req)
{
HttpSession session = req.getSession(true);
String profileName
(String)session.getValue("profileName");
if(profileName == null)
{
profileName = req.getParameter("profileName");
if(profileName == null || profileName.length()==0)
{
profileName = "A spineless spy";
}
session.putValue("profileName",profileName);
}
else
{
String newName = req.getParameter("profileName");
if(newName!=null && newName.length() >0 &&
!newName.equals(profileName))
{
profileName = newName;
session.putValue("profileName",profileName);
}
}
return profileName;
}
private ChatRoom getRoom(HttpServletRequest
req,HttpServletResponse res)throws IOException
{
HttpSession session = req.getSession(true);
PrintWriter out = res.getWriter();
String roomName
(String)session.getValue("roomName");
if(roomName==null)
{
roomName=req.getParameter("roomName");
if(roomName == null || roomName.length() ==0)
{
writeError(out,"Room not specified");
return null;
}
session.putValue("roomName",roomName);
}
else
{
String newRoom = req.getParameter("roomName");
if(newRoom!=null && newRoom.length() >0 &&
!newRoom.equals(roomName))
{
roomName = newRoom;
session.putValue("roomName",roomName);
}
}
Hashtable roomList
(Hashtable)getServletContext().getAttribute("roomList");
ChatRoom room = (ChatRoom)roomList.get(roomName);
if(roomList!=null)
{
writeError(out,"Room" + roomName + "not found");
return null;
}
return room;
}
private void writeError(PrintWriter out , String msg)
{
out.println("<html>");
out.println("<head><title>Error</title></head>");
out.println("<body>");
out.println("<h1>Error</h1>");
out.println(msg);
out.println("</body></html>");
}
private void writeFrame(HttpServletResponse
res,ChatRoom room)throws IOException
{
//res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head><title>"+room.getName()+"</title></head>");
out.println("<Frameset rows ='50%,50%' Border=0
FrameBorder=No>");
out.println("<Frame src=\""
+res.encodeURL(chatRoomPath)+"?list=true\"
name=\"list\" scrolling=\"Auto\">");
out.println("<Frame src=\""
+res.encodeURL(chatRoomPath)+"?list=false\"
name=\"list\" scrolling=\"Auto\">");
out.println("<NoFrames>");
out.println("<body>");
out.println("Viewing this requires a browser capable
of displaying frames");
out.println("</body>");
out.println("</NoFrames>");
out.println("</Frameset>");
out.println("</html>");
out.close();
}
private void writeMessages(PrintWriter out,ChatRoom
room,String profileName)
{
StringBuffer sb = new StringBuffer();
out.println("<html>");
out.println("<head><Meta http-eqv=\"refresh\"
content=\"5\"></head>");
out.println("<body>");
out.println("<b>Room: " +room.getName() + "</b><br>" +
"<b>Identity:"+profileName + "</b><br>");
if(room.size()==0)
{
out.println("<font color =red>There are no messages in
this room yet</font>");
}
else
{
Iterator entries = room.iterator();
while(entries.hasNext())
{
ChatEntry entry = (ChatEntry)entries.next();
if(entry == null)
{
continue;
}
String entryName = entry.getProfileName();
if(entryName.equals(profileName))
{
out.print("<Font color = Blue>");
}
out.println(entryName + " :" + entry.getMessage() +
"<br>");
if(entryName.equals(profileName))
{
out.print("<font>");
}
}
}
out.println("</body></htmL>");
}
}*******************************************************
__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/