JavaEE项目 Web聊天室(JSP实现)

壁画不多说,直接上代码:

这里只放了部分核心代码:

聊天页面:

<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2020/5/4
  Time: 0:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        html
        {
            width: 1920px;
            height: 1080px;
            background-image: url("11546722.jpg");
        }
        .total
        {
            background-color: white;
            float: left;
            width: 840px;
            height: 740px;
            border: 2px solid black;
            box-shadow: 0 0 10px black;
            margin-top: 150px;
            margin-left: 500px;
        }
        .show
        {
            width: 700px;
            height: 560px;
        }
        .frame
        {
            width: 700px;
            height: 560px;
            /*border: 1px solid red;*/
        }
        .message
        {
            width: 700px;
            height: 180px;
        }
        .contentbox
        {
            border-width: 2px;;
            width: 696px;
            height: 125px;
        }
        .submitbuttom
        {
            float: right;
            margin-top: 5px;
            margin-bottom: 5px;
            width: 90px;
            height: 30px;
            background-color: rgb(124,108,87);
        }
        .people
        {
            float: right;
            width: 140px;
            height: 100%;
            /*background-color: #4dde3c;*/
        }
        .frame02
        {
            height: 100%;
            width: 140px;
        }
    </style>
</head>
<body>
<div class="total">
    <div class="people">
        <iframe src="./showpoeple.jsp" frameborder="0" class="frame02"></iframe>
    </div>
    <div class="show">
        <iframe src="./record.jsp" frameborder="0" class="frame" name="ifra"></iframe>
    </div>
    <div class="message">
        <form action="./Talking.jsp"  method="post">
<%--            这里没有用target属性,因为这是指定显示在哪的一个属性,我们只需要从新跳转到此页面即可--%>
            <input type="text" name="content" class="contentbox">
            <input type="submit" value="发送" class="submitbuttom">
            <%
                request.setCharacterEncoding("UTF-8");
                List<Object> usermessages=(List<Object>) application.getAttribute("msgs");
                String messqge=request.getParameter("content");
                String name= (String) session.getAttribute("name");
                if (messqge!=null)
                {
                    usermessages.add(name+":"+messqge);
                }
//                session.invalidate();
            %>
<%--            <input type="button" name="Button" value="Button" onclick="document.frames('ifra').location.reload()">--%>
        </form>
    </div>
</div>
</body>
</html>

记录群聊记录的页面:

<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2020/5/4
  Time: 9:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        html
        {
            width: 700px;
            height: 560px;
            font-size: 5px;
        }
    </style>
</head>
<body>
<%
    response.setCharacterEncoding("UTF-8");
    response.setHeader("refresh","1");
    List<Object> msgs=(List<Object>)application.getAttribute("msgs");
    for (int i=0;i<msgs.size();i++)
    {
        String string=(String)msgs.get(i);
        out.println(string+"<br>");
    }
%>
</body>
</html>

Listener代码:

public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        try {
            sce.getServletContext().setAttribute("counter",new CounterFile().ReadFile());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<Object> list=new ArrayList<Object>();
        int counter=0;
        sce.getServletContext().setAttribute("msgs",list);
        sce.getServletContext().setAttribute("Online",counter);
        System.out.println("Listener running");

    }

 

 

在线人数的显示也是用Listener来实现的,不过,写上去没有反应,监听不到,我也不知怎么回事。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值