作者: | 来源:互联网 | 2023-10-09 21:44
原标题:java-web实现简单聊天室(含拍一拍功能)
目标
servlet、jsp实现简单聊天室,用户通过浏览器登录后进入聊天室,可发送消息进行群聊,点击聊天信息框中的用户名可实现拍一拍功能。
效果
基础知识
-
数据的存取 setAttribute / getAttribute
-
request请求对象 :有效时间短
ServletContext上下文对象:一直存在于服务器,存储公有、 共享数据
Session会话对象:独立
-
网站默认页面一般是index.jsp
-
访问作用域变量:在JSP页面中,可使用JSP表达式访问作用域变量。一般做法:在Servlet中使用setAttribute()将一个变量存储到某个作用域对象上,如HttpServletRequest、HTTPSession及ServletContext等。然后使用RequestDispatcher对象的forward()将请求转发到JSP页面,在JSP页面中调用隐含变量的getAttribute()返回作用域变量的值。
实现思路
-
登录页面 login.jsp 输入昵称
-
编写一个LoginSevlet,处理登录提交的昵称,保存登录昵称到session(会话变量)
若昵称合法 if(nickname.length()!=0),跳转到聊天室,不合法回到登录页面
-
编写聊天室页面chatroom.jsp ,是一个框架,把多个页面集成到一个页面中
<iframe src="message.jsp" width="100%" height="70%"> iframe>
<iframe src="input.jsp" width="100%" height="25%"> iframe>
- 聊天内容显示message.jsp,不断自动刷新标签内设置响应头,用${上下文变量}显示聊天内容
<meta http-equiv="refresh" content="1">
- input.jsp(不刷新)输入聊天内容文本框text,点击发送聊天内容按钮时,将把文本框的聊天信息提交给ChatServlet处理。下方有超链接可退出聊天室。
<a href="login.jsp" target="_top">退出聊天室a>
- ChatServlet:检测聊天信息合法性,把聊天信息加入到ServletContext变量(字符串)中,(每次都将xx:xxxx加入上下文字符串)
判断是否存在该变量,存在则直接读取现有聊天内文章来源地址34990.html容,把数据加入进去后,重新写回上下文变量
- 实现拍一拍功能(目的:学习如何进行链接操作)
每个人名做成一个链接,访问某一个servlet,该servlet可以在聊天信息(上下文变量)里添加一行,xx拍了yyy一下,跳转回message.jsp
问题:链接里应该包含yyy参数,servlet?nick=yyy
say = ""+nickname+"a>"+":"+text;
- 多人聊天实现:多种类型浏览器
关键代码
1. login.jsp
昵称:<input type="text" name="nickname"/><br> <br><br>
<input type="submit" value="登录"/>
<input type="reset" value="取消"/>
2. input.jsp
<form action="input.do" method="post">
<input type="text" size="50" name="text"/>
<input type="submit" value="www.yii666.com发送"><br><br><br><br>
<a href="login.jsp" target="_top">退出聊天室a>
form>
3. message.jsp
<h3>一起来聊天吧h3>
${says}
4. LoginServlet.java
package chatting;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
imwww.yii666.comport javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "LoginServlet2",urlPatterns = {"/login.do"})
public class LoginServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String nickname = request.getParameter("nickname");
HttpSession session = request.getSession();
session.setAttribute("nickname",nickname);
if(nickname.length()!=0){
request.getRequestDispatcher("/chatroom.jsp").forward(request,resp文章来源站点https://www.yii666.com/onse);
}
else{
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
5. ChatServlet.java
package chatting;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Vector;
@WebServlet(name = "ChatServlet",urlPatterns = "/input.do")
public class ChatServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;chars文章来源地址34990.htmlet=utf-8");
HttpSession session = request.getSession();
String text = request.getParameter("text");
if (text.length()!=0) {
String say =new String();
String nickname = (String)session.getAttribute("nickname");
say = ""+nickname+""+":"+text;
String says = new String();
ServletContext context = getServletContext();
if(context.getAttribute("says")!=null){
says = context.getAttribute("says") + say + "
";
}
else{
says = say + "
";
}
context.setAttribute("says", says);
request.setAttribute("says", says);
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/input.jsp");
dispatcher.forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
6. NickServlet.java
package chatting;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "NickServlet",urlPatterns = "/NickServlet")
public class NickServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String nickname = (String)session.getAttribute("nickname");
String name = java.net.URLDecoder.decode(request.getParameter("nickname"),"UTF-8");
String pick = nickname+"拍了拍"+name+"
";
ServletContext context = request.getServletContext();
String says = context.getAttribute("says")+pick;
context.setAttribute("says",says);
RequestDispatcher dispatcher = request.getRequestDispatcher("/chatroom.jsp");
dispatcher.forward(request, response);
}
}
编程小白,有错误请大佬指出…
本人原创,欢迎转载~
来源于:java-web实现简单聊天室(含拍一拍功能)