用java web实现聊天室_java web实现简单聊天室「建议收藏」

2022-06-25 13:08:16 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

目标

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

package chatting;

import javax.servlet.RequestDispatcher;

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 = “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”);

//nickname保存用户昵称

String nickname = request.getParameter(“nickname”);

//nickname存储到session变量中

HttpSession session = 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);

}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

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;charset=utf-8”);

HttpSession session = request.getSession();

String text = request.getParameter(“text”);

//text存储聊天信息,并进行合法性判断

if (text.length()!=0) {

String say =new String();

//获取session中的昵称

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 {

}

}

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”);

//将拍一拍放到上下文变量says中 一起输出

HttpSession session = request.getSession();

//nickname存储当前用户昵称

String nickname = (String)session.getAttribute(“nickname”);

//name存储连接中传递的参数,即被拍的用户昵称

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);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152198.html原文链接:https://javaforall.cn

0 人点赞