简单聊天室的实现

时间:2022-12-24 10:08:41

1:创建login.jsp
2:创建doLogin.jsp
3:创建聊天室主页面:main.jsp,send.jsp,list.jsp,msg.jsp
4:完善点:
  1:实现退出登录功能:loginOff.jsp
  2:将验证用户是否登录的逻辑抽离出来写成:check.jsp,在相关页面通过include指令包含进来
  3:默认调用main.jsp,修改web.xml中欢迎页面标签
  4:同一台机器的不同浏览器可以有不同的登录用户

login.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String msg=(String)request.getAttribute("msg");
if(msg==null)
{
msg="";
}

%>
<center>
<h1><%=msg%></h1>
<form action="doLogin.jsp" method="post" name="loginForm">
用户名:<input type="text" name="username"/> <br/>
密   码:<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</center>
</body>
</html>



 

doLogin.jsp

<body>
<%

request.setCharacterEncoding("utf-8");
//获取表单中提交过来的值
String username=request.getParameter("username");
String pass=request.getParameter("password");
//判断用户名和密码是否为空,返回登陆页面
if(username.trim().length()==0||pass.trim().length()==0)
{
//设置提示信息
request.setAttribute("msg","请输入用户名或密码");
//转发到login.jsp
request.getRequestDispatcher("login.jsp").forward(request,response);
return;
}
//从applicaton作用域中取出用户列表
List users=(List)application.getAttribute("users");
//如果该用户列表还不存在,实例化该用户列表
if(users==null)
{
users=new ArrayList();
}
//查看当前列表中是否包含当前的登陆用户
if(users.contains(username))
{
//设置提示信息
request.setAttribute("msg","该用户已经登陆,请重新登陆");
//转发到login.jsp
request.getRequestDispatcher("login.jsp").forward(request,response);
return;

}
//将当前登陆用户名加入该用户列表
users.add(username);
//将用户列表存放在application作用域中
application.setAttribute("users",users);
//替换特殊字符
username.replaceAll("<","<");
username.replaceAll(">",">");
//要将当前登陆用户存入session作用域
session.setAttribute("user",username);
//重定向到主页面
response.sendRedirect("main.jsp");

%>
</body>
</html>



 

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="check.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<frameset rows="80%,*">
<frameset cols="80%,*">
<frame src="msg.jsp"/>
<frame src="list.jsp"/>
</frameset>
<frame src="send.jsp"/>
</frameset>
</html>


 

send.jsp

<body>
<form action="" method="post">
发送消息:<input type="text" name="message" size="50"/><br/>
<input type="submit" value="发送"/>
</form>
<%

//post请求 设置中文
request.setCharacterEncoding("utf-8");
//获取消息字符串
String message = request.getParameter("message");
//从application作用域中取出消息对象
StringBuffer sb=(StringBuffer)application.getAttribute("msgs");
if(sb==null)
{
sb=new StringBuffer();
}
if(message!=null && ! message.equals(""))
{
//拼接消息为:[**人]:+ 消息
sb.append("["+user+"]说:"+message+"<br/>");
}
//将sb对象存入application作用域
application.setAttribute("msgs",sb);

%>
</body>
</html>

list.jsp

<body>

<%


//从applicaton作用域中取出用户列表
List users=(List)application.getAttribute("users");
//遍历该用户列表
for(int i=0;i<users.size();i++)
{
String username=(String)users.get(i);
out.print("<li>"+username);
if(username.equals(user))
{
out.print("<a href='loginOff.jsp' target='_parent'>退出</a>");
}
}

%>


</body>
</html>


 

msg.jsp

<body>
<%
//从application作用域取出消息字符串
StringBuffer sb=(StringBuffer)application.getAttribute("msgs");
String msg="";
if(sb!=null)
{
msg=sb.toString();
}
else
{
msg="暂无聊天记录";
}
%>
<%=msg%>
</body>
</html>


 

loginOff.jsp

<body>
<%

//从applicaton作用域中取出用户列表
List users=(List)application.getAttribute("users");
String username=(String)session.getAttribute("user");
//从该用户列表中移除该用户
users.remove(username);
application.setAttribute("users",users);
//注销该用户的会话
session.invalidate();

%>
<h1>已经退出聊天室,请点击<a href="login.jsp">这里,重新登录!</a> </h1>
</body>
</html>


 

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%

String user=(String)session.getAttribute("user");
if(user==null)
{
request.getRequestDispatcher("login.jsp").forward(request,response);
return;
}

%>