当前位置:首页 > 代码 > 正文

jsp聊天室代码(jsp开发聊天室)

admin 发布:2022-12-19 23:02 150


今天给各位分享jsp聊天室代码的知识,其中也会对jsp开发聊天室进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

我是用JSP实现简单的聊天室,实现聊天的内容在文本域中显示!我代码如下:

把你要显示的东西放在textarea/textarea之间啊比如想把从服务端得到的一个字符串在文本域中显示 textarea你要显示的内容/textarea

一个小小的JSP聊天室代码问题

1.登陆界面

%@ page language="java" pageEncoding="gb2312"%

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

meta http-equiv="Content-Type" content="text/html; charset=gb2312" /

title用户登陆/title

/head

body

form action="chart.jsp" method="post"

帐号input type="text" name="userName"/

口令input type="password" name="passwd"/

input type="submit" value="确认"/

/form

/body

/html

2.

%@ page contentType="text/html; charset=gb2312" import="

com.san.db.UserId,

java.util.*"

%

%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

base href="%=basePath%"

meta http-equiv="Content-Type" content="text/html; charset=gb2312" /

titleMy JSP 'chart.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"

--

%response.setIntHeader("Refresh",10); %

/head

body

%

/**

程序目的:模拟一个聊天室程序

测试方法,请在本机上打开3个IE窗口,从登陆界面进去。再发言.

反应可能不是很快,你多等一会。我已经测试过了.

**/

try{

//获取从用户登陆界面传递过来的用户

String loginname = request.getParameter("userName");

//获取用户的sessionId

String userSessionId=(String)session.getAttribute("userSessionId");

//根据sessionId取出用户帐号

String name = (String)session.getAttribute(userSessionId+"user");

//如果loginnmae!=null,表明用户初次登陆

if(loginname!=null)

{ //初次登陆,创造一个sessionid,把系统当前时间作为sessionId

userSessionId=System.currentTimeMillis()+"";

session.setAttribute("userSessionId",userSessionId);

//存储帐号在session中

session.setAttribute(userSessionId+"user",loginname);

}

//获取用户发言

String word = request.getParameter("yourWords");

//从application中所有用户历史发言

String chart = (String)application.getAttribute("chart");

//当发言到一定长度就清空,以免耗尽内存

if(chart!=nullchart.length()10000) application.setAttribute("chart","");

if(name!=nullword!=null){

//组装用户在屏幕上发言语句:

String userword=new Date()+" ["+name+"]说:"+word;

//将用户发言存入历史记录中

chart+=userword+"\r\n";

application.setAttribute("chart",chart);

}

%

textarea rows="15" cols="40" name="output" style="width:100%"

% if(name!=null) out.print(chart);

%

/textarea

% if(name!=null) out.print(name+"说");%

form action="" method="post"

input type="text" size="45" name="yourWords"

input type="hidden" name="chart"br

input type="submit" value="发送"/

/form

/body

/html

%}catch(Exception e){

e.printStackTrace();

}%

如何用JSP实现网页聊天室

在这里,我们将提供一个聊天室的简单的JSP程序。我们的程序是按照JSP

Model

2的规范来实现的。简而言之,就是我们将绝大多数处理用户请求的任务交给Servlet来执行,而我们的JSP仅仅是用来完成显示。

在阅读我们的程序之前,你最好已经具备了Java和JSP的基础知识,但是如果你对Java和JSP不是太熟悉,你也可以直接阅读我们的程序,对于每一项涉及到Java和JSP的内容,在我们的程序中第一次出现时,我们会作简要的说明。

当然,你还是需要阅读其他的相关教程,因为我们的这篇文章毕竟不是一个教程。

此外,我们的聊天室是挂接在另一个应用系统上的,对于其中一些特殊的命令方法,你可以不必太介意。

用JSP实现一个简易的网页版聊天室。

1.登录页面

文件名:know.jsp

%@ page language="java" import="java.util.*" pageEncoding="utf-8"%

%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

  head

    base href="%=basePath%"

    

    title登录/title

    

    meta http-equiv="pragma" content="no-cache"

    meta http-equiv="cache-control" content="no-cache"

    meta http-equiv="expires" content="0"    

  /head

  body

      form action="know2.jsp" method="post"

         input type="text" name="username"br/

         input type="submit" value="提交"/

      /form

  /body

/html

2.聊天页面

文件名:know2.jsp

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%

%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%

!DOCTYPE HTML

html

  head

    base href="%=basePath%"

    

    title聊天/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="refresh"content="10;url=know2.jsp"

  /head

   style

          .container{

            position:relative;

            top:100px;

            margin:0 auto;

            width: 500px;

            height: 300px;

            border: 1px solid #aaa;

            overflow: hidden;

          }

          .usrlist{

            width: 100px;

            height: 250px;

            background-color: #bbb;

            display: block;

            float: left;

             overflow: scroll;

          }

          .chartinfo{

            width: 400px;

            height: 250px;

            background-color: #ccc;

            display: block;

            float: left;

            overflow: scroll;

          }

          .send{

            width: 500px;

            height: 50px;

            background-color: #ddd;

            display: block;

            float: left;

          }

          select{

            width: 50px;

           }

       /style

    %

        ListString chartInfo = new  ArrayListString();//保存聊天信息的集合

        ListString usrs1 =new  ArrayListString();//保存登录用户的集合

        if(null!=application.getAttribute("chartinfo")){

           chartInfo =  (ListString)application.getAttribute("chartinfo");

        }

        if(null!=application.getAttribute("users")){

           usrs1  =  (ListString) application.getAttribute("users");

        }

        //form 提交过来的数据

        String username = request.getParameter("username");

        String say = request.getParameter("gang");

        String to_usr = request.getParameter("tousr");

        

           if(null!=username!"".equals(username)){

            if(!usrs1.contains(username)){

             usrs1.add(username);

             session.setAttribute("users",username);

            }

           }

       application.setAttribute("users",usrs1);//将user1集合放入application

       

       String lgusr = "";

       if(null!=session.getAttribute("users")){

         lgusr = (String)session.getAttribute("users");

       }

       if(lgusr.equals("")||null==lgusr){

        response.setStatus(response.SC_MOVED_TEMPORARILY);

        response.setHeader("Location", "know.jsp"); 

       }

         String chart="";

       if(null!=to_usrnull!=say!"".equals(to_usr)!"".equals(say)){

           chart = lgusr+" 对   "+to_usr+"说: "+say;

        }

        

        if(!"".equals(chart)){

            chartInfo.add(chart);

        }

        application.setAttribute("chartinfo", chartInfo);//将聊天信息集合 放入 application

     %

      

  body

   div class="container"

     div class="usrlist"

        %

         ListString usrs  =  (ListString) application.getAttribute("users");

         if(usrs.size()0){

             for(String u : usrs){

                out.print(u);

                out.print("br/");

             }

         }

         %

     /div

     div class="chartinfo"

        %

     //   out.print(chartInfo.size());

        for(String ct : chartInfo){

          out.print(ct);

          out.print("br/");

        }

         %

     /div

     div class="send"

     form method="post" action="know2.jsp"

         span对  /span

          select name="tousr" 

          option/option

             %

             for(String u : usrs){

                if(!u.equals(lgusr)){

                  out.print("");

                  out.print("option value='"+u+"'"+u+"/option");

                }

                

             }

              %

          /select 

           span  说 :/span

           input type="text" name="gang"style="width: 300px;"

           input type="submit" value="发送"

           /form

     /div

   /div

  /body

/html

这是两个jsp文件,分别是know.jsp 和know2.jsp。你说的功能基本上都达到。

关于jsp聊天室代码和jsp开发聊天室的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/29065.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载