想要实现一个在线聊天室
使用application与session
每个用户聊天信息显示用户名并且application保存所有的聊天信息
但是一直用户名和输出的话都是null
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loopy聊天室</title>
</head>
<style>
body {background-color:white; }
h1 {color:#fbcccf;
text-align:center;
font-size:40px;
}
h2{text-align:center;}
</style>
<body>
<form action="homework6.jsp" method="post">
<h2>
<img src=" D:\photo\3.jpg" width="120" height="120"></img>
</h2>
<h1>
聊天室
<br>
用户名:<input id="usernameID" type="text" name="username"/>
<br>
密码:<input type="text" name="password"/>
<br>
<input type="submit" value="提交"/>
</h1>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String message="聊天区域:";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>聊天室</title>
<style>
.div3{
background-color:pink;
border:solid 1px black;
width:300px;
height:300px;
line-height:150%
}
img{
width:15px;
height:20px;
}
</style>
</head>
<body>
<div class="div3">
<form action="homework7.jsp" method="post">
<%
String cy=(String)application.getAttribute("cy");
if(cy==null){
out.print("聊天内容:");
}else{
out.print("聊天内容:"+cy);
}
%>
<%
String username=(String)application.getAttribute("username");
out.print("你的用户名是:"+username);
%>
<br>
聊天区域:<input type="text" name="cy"/>
<br>
<input type="submit"value="提交"/></input>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String message="聊天区域:";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>聊天室</title>
<style>
.div3{
background-color:pink;
border:solid 1px black;
width:300px;
height:300px;
line-height:150%
}
img{
width:15px;
height:20px;
}
</style>
</head>
<body>
<div class="div3">
<%
request.setCharacterEncoding("UTF-8");
String cy=(String)application.getAttribute("cy");
message=message+cy+".";
out.print("");
application.setAttribute("cy",cy);
out.print(cy);
out.println("聊天内容输入成功,20s后自动跳回聊天页面");
response.setHeader("refresh","20;url='homework6.jsp'");
%>
</div>
</body>
</html>