在线交流系统的实现

一。系统分析 

1. 页面结构 

      要用到两个界面 ,登录界面和聊天界面。应该写几个JSP 代码 呢?为了利于分工,我们将

界面显示和动作分开。

三个动作

(1)登录,为这个动作设计一个输入页面 loginForm.jsp ,显示登录表单;提交给 loginAction.jsp, 负责接收参数,处理请求

(2)聊天,为这个动作设计 一个输入页面chatForm.jsp. 显示聊天 界面表单;该表单提交 给 chatAction.jsp ,负责接受

聊天 信息,处理聊天请求。

    请求完毕 ,跳转到 chatForm.jsp,在chatForm.jsp中,信息内容的显示和在线名单可以另外写msgs.jsp ,通过 iframe 嵌入。

作用是显示用户的聊天信息,在线名单 ,这个页面每隔一会刷新一下。

(3)退出登录,为这个动作写一个 logoutAction.jsp ,负责 清空用户的状态,跳转到 loginForm.jsp。

2. 状态保存

消息内容和在线名单都是保存在  application 中。

用户的信息保存在session 中。


二、开发过程 

1. 准备数据 

创建 数据 库脚本为

create table t_customer(
 account varchar(40),
 password varchar(40),
 name varchar(40));

2. 编写 DAO 和VO

在DAO中验证用户的身份,用户的信息用VO封装。这里只写下DAO中的关键代码 。

public void initConnection() throws Exception{
		Class.forName("com.mysql.jdbc.Driver");  //指明驱动
		String url = "jdbc:mysql://localhost:3306/test";  
		connection= DriverManager.getConnection(url, "root","root");
	}
	
	public Customer getCustomerByAccount(String account)throws Exception{
		Customer customer= null;
		initConnection();
		String sql= "select *  from t_customer where account =?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, account);
		ResultSet rs= ps.executeQuery();
		
		if (rs.next()){
			customer= new Customer();
			customer.setAccount(rs.getString("account"));
			customer.setName(rs.getString("name"));
			customer.setPassword(rs.getString("password"));
		}
		closeConnection();
		return customer;
	}

3. 编写 loginForm.jsp 和loginAction.jsp 

对于登录界面来说 ,有两个页面。

loginForm是:

<%
    	//初始化application 
      	ArrayList customers = (ArrayList) application.getAttribute("customers");
    	if (customers == null) {
    		customers = new ArrayList();
    		application.setAttribute("customers", customers);
    	}

    	ArrayList msgs = (ArrayList) application.getAttribute("msgs");
    	if (msgs == null) {
    		msgs = new ArrayList();
		    application.setAttribute("msgs", msgs);
    	}
    %>
    
    欢迎登录在线交流系统 
    <form action="loginAction.jsp"  name ="form1" method ="post">
    输入帐号:<input name ="account"  type="text"><br>
    输入密码:<input name ="password"  type= "text"> <br>
    <input type ="submit"  value="登录">
    </form>

loginAction是

  <%
    request.setCharacterEncoding("gb2312");
    String account = request.getParameter("account");
    String password = request.getParameter("password");
    
    CustomerDAO cdao = new CustomerDAO();
    Customer customer= cdao.getCustomerByAccount(account);
    if (customer==null|| !customer.getPassword().equals(password)){
    %> 
    登录失败,<a href ="loginForm.jsp">返回页面</a>
    <%
    }
    else{
    session.setAttribute("customer", customer);
    ArrayList customers =(ArrayList) application.getAttribute("customers");
    ArrayList msgs = (ArrayList)application.getAttribute("msgs");
    customers.add(customer);
    msgs.add(customer.getName()+"上线啦");
    response.sendRedirect("chatForm.jsp");
    } 
     %>

4. 编写 chatForm.jsp 和chatAction.jsp 

chatForm.jsp .

 <%
  Customer customer=(Customer)session.getAttribute("customer");
   %>
   欢迎 <%=customer.getName() %>聊天<br>
   <form action="chatAction.jsp"  method ="post">
   输入聊天信息:<input name ="msg" type="text"  size="40">
   <input type ="submit" value ="发送">
   </form>
   <a href ="logoutAction.jsp">退出登录</a>
   <hr>
   <iframe src ="msgs.jsp" witdth ="100%"  height ="80%"  frameborder ="0"></iframe>

chatAction.jsp 

<%
		Customer customer = (Customer) session.getAttribute("customer");
	%>
	<%
		request.setCharacterEncoding("gb2312");
		String msg = request.getParameter("msg");
		ArrayList msgs = (ArrayList) application.getAttribute("msgs");
		if (msg != null && !msg.equals("")) {
			msgs.add(customer.getName() + "说:" + msg);
			response.sendRedirect("chatForm.jsp");
		}
	%>

msgs.jsp

<%response.setHeader("Refresh", "10");
     %>
    
     <table width ="80%"  border="0" align ="center">
     <tr bgcolor = "yellow"  align ="center">
     <td width ="75%" >消息</td>
     <td width ="25%">当前在线</td>
     </tr>
     
     <tr bgcolor= "pink">
     <td >
     <%
     	ArrayList msgs = (ArrayList) application.getAttribute("msgs");
       	if (msgs != null) {
     		for (int i = msgs.size() - 1; i >= 0; i--) {
     			out.println(msgs.get(i) + "<br>");
     		}
     	}
     %></td>
     <td valign="top">
     <%
     ArrayList customers = (ArrayList)application.getAttribute("customers");
     if (customers!=null){
     	for (int i=customers.size()-1; i>=0;i--){
    		 Customer customer= (Customer)customers.get(i);
     		out.println(customer.getAccount()+"("+customer.getName()+")"+"<br>");
     	}
     }
      %></td>
     </tr>
     </table>


转载于:https://my.oschina.net/chuiyuan/blog/281160

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值