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

📄 11-2.txt

📁 jsp常用实例讲解
💻 TXT
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>欢乐聊天室</title>
</head>

<%-- 首先取出用户提交的代号名称,保存在变量chatid中 --%>
<%
String chatid=new String();
chatid=request.getParameter("inputid");
%>
<%-- 使用变量flag来标记用户输入是否合法,如果合法,则flag为true --%>
<%
boolean flag;
flag=true;
%>
<%-- 判断用户的输入是否为空 --%>
<%
if(chatid==null) {
	chatid="";
}
if(chatid.length()==0){
	flag=false;
}
%>
<%-- 比较用户所输入的id和目前聊天室中存在的所有id --%>
<%
for(int i=1;i<=6 && flag;i++){
	String itemp=new String();
	itemp=itemp.valueOf(i);
	int num;
	String numtemp=new String();
	String temp=new String();
	%>
	<%--从application 对象中取出第i个聊天室中的总人数,temp变量保存的是application 中用于保存第i个聊天室总人数的相应变量的名称--%>
	<%
	temp="room"+itemp+"usernum";
	numtemp=(String)application.getAttribute(temp);
	if(numtemp==null){
		numtemp="0";
		application.setAttribute(temp,numtemp);
	}
	num=Integer.parseInt(numtemp);
	%>
	<%-- 把用户输入的id与第i个聊天室中的每一个用户id比较 --%>
	<%
	for(int j=1;j<=num && flag;j++){
		String jtemp=new String();
		jtemp=jtemp.valueOf(j);
		%>
		<%--从application 对象中取出第i个聊天室中第j个用户的id,temp变量保存的是application 对象用于保存第i个聊天室第j个用户id相应的变量名--%>
		<%
		temp="room"+itemp+"user"+jtemp;
		String usertemp=new String();
		usertemp=(String)application.getAttribute(temp);
		%>
		<%-- 如果用户id已经存在,则设标志flag为false --%>
		<%	
		if(usertemp.equalsIgnoreCase(chatid)){
			flag=false;
		}
	}
}
%>
<%-- 如果flag为true,则用户的id合法,需要对其进行处理定义数组nnn,以用于保存每个聊天室中的人数--%>
<%
int nnn[]=new int[6];
if(flag){
	String temproom=new String();
	temproom=(String)session.getValue("chatroom");
	%>
	<%--
	判断用户的session对象chatroom是否为空,如果不为空则说明用户已经登录成功过,而且没有正常退出,所以不再为用户保存新的id,者也是为了放置用户重复登录一个以上的id --%>
	<%
	if(temproom==null){
		session.putValue("chatid",chatid);
	}
	%>
	<%-- 从application对象的相应变量中获取各聊天室的人数并保存在变量数组nnn中 --%>
	<%
	for(int i=1;i<=6;i++){
		String itemp=new String();
		itemp=itemp.valueOf(i);
		int num;
		String numtemp=new String();
		String temp=new String();
		temp="room"+itemp+"usernum";
		numtemp=(String)application.getAttribute(temp);
		if(numtemp==null){
			numtemp="0";
		}
		num=Integer.parseInt(numtemp);
		nnn[i-1]=num;
	}
}
%>

<body bgcolor="#C0C0C0">
<p> </p>
<p align="center"><b><font color="#ff00ff" face="隶书" size="6">欢 乐 聊 
天 室</font></b></p>

<div align="center">
  <center>
<%
if(flag){
%>

<p align="center"><font color="red"><%=chatid%></font> 您好,请选择要感兴趣的聊天室!</p>
  <table border="1" width="370">
    <tr>
      <td width="50%"><a href="JSPchat.jsp?chatroom=1">
	今天我们相识(<%=nnn[0]%>)</a></td>
      <td width="50%"><a href="JSPchat.jsp?chatroom=2">
	校园的那条小路(<%=nnn[1]%>)</a></td>
    </tr>
    <tr>
      <td width="50%"><a href="JSPchat.jsp?chatroom=3">
	职场淘金(<%=nnn[2]%>)</a></td>
      <td width="50%"><a href="JSPchat.jsp?chatroom=4">
	网络技术交流(<%=nnn[3]%>)</a></td>
    </tr>
    <tr>
      <td width="50%"><a href="JSPchat.jsp?chatroom=5">
	世界体育大看台(<%=nnn[4]%>)</a></td>
      <td width="50%"><a href="JSPchat.jsp?chatroom=6">
	新闻背后的故事(<%=nnn[5]%>)</a></td>
    </tr>
  </table>
<%
}else{
%>
<p>id不能为空,或者此id已经被使用,请重新选择!</p>
<p><a href="login.html">返  回</a></p>
<%
}
%>
</center>
</div>
</body>
</html>

⌨️ 快捷键说明

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