aspose file tools*
The moose likes Servlets and the fly likes Code Review - ChatServlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Code Review - ChatServlet" Watch "Code Review - ChatServlet" New topic
Author

Code Review - ChatServlet

Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
All - I read a few messages recently about thread-safety and about doing a chat servlet, so I whipped one out and thought maybe a few of y'all could try it out and review it. I'd especially like to know about the thread-safeness of this since the message store (chat log?) is using an instance variable and is memory-resident. It can be extended in all sorts of ways - this was just a first pass proof-of-concept type thing, and something that everyone can pick on to help me (and everyone else) learn proper servlet writing techniques. With that, review away! (hopefully the code will post - it's a little long, and I don't have a personal website that I can post it to)


CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
BTW, to test the code, it's best to open two or more different browser windows. Everything is form-based, so it doesn't use sessions.
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
What would be the best way to save the chat log as a continuously appended file to avoid restarts due to server reset?
Is there a simple way to turn this servlet into a RMI server and client as well, so that a chat servlet on another server could share the chat session?
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Just thought of something else - what would be the easiest way to restrict access to certain users? Would it be better to use a .properties file, do it in the web.xml descriptor, or hard-code it? Any ideas for non-SSL encryption (password hiding) schemes?
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
I guess using the ServletContext to store the the user list and hold the chat messageLog object might be better than using a global in the servlet? Or would it? Also, what is the lifetime of a ServletContext? Is it longer than the servlet, or the same?
Is there a way to init (ie, do pre-processing and populate objects into) the ServletContext when a web-app (.war) loads, independent of the ChatServlet?
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Hi Gery, i tried ur code but got several compiler errors possibly due to formatting error! Can u send me the code in .doc format(MS-WORD)?

------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Sent you an email with .DOC and .java attached. The formatting problem is the logical OR is '| |', whereas when you cut/paste the code it somehow ends up as '| |'. Just remove the space and it should compile fine.
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
New Version! I changed it to not allow blank submits, and it now uses a table for display so it can show the current users on the right-hand side. I should probably come up with a timer to 'expire' users from the userList and force them to login again if they try to submit after X minutes.
Here it is:
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I tried to save this code and run, but it is giving some error
import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;public class ChatServlet extends HttpServlet{private static int maxMessages = 100; // max to save in memory at any one timeprivate static String defaultUserMax = "25"; // default user to show only this many messagesprivate static String defaultUserRefresh = "5"; // default user to refesh this oftenprivate
ArrayList messageLog = new ArrayList(); //holds up to maxMessages messages
It is highlighting messageLog object of ArrayList class. It is
not saying any error. but it is not compiling whereas all the other methods are compiling
does anyone have any idea
can you email me your code in word format


Bhasker Reddy
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
i got some more information it says
FIELD TYPE ARRAYLIST IS MISSING.
WHAT COULD BE THE ERROR, may be my version of java is not having
arraylist
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
This program requires JDK 1.2 at a minimum. Maybe try changing ArrayList to Vector? You should be able to look at the code and see if there's any weird formatting errors. Also, you could try cutting out the body of doGet() and doPost() as well as removing doLogin(), doChat(), and other stuff and see if that helps it compile. I'm on my way out the door for the weekend, so if you still aren't able to get it working, let me know Monday and I'll email the code then. Gotta run...
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
what are the changes i need to make, if i want to use Vector
instead of ArrayList, MY IDE visual age 3.0 doesn't support
ArrayList.
can someone guide me
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
They should be equivalent (both are subclassed from AbstractList). Just search/replace ArrayList with Vector and see how it works. If a method isn't supported, just look at the APIs and you should see the proper method to subsitute:
http://java.sun.com/j2se/1.3/docs/api/java/util/package-summary.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code Review - ChatServlet