java web实现简单聊天室
目标
servlet、jsp实现简单聊天室,用户通过浏览器登录后进入聊天室,可发送消息进行群聊,点击聊天信息框中的用户名可实现拍一拍功能。
基础知识
- 数据的存取setAttribute/getAttribute
- request请求对象:有效时间短
ServletContext上下文对象:一直存在于服务器,存储公有、共享数据
Session会话对象:独立
- 网站默认页面一般是index.jsp
实现思路
1.登录页面login.jsp输入昵称
2.编写一个LoginSevlet,处理登录提交的昵称,保存登录昵称到session(会话变量)
若昵称合法if(nickname.length()!=0),跳转到聊天室,不合法回到登录页面
3.编写聊天室页面chatroom.jsp,是一个框架,把多个页面集成到一个页面中
4.聊天内容显示message.jsp,不断自动刷新标签内设置响应头,用${上下文变量}显示聊天内容
5.input.jsp(不刷新)输入聊天内容文本框text,点击发送聊天内容按钮时,将把文本框的聊天信息提交给ChatServlet处理。下方有超链接可退出聊天室。
退出聊天室
6.ChatServlet:检测聊天信息合法性,把聊天信息加入到ServletContext变量(字符串)中,(每次都将xx:xxxx加入上下文字符串)
判断是否存在该变量,存在则直接读取现有聊天内容,把数据加入进去后,重新写回上下文变量
7.实现拍一拍功能(目的:学习如何进行链接操作)
每个人名做成一个链接,访问某一个servlet,该servlet可以在聊天信息(上下文变量)里添加一行,xx拍了yyy一下,跳转回message.jsp
问题:链接里应该包含yyy参数,servlet?nick=yyy
say=""+nickname+""+":"+text;
8.多人聊天实现:多种类型浏览器
关键代码
login.jsp
昵称:
input.jsp
退出聊天室
message.jsp
一起来聊天吧
${says}
LoginServlet.java
packagechatting; importjavax.servlet.RequestDispatcher; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.servlet.http.HttpSession; importjava.io.IOException; importjava.io.PrintWriter; @WebServlet(name="LoginServlet2",urlPatterns={"/login.do"}) publicclassLoginServlet2extendsHttpServlet{ protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //避免乱码 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); //nickname保存用户昵称 Stringnickname=request.getParameter("nickname"); //nickname存储到session变量中 HttpSessionsession=request.getSession(); session.setAttribute("nickname",nickname); //对nickname进行合法性判断 if(nickname.length()!=0){ //昵称合法,则进入聊天室页面 request.getRequestDispatcher("/chatroom.jsp").forward(request,response); } else{ //昵称不合法,返回登陆页面 request.getRequestDispatcher("/login.jsp").forward(request,response); } } protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ } }
ChatServlet.java
packagechatting; importjavax.servlet.RequestDispatcher; importjavax.servlet.ServletContext; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.servlet.http.HttpSession; importjava.io.IOException; importjava.util.Vector; @WebServlet(name="ChatServlet",urlPatterns="/input.do") publicclassChatServletextendsHttpServlet{ protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //防止乱码 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); HttpSessionsession=request.getSession(); Stringtext=request.getParameter("text"); //text存储聊天信息,并进行合法性判断 if(text.length()!=0){ Stringsay=newString(); //获取session中的昵称 Stringnickname=(String)session.getAttribute("nickname"); //将昵称作为链接,并传参 say=""+nickname+""+":"+text; Stringsays=newString(); //将昵称和聊天信息一起放在上下文变量 ServletContextcontext=getServletContext(); if(context.getAttribute("says")!=null){ says=context.getAttribute("says")+say+"
"; } else{ says=say+"
"; } context.setAttribute("says",says); request.setAttribute("says",says); } //转发 RequestDispatcherdispatcher=request.getRequestDispatcher("/input.jsp"); dispatcher.forward(request,response); } protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ } }
NickServlet.java
packagechatting; importjavax.servlet.RequestDispatcher; importjavax.servlet.ServletContext; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.servlet.http.HttpSession; importjava.io.IOException; importjava.io.PrintWriter; @WebServlet(name="NickServlet",urlPatterns="/NickServlet") publicclassNickServletextendsHttpServlet{ protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ } protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("utf-8"); //将拍一拍放到上下文变量says中一起输出 HttpSessionsession=request.getSession(); //nickname存储当前用户昵称 Stringnickname=(String)session.getAttribute("nickname"); //name存储连接中传递的参数,即被拍的用户昵称 Stringname=java.net.URLDecoder.decode(request.getParameter("nickname"),"UTF-8"); Stringpick=nickname+"拍了拍"+name+"
"; ServletContextcontext=request.getServletContext(); Stringsays=context.getAttribute("says")+pick; context.setAttribute("says",says); RequestDispatcherdispatcher=request.getRequestDispatcher("/chatroom.jsp"); dispatcher.forward(request,response); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。