新学web编程,老师让写一个聊天室,我使用了application存储聊天记录,结果同学提出疑问application存储聊天记录会不会只有自己能够看到而别人不能访问。如果是的话我就白写了......所以问一下各位大神,application能像我这样用吗?如果不行,那要怎样才能看到使得每个人的聊天记录呢?聊天室的一部分代码如下:
另外,聊天内容如果是中文会显示乱码,但我用chat=new String(chat.getBytes("ISO-8859-1"),"UTF-8");又会报错,老师只说过这一种转码方式啊。恳请各位大神帮忙!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import=" javax.servlet.http.HttpServletRequest"%>
<%@ page import=" javax.servlet.http.HttpServletResponse"%>
<%@ page import=" javax.servlet.http.HttpSession"%>
<%@ page import=" java.util.Date" %>
<!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>Insert title here</title>
<style>
#div2{
position:absolute;
top:10px;
left:10px;
bottom:130px;
reght:10px;
width:100%;
background:#F0F0F0;
}
#div1{
position:absolute;
bottom:10px;
width:100%;
height:100px;
margin:0;
}
#text{
margin:0;
width:84%;
padding:0;
height:100px;
float:left;
}
#text2{
margin:0;
width:100%;
height:100%;
}
#button{
margin:0;
width:15%;
padding:0;
height:100px;
float:right;
}
</style>
<script type="text/javascript">
function check(){
if(document.getElementById("text").value.length==0){
document.getElementById("text").value="聊天记录不能为空";
return false;
}
else{
localtion.reload();
return true;
}
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String chat=request.getParameter("text");
String chats=(String)application.getAttribute("chat");
if(chat!=null){
String username;
HttpSession session1=request.getSession();
if(session1.getAttribute("username")!=null){
username=(String)session1.getAttribute("username");
}
else username="游客";
Date d=new Date();
chat=username+" "+d.toLocaleString()+"\r\t"+chat;
if(chats==null)
chats=chat;
else chats=chats+"\r\n"+"\r\n"+chat;
}
if(chats!=null)
application.setAttribute("chat", chats);%>
<div id="div2"><textarea id="text2"><%out.println(application.getAttribute("chat"));%></textarea></div>
<form name="form1" id="form1" method="get" action="chatting.jsp" onsubmit="return check()">
<div id="div1">
<div><textarea rows="6" cols="10" id="text" name="text"></textarea></div>
<div><input type="submit" id="button" name="button" value="发言"></div>
</div>
</form>
</body>
</html>