您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > Ajax_web聊天室-Java
1.介绍:本文的主要目的就是通过Java语言开发基于html通信的聊天程序,主要用到了ajax,html通信,java,jsp。穿越防火墙,在web页面进行通信,没有利用socket,不用另外开设端口进行通信。仅仅利用浏览器的80端口就可以搞定。下面列出主要的代码结构:下面给出涉及到的各个代码:1.ChatService.java类:packagelee;importjava.util.*;importjava.io.*;/**@version1.0*/publicclassChatService{privatestaticChatServicecs;privatePropertiesuserList;privateLinkedListStringchatMsg;privateChatService(){}publicstaticChatServiceinstance(){if(cs==null){cs=newChatService();}returncs;}publicbooleanvalidLogin(Stringuser,Stringpass)throwsIOException{if(loadUser().getProperty(user)==null){returnfalse;}if(loadUser().getProperty(user).equals(pass)){returntrue;}returnfalse;}publicbooleanaddUser(Stringname,Stringpass)throwsException{if(userList==null){userList=loadUser();}if(userList.containsKey(name)){thrownewException(用户名已经存在,请重新选择用户名);}userList.setProperty(name,pass);saveUserList();returntrue;}publicStringgetMsg(){if(chatMsg==null){chatMsg=newLinkedListString();return;}Stringresult=;for(Stringtmp:chatMsg){result+=tmp+\n;}returnresult;}publicvoidaddMsg(Stringuser,Stringmsg){if(chatMsg==null){chatMsg=newLinkedListString();}if(chatMsg.size()40){chatMsg.removeFirst();}chatMsg.add(user+说:+msg);}////////////////////////////////////////////////////////////////下面是系统的工具方法/////////////////////////////////////////////////////////////privatePropertiesloadUser()throwsIOException{if(userList==null){Filef=newFile(userFile.properties);if(!f.exists())f.createNewFile();userList=newProperties();userList.load(newFileInputStream(f));}returnuserList;}privatebooleansaveUserList()throwsIOException{if(userList==null){returnfalse;}userList.store(newFileOutputStream(userFile.properties),userList);returntrue;}}2.ChatServlet.javapackagelee;importjavax.servlet.ServletException;importjavax.servlet.ServletContext;importjavax.servlet.RequestDispatcher;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.PrintWriter;importjava.io.IOException;/**@version1.0*brDate:*/publicclassChatServletextendsHttpServlet{publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{request.setCharacterEncoding(UTF-8);Stringmsg=request.getParameter(chatMsg);if(msg!=null&&!msg.equals()){Stringuser=(String)request.getSession(true).getAttribute(user);ChatService.instance().addMsg(user,msg);}//设置中文流response.setContentType(text/html;charset=GBK);PrintWriterout=response.getWriter();out.println(ChatService.instance().getMsg());}}3.LoginServlet.javapackagelee;importjavax.servlet.ServletException;importjavax.servlet.ServletContext;importjavax.servlet.RequestDispatcher;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.PrintWriter;importjava.io.IOException;/**@author*brDate:*/publicclassLoginServletextendsHttpServlet{publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{Stringname=request.getParameter(name);Stringpass=request.getParameter(pass);if(name==null||pass==null){request.setAttribute(error,用户名和密码都不能为空);forward(/index.jsp,request,response);}if(ChatService.instance().validLogin(name,pass)){request.getSession(true).setAttribute(user,name);request.setAttribute(msg,ChatService.instance().getMsg());forward(/chat.htm,request,response);}else{request.setAttribute(error,用户名和密码不匹配);forward(/index.htm,request,response);}}publicvoidforward(Stringurl,HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ServletContextsc=getServletConfig().getServletContext();RequestDispatcherrd=sc.getRequestDispatcher(url);rd.forward(request,response);}}4.RegServlet.javapackagelee;importjavax.servlet.ServletException;importjavax.servlet.ServletContext;importjavax.servlet.RequestDispatcher;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.PrintWriter;importjava.io.IOException;/**@author*brDate:*/publicclassRegServletextendsHttpServlet{publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{Stringname=request.getParameter(name);Stringpass=request.getParameter(pass);if(name==null||pass==null){request.setAttribute(tip,用户名和密码都不能为空);forward(/reg.jsp,request,response);}try{if(ChatService.instance().addUser(name,pass)){request.setAttribute(tip,注册成功,请登陆系统);forward(/reg.jsp,request,response);}else{request.setAttribute(tip,无法正常注册,请重试);forward(/reg.jsp,request,response);}}catch(Exceptione){request.setAttribute(tip,e.getMessage());forward(/reg.jsp,request,response);}}publicvoidforward(Stringurl,HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ServletContextsc=getServletConfig().getServletContext();RequestDispatcherrd=sc.getRequestDispatcher(url);rd.forward(request,response);}}chat.htm!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//EN
本文标题:Ajax_web聊天室-Java
链接地址:https://www.777doc.com/doc-5962445 .html