JSP简易聊天室

时间:2022-05-30 10:08:03

这是一个网页版的简易聊天室,做的界面是不是很好 请见谅^_^

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>

<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<form method = "post" action = "chuli.jsp">
用户名:<input type = "text" name = "user"></input><br>
密码:<input type = "password" name = "pass"></input><br>
<input type="submit" value ="登录"></input>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>

<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<%
String us="", ps="";
us = request.getParameter("user");
ps = request.getParameter("pass");
if(ps.equals("123456")){
session.setAttribute("sess_us", us);
response.sendRedirect("chat.jsp");
}
else{
out.print("对不起 您的密码不正确");
}
%>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk" import="java.util.*"%>

<!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=gbk">
<title>Insert title here</title>
<script type="text/javascript">
function Judge(){
var s;
s = chats.say.value;
if(s==""){
alert("发言不能为空");
return false;
}
return true;
}
</script>
</head>
<body>
<%
response.setHeader("refresh","5");
String sess_us, fy, tmp;
request.setCharacterEncoding("gbk");
sess_us = (String)session.getAttribute("sess_us");
Date now = new Date();
fy = request.getParameter("say");
if(fy != ""){
String ans = now.toString()+" "+sess_us+"说: "+fy+"<br>";
if(application.getAttribute("app_fy")== ""){
application.setAttribute("app_fy", ans);
}
else{
tmp = (String)application.getAttribute("app_fy");
ans = tmp + ans;
application.setAttribute("app_fy", ans);
}
}
%>

<table border="1" width="800">
<tr><td><%=application.getAttribute("app_fy") %>
</table>

<form action="chat.jsp" method = "post" name = "chats">
<textarea rows="3" cols="80" name = "say"></textarea><br>
<input type = "submit" value = "发言" onclick = "return Judge()"></input>

</form>
</body>
</html>