简单聊天室的实现

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

%>


 

 

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
简易聊天室本次实验的目的是通过以下题目掌握JSP内置对象,包括:request,response,session,application等。 (1)制作简易聊天室,能够实现简单的页面聊天功能。 (2)制作网页计数器,要求相同的窗口内刷新页面访问次数并不增加,并且用图片来显数字。1、 熟悉request、response、session、application、out等内置对象; 2、 选择制作网页计数器程序需准备数字图片;1、进入jsp子目录,编写简易聊天室的JSP程序,聊天室的需要实现的基本功能:输入昵称、聊天。 2.根据功能编写页面代码。二、网页计算器 利用内置对象application <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Counter.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 您是第位访问者! </body> </html> 简易聊天室本次实验的目的是通过以下题目掌握JSP内置对象,包括:request,response,session,application等。 (1)制作简易聊天室,能够实现简单的页面聊天功能。 (2)制作网页计数器,要求相同的窗口内刷新页面访问次数并不增加,并且用图片来显数字。1、 熟悉request、response、session、application、out等内置对象; 2、 选择制作网页计数器程序需准备数字图片;1、进入jsp子目录,编写简易聊天室的JSP程序,聊
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值