實驗4 內建物件使用
阿新 • • 發佈:2019-02-15
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <!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>網頁聊天室</title> <style type="text/css"> #header { background-color:black; color:white; text-align:center; padding:5px; } #left { line-height:30px; background-color:#eeeeee; height:400px; width:300px; float:left; padding:5px; } #middle { width:350px; float:left; padding:10px; } #footer { background-color:#FFD700; color:white; clear:both; text-align:center; padding:50px; } </style> </head> <script type="text/javascript"> function reset() { window.location.reload(); } </script> <body> <%request.setCharacterEncoding("utf-8");%> <% /*獲取聊天時間*/ Date data=new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dd=format.format(data); String name=(String) session.getAttribute("name"); String loginDate = session.getAttribute("loginDate").toString(); Date logintime = format.parse(loginDate); response.setHeader("refresh", "20"); %> <%! List<String> sllist = new ArrayList<String>(); List<String> qllist = new ArrayList<String>(); %> <% try { String liaotian = request.getParameter("lt"); if (liaotian.equals("siliao")) { //私聊 String sln = request.getParameter("slname"); sllist.add(name + "#對#" + sln + "#說:"+ request.getParameter("chatcontent") + " "+ dd); } else { //群聊 qllist.add(name + ":#" + request.getParameter("chatcontent") + "#"+ dd); } } catch (Exception e) { } session.setAttribute("siliao", sllist); application.setAttribute("qunliao", qllist); %> <!--某某上線--> <div id="header"> <%=name+"上線" %> </div> <!--線上人數--> <div id="left"> <% List<String> Oluser = (List) application.getAttribute("username");%> 線上人數:<%=Oluser.size() %><br> 線上使用者:<br> <% for (int i = 0; i < Oluser.size(); i++) { %> <%=Oluser.get(i)%> <br> <% } %> </div> <!--聊天資訊介面--> <div id="middle"> <%try{ List<String> list1 = (List<String>) session.getAttribute("siliao"); if (list1 == null || list1.isEmpty()) { } else { for (int i = 0; i < list1.size(); i++) { String[] sp = list1.get(i).split("#"); if (sp[0].equals(name)||sp[2].equals(name)) { out.print(sp[0] + sp[1] + sp[2]+sp[3]); %> <br> <% } } } } catch(Exception e){ } %> <% List<String> list = (List) application.getAttribute("qunliao"); if (list == null) { out.print("歡迎來到聊天室!"); } else { for (int i = 0; i < list.size(); i++) { String[] sp = list.get(i).split("#"); Date time1 = format.parse(sp[2]); if (logintime.getTime() <= time1.getTime()) {%> <%= sp[0]+ " " %> <input type="text" value="<%= sp[1]%>" style="background-color: blue;"> <%= " "+ format.format(time1) %><br> <% } } } %> </div> <!--選擇聊天方式--> <div id="footer"> <table align="center"> <form action="chatroom.jsp" method="post"> <td> <textarea name=chatcontent rows="1" cols="15" > </textarea> </td> <td> <input type="radio" name="lt" value="qunliao">群聊 <input type="radio" name="lt" value="siliao">私聊 <select name="slname" > <option selected="selected" ></option> <% for (int i = 0; i < Oluser.size(); i++) { %> <option><%=Oluser.get(i)%></option> <% } %> </select> </td> <td> <input TYPE="submit" name="submit" VALUE="傳送"> </td> <td> <input type="reset" name="sx" value="重新整理/重置" onclick="reset();"> </td> <td> <a href="leave.jsp?name1=<%=session.getAttribute("name").toString()%>" target="_parent" ">退出</a> </td> </form> </table> </div> </body> </html>