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="10url=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。你说的功能基本上都达到。
改成下面的样子:<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
<title>My JSP 'lab.jsp' starting page</title>
</head>
<body>
<%
if(application.getAttribute("chat")!=null)
{ if(request.getParameter("mywords")!=null)
{ String mywords=request.getParameter("mywords")
mywords=(String)application.getAttribute("chat")+"<br>"+mywords
application.setAttribute("chat",mywords)
out.print((String)application.getAttribute("chat"))
}
}else{
application.setAttribute("chat","")
}
%>
<form action="a1.jsf" method="post">
<input type="text" size="30" name="mywords" value="我喜欢聊天">
<input type="submit" name="sumbit" value="提交">
</form>
</body>
</html>
1.要用post提交,否则中文乱码
2.out.print((String)application.getAttribute("char"))这句你写错了,应该是chat不是char
3.你第一次运行的时候application.getAttribute("chat")肯定是空的,是空的话你又什么都不作,下次还是空的呀
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)