• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code Review - ChatServlet

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic