目标
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加入上下文字符串)
判断是否存在该变量,存在则直接读取现有聊天内容,把数据加入进去后,重新写回上下文变量 - 实现拍一拍功能(目的:学习如何进行链接操作)
每个人名做成一个链接,访问某一个servlet,该servlet可以在聊天信息(上下文变量)里添加一行,xx拍了yyy一下,跳转回message.jsp
问题:链接里应该包含yyy参数,servlet?nick=yyy
say = "<a href='NickServlet?nickname="+nickname+"'target='_parent'>"+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="发送"><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;
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 {
}
}
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;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 = "<a href='NickServlet?nickname="+nickname+"'target='_parent'>"+nickname+"</a>"+":"+text;
String says = new String();
//将昵称和聊天信息一起放在上下文变量
ServletContext context = getServletContext();
if(context.getAttribute("says")!=null){
says = context.getAttribute("says") + say + "<br>";
}
else{
says = say + "<br>";
}
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");
//将拍一拍放到上下文变量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+"<br>";
ServletContext context = request.getServletContext();
String says = context.getAttribute("says")+pick;
context.setAttribute("says",says);
RequestDispatcher dispatcher = request.getRequestDispatcher("/chatroom.jsp");
dispatcher.forward(request, response);
}
}
编程小白,有错误请大佬指出…
本人原创,欢迎转载~