用JSP实现一个简易的网页版聊天室。 30
2个回答
展开全部
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>
<%
List<String> chartInfo = new ArrayList<String>();//保存聊天信息的集合
List<String> usrs1 =new ArrayList<String>();//保存登录用户的集合
if(null!=application.getAttribute("chartinfo")){
chartInfo = (List<String>)application.getAttribute("chartinfo");
}
if(null!=application.getAttribute("users")){
usrs1 = (List<String>) 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_usr&&null!=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">
<%
List<String> usrs = (List<String>) 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。你说的功能基本上都达到。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询