⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 11-6.txt

📁 jsp常用实例讲解
💻 TXT
字号:
<html>
<head>
<title>聊天室</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<meta http-equiv="Refresh" content";URL=chat.jsp?isret=yes">
</head>

<%-- 用变量chatroom、chatid、chatpoint保存聊天室名称、用户id和当前聊天信息的指针--%>
<%
String chatroom=new String();
chatroom=(String)session.getValue("chatroom");
//out.println("<BR>chatroom="+chatroom);
String chatid=new String();
chatid=(String)session.getValue("chatid");
String chatpoint=new String();
String temp=new String();
temp=chatroom+"chatpoint";
chatpoint=(String)application.getAttribute(temp);
if(chatpoint==null){
	chatpoint="0";
}
int chatpointn;
chatpointn=Integer.parseInt(chatpoint);
String retime1=new String();
String retime2=new String();
%>
<%-- 使用变量retime1保存聊天信息刷新时间间隔 --%>
<%
retime1=(String)session.getValue("retime1");
String chatword=new String();
String chatget=new String();
String chatgive=new String();
String chatcolor=new String();
String chatface=new String();
String chatprivate=new String();
%>
<%-- 使用isret变量获取提交时isret字段的内容,如果为no,则表示使用户提交,要保存用户最新的聊天信息;如果为yes,则表示是页面的自动刷新,不需要保存处理--%>
<%
String isret=new String();
isret=request.getParameter("isret");
//out.println("<BR>isret="+isret);
if(isret.equals("no")){
	chatword=request.getParameter("inputword");
	//out.println("<BR>SAID="+chatword);
	if(chatword==null||chatword.length()==0){
		chatword="Hello,everyone!";
	}
	chatcolor=request.getParameter("selectcolor");
	chatface=request.getParameter("selectface");
	chatgive=chatid;
	chatget=request.getParameter("selectuser");
	chatprivate=request.getParameter("radprivate");
	//out.println("<BR>chatget="+chatget);
	//out.println("<BR>chatprivate="+chatprivate);
	//out.println("<BR>color="+chatcolor);
	if (chatprivate==null){  
		chatprivate="no";
	}
%>
<%-- 设定当前聊天指针加1,如果超过30则返回到1,覆盖前面就的聊天信息 --%>
<%
	chatpointn=chatpointn+1;
	if(chatpointn==31){
		chatpointn=1;
	}
	chatpoint=chatpoint.valueOf(chatpointn);
	temp=chatroom+"chatpoint";
	application.setAttribute(temp,chatpoint);
%>
<%-- 保存用户聊天内容于application对象中 --%> 
<%
	temp=chatroom+"chatword"+chatpoint;
	application.setAttribute(temp,chatword);
%>
<%-- 保存用户聊天内容显示颜色在application对象中 --%>
<%
	temp=chatroom+"chatcolor"+chatpoint;
	application.setAttribute(temp,chatcolor);
%>
<%-- 保存聊天内容接收者id在application 对象中 ---%>
<%
	temp=chatroom+"chatget"+chatpoint;
	application.setAttribute(temp,chatget);
%>
<%-- 保存聊天内容发送者id在application 对象中 ---%>
<%
	temp=chatroom+"chatgive"+chatpoint;
	application.setAttribute(temp,chatgive);
%>
<%-- 设定用户聊天内容是否为私聊的标志,保存在application对象中 --%>
<%
	temp=chatroom+"chatprivate"+chatpoint;
	application.setAttribute(temp,chatprivate);
%>
<%-- 保存用户聊天表情在application 对象中 --%>
<%
	temp=chatroom+"chatface"+chatpoint;
	application.setAttribute(temp,chatface);
%>
<%-- 保存用户设定的聊天内容刷新速度和在线用户列表刷新速度在session对象中 --%>
<%
	retime1=request.getParameter("selectre1");
	retime2=request.getParameter("selectre2");
	session.putValue("retime1",retime1);
	session.putValue("retime2",retime2);
}

%>


<body bgcolope="#FFFFFF" leftmargin="20" topmargin="20">
<table width="550" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>
<%--显示保存过的所有聊天内容 --%>
<%
if(chatpointn!=0){
%>
<%-- 变量j表示当前要显示的聊天信息的指针,首先从最新的信息开始显示,所以j等于chatpint --%>
<%
int j;
j=chatpointn;
for(int i=1;i<=30;i++){
%>
<%-- 得到以j为当前指针的聊天信息的各部分内容 --%>
<%
String jtemp=new String();
jtemp=jtemp.valueOf(j);
temp=chatroom+"chatword"+jtemp;
chatword=(String)application.getAttribute(temp);
temp=chatroom+"chatcolor"+jtemp;
chatcolor=(String)application.getAttribute(temp);
temp=chatroom+"chatface"+jtemp;
chatface=(String)application.getAttribute(temp);
temp=chatroom+"chatprivate"+jtemp;
chatprivate=(String)application.getAttribute(temp);
temp=chatroom+"chatget"+jtemp;
chatget=(String)application.getAttribute(temp);
temp=chatroom+"chatgive"+jtemp;
chatgive=(String)application.getAttribute(temp);
//out.println("<BR>color="+chatcolor);
//out.println("<BR>face="+chatface);
//out.println("<BR>chatprivate="+chatprivate);
//out.println("<BR>chatget="+chatget);
//out.println("<BR>chatgive="+chatgive);
%>
<%--指针减1,在下一次循环中将显示下一组聊天信息。当指针为0时,返回显示末尾纪录--%>
<%
j--;
if(j==0){
	j=30;
}
%>
<%-- chatprivate=null表明当前指针没有内容,这时会出现在聊天室刚刚启动application对象的变量中还没有保存过聊天内容的时候 --%>
<%
if(!(chatprivate==null)){
%>
<%-- 下面的判断如果为真,则表示当前指针所指向的聊天信息是可以显示的,其中一种为公共信息,另一种是接收者和发送者当前执行程序的用户的私聊信息--%>
<%

	if(chatprivate.equals("no")||(chatprivate.equals("yes") && (chatgive.equals(chatid)||chatget.equals(chatid)))){
%>
<%-- 设定显示的颜色和字体大小 --%>
<font size="2", colors="<%=chatcolor%>">
<%--
判断是否为系统公告信息,如果是则根据颜色显示不同的公告信息:红色表示是用户进入到聊天室的信息;绿色表示是用户离开时的系统公告信息--%>
<%
	if(chatprivate.equals("no")&&chatget==null){
		if(chatcolor.equals("red")){
%>
			(系统公告)<%=chatword%>进入聊天室。
<%
		}else{
%>
			(系统公告)<%=chatword%>离开聊天室。
<%
		}
	}else{
%>
<%-- 如果是私聊信息,要加上“私聊”字样 --%>
<%
		if(chatprivate.equals("yes")){
%>
			(私聊)
<%
		}
%>
<%-- 下面分别显示聊天的发送者、接收者、表情和聊天内容 --%>
<%
		//out.println(chatgive);
%>
<%
		if(chatget.equals("all")) {
%>
			大家
<%
		}else{
			out.println(chatget);
		}
%>
<%
int nn;
nn=Integer.parseInt(chatface);
switch(nn){
case 1:
%>
微笑着
<%
break;
case 2:
%>
大笑着
<%
break;
case 3:
%>
愤怒的
<%
break;
case 4:
%>
生气的
<%
break;
case 5:
%>
撒娇的
<%
break;
case 6:
%>
严肃的
<%
}
%>
说:
<%
out.println(chatword);
}
%>
</font>
<br><br>
<%
}
}
}
}
%>
</td>
</tr>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -