Web小应用---网页聊天室
生活随笔
收集整理的這篇文章主要介紹了
Web小应用---网页聊天室
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ?應用解析:當不同IP的用戶登錄掛載在服務器上的網頁聊天室時,就可以即時得進行像QQ一樣的文字聊天,進行即時通信。
? ? ?
? ? ? 代碼:
login.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" %> <html> <body style=background-color:green > <marquee behavior="scroll" direction="right"><font color="black">歡迎光臨!</font></marquee> <h1 align="center">歡迎進入登錄室</h1> <hr color="red" /> <script type="text/javascript"> function denglu(){ if(document.getElementsByName("username")[0].value==""){ alert("賬號不能為空!"); document.getElementsByName("username")[0].focus();return false;} if(document.getElementsByName("password")[0].value==""){ alert("密碼不能為空!"); document.getElementsByName("password")[0].focus();return false;} } </script> <form name="form1" method="post" action="main.jsp"> <p align="center"> 用戶名:<input type="text" name="username" > <br/> 密 碼: <input type="password" name="password"> <br/> <input type="submit" value="登錄" οnclick="return denglu();" /> <input type="reset" value="重置"> </p> </form></body> </html>
main.jsp
<%@ page language="java" contentType="text/html;charset=utf-8" %> <html> <head></head><frameset rows="90%,10%"><frame src="display.jsp" name="top"></frame><frame src="sponse.jsp" name="bottom"></frame></frameset> <%request.setCharacterEncoding("gbk");String username=(String)request.getParameter("username");session.setAttribute("username", username);%> </html>
sponse.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" %> <html> <head> <script type="text/javascript">function sendMsg(){if(form3.message.value==""){alert("不能發送空消息!");return false;}}</script> </head> <body style=background-color:green ><form name="form3" method="post" action="display.jsp" target="top" οnsubmit="return sendMsg()" > <p align="center"> <input type="text" name="message" style="width:600px"> <input type="submit" value="我要留言" > <input type="reset" value="清除"> </p> </form></body> </html>
display.jsp
<%@ page language="java" contentType="text/html;charset=utf-8" import="java.util.*" import="java.text.SimpleDateFormat" %> <html> <meta http-equiv="refresh" content="1"> <body style=background-color:green > <h1 align="center">歡迎進入聊天室</h1> <hr color="red" /> <form name="form2" method="post" > <%request.setCharacterEncoding("gbk");//獲取用戶名String username =(String) session.getAttribute("username"); //獲取發言內容String message = request.getParameter("message");String show = (String) application.getAttribute("show");//保存歷史記錄SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();String time = format.format(date);String Message = username + "在" + time + "說:" + message + "<br>";//application.setAttribute("message", message);if (message!=null) {//message = username + "在" + time + "說:" + message + "<br>";if (application.getAttribute("show") != null){application.setAttribute("show", Message+ "<br>" +application.getAttribute("show").toString());}else{application.setAttribute("show",Message);}} out.println(application.getAttribute("show")); %> </form> </body> </html>
登陸窗口:
?梁主任:
海哥:
? 總結:該種功能也可以用在網頁留言上,用session會話保存數據,用戶已登錄就可以查看歷史留言。
總結
以上是生活随笔為你收集整理的Web小应用---网页聊天室的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转换输出流小练习 】现有一字符串:”我
- 下一篇: WIN10下CSF布料模拟算法