在我的jsp页面中,只有当我得到sessionId!=“null”时,我才显示包含文本区域、文本字段和发送按钮的div。
此外,我还有一个确认警报框,用于关闭窗口以注销会话...
但是,如果我尝试检索 sessionId、userId 等的值,或者在按下 startChat 或发送按钮时将消息发送到服务器,我每次都会收到相同的警报……在我的 Jsp 页面中,
<div id="login">
<form id="indexForm" name="indexForm" action="LoginAction" method="post">
<table>
<tr>
<td id="fname" class="fontStyle">First Name :</td>
<td id="fvalue"><input type="text" id="firstname"
name="firstname" /></td>
<td id="lname" class="fontStyle">Last Name :</td>
<td id="lvalue"><input type="text" id="lastname" name="lastname" /></td>
</tr>
<tr>
<td colspan="4" id="err1" style="color: red"> </td>
</tr>
<tr>
<td id="email_id" class="fontStyle">Email Id :</td>
<td><input type="text" id="email" name="email" /></td>
<td id="sub" class="fontStyle">Subject
:</td>
<td><input type="text" id="subject" name="subject" /></td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td colspan="4" id="err2" style="color: red"> </td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td></td>
<td>
<center><a id="button" href="javascript:getValues();">Start
chat</a></center>
</td>
<td>
<center><a id="button" href="javascript:logout();">Stop
chat</a></center>
</td>
<td></td>
</tr>
</table>
</form>
</div>
<div style="display: none;" id="div">
<div><textarea id="textarea" rows="10"></textarea></div>
<div>
<table>
<tr>
<td id="msg" class="fontStyle">message :</td>
<td id="msgvalue"><input size="40" type="text" id="message"
onkeydown="enterKey()" /></td>
<td style="width: 5%;"></td>
<td><!-- <input type="button" value="send" onclick="sendMessage()" /> -->
<a id="button3" href="javascript:sendMessage();">Send</a></td>
</tr>
</table>
</div>
</div>
在我的 JavaScript 中,
window.onbeforeunload = function () {
return "Are you Sure want to LOGOUT the session ?";
};
window.onunload = function () {
if((sessionId != null)&&(sessionId!="null")&& (sessionId != ""))
logout();
};
function sendMessage(){
message = document.getElementById("message").value;
document.getElementById("message").innerText = "";
try
{
xmlhttp.onreadystatechange=function()
{
//alert("Status : "+xmlhttp.status+"\nreadyState : "+xmlhttp.readyState);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var checkMsg = decodeURIComponent(xmlhttp.responseText.toString());
if(checkMsg != "null" && checkMsg != null){
//document.getElementById("textarea").innerHTML += checkMsg;
if(textarea.value == "")
textarea.value = checkMsg;
else
textarea.value += "\n"+ checkMsg ;
}
}
};
xmlhttp.open("POST","SendMessageAction?sessionId="+sessionId+"&userId="+userId+"&securekey="+secureKey+"&message="+encodeURIComponent(message)+"&sid"+Math.random(),true);
xmlhttp.send();
}
catch(err)
{
alert(err.description);
}
}
在我的 Servlet 中,
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
msg = request.getParameter("message");
//System.out.println("msg -----> : "+msg);
seckey = request.getParameter("securekey");
uid = request.getParameter("userId");
sessionId = request.getParameter("sessionId");
//counter =Integer.parseInt(request.getParameter("counter"));
counter = 1;
protocol = ApplicationInfo.flexProtocol;
message = new SendMessage();
message.send(msg, seckey, uid, sessionId, counter, protocol);
CustomerInfo customer = ApplicationInfo.customerDetails.get(uid);
out.print(customer.getMessage());
//System.out.println("msgDecoded -----> : "+msg);
}
帮助我...我不需要每次都收到警报,除非我正在关闭浏览窗口...