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

📄 chat.jsp

📁 是一个基于ajax的聊天室!实现了在线聊天
💻 JSP
字号:

<%@ page language="java" contentType="application/xml; charset=UTF-8" pageEncoding="GBK" %>

<%
response.setHeader("Pragma","No-cache");//HTTP 1.1
response.setHeader("Cache-Control","no-cache");//HTTP 1.0
response.setHeader("Expires","0");//防止被proxy

request.setCharacterEncoding("UTF-8");
%>

<%!

	/**
	 * 转换字符串的内码.
	 *
	 * @param input
	 *            输入的字符串
	 * @param sourceEncoding
	 *            源字符集名称
	 * @param targetEncoding
	 *            目标字符集名称
	 * @return 转换结果, 如果有错误发生, 则返回原来的值
	 */
	public static String changeEncoding(String input, String sourceEncoding,
			String targetEncoding) {
		if (input == null || input.equals("")) {
			return input;
		}

		try {
			byte[] bytes = input.getBytes(sourceEncoding);
			return new String(bytes, targetEncoding);
		} catch (Exception ex) {
		}
		return input;
	}
%>
<%--
String repla(String str){
	if(str == null) {
		str = "";
	}

	str = str.replaceAll("/\&/g", "&amp;");
	str = str.replaceAll("/\>/g", "&gt;");
	str = str.replaceAll("/\</g", "&lt;");
	str = str.replaceAll("/\"/g", "&quot;");
	str = str.replaceAll("/\'/g", "&#39;");
	return str;
}
--%>

<%
String useIp = request.getRemoteAddr();

int chatcounts = 0;

try {
  chatcounts = Integer.parseInt((String)application.getAttribute("chatcounts"));
} catch (Exception ex) {
  //ex.printStackTrace();
}

if( application.getAttribute("chatcounts") == null){
	application.setAttribute("chatcounts", "0");
}

if( request.getParameter("cont") != null && (! "0".equals(request.getParameter("cont")) ) ) {

		if(chatcounts < 21){
			chatcounts += 1;
		}else if(chatcounts >= 21){
			chatcounts = 1;
		}

		application.setAttribute("chatcounts", chatcounts + "");

		String msgs = request.getParameter("cont");

		System.out.println(changeEncoding(msgs, "ISO8859-1", "UTF-8"));
		// 下面这句只是为了在 Tomcat 下面运行的字符集问题, 其它 Server 请酌情处理
		msgs = changeEncoding(msgs, "ISO8859-1", "UTF-8");

		application.setAttribute("ips" + chatcounts, useIp +"");

		application.setAttribute("msgs" + chatcounts, msgs +"");

        String names = request.getParameter("named");

		if(names == null){
            names = "";
		}

		// 下面这句只是为了在 Tomcat 下面运行的字符集问题, 其它 Server 请酌情处理
		names = changeEncoding(names, "ISO8859-1", "UTF-8");

		application.setAttribute("names" + chatcounts, names + "");

}

if( "cls".equals(request.getParameter("cont") ) ) {
	application.removeAttribute("names");
	application.removeAttribute("msgs");
	application.removeAttribute("chatcounts");
	application.removeAttribute("ips");
}

out.clear();
%><?xml version="1.0" encoding="utf-8" ?>
<ppl>
	<num><%=chatcounts%></num>
	<%
	int clien = 0;

	try {
	    clien = Integer.parseInt(request.getParameter("clien"));
	} catch (Exception ex) {
	  //ex.printStackTrace();
	}

	if("m".equals(request.getParameter("clien"))) {
	%>
	<items>
		<ip><%=useIp%></ip>
	</items>
	<%
	}else if(clien < chatcounts) {
		for(int i=clien; i<chatcounts; i++){
			int s = i+1;
			String useName = (String)application.getAttribute("names" + s);
			String useMsg = (String)application.getAttribute("msgs" + s);
			useIp = (String)application.getAttribute("ips" + s);
	%>
	<items>
		<name><%=useName%></name>
		<conts><%=useMsg%></conts>
		<ip><%=useIp%></ip>
	</items>
	<%
		}
	} else if(clien > chatcounts) {
		for(int i=clien; i<21; i++){
			String useName = (String)application.getAttribute("names" + i);
			String useMsg = (String)application.getAttribute("msgs" + i);
			useIp = (String)application.getAttribute("ips" + i);
	%>
	<items>
		<name><%=useName%></name>
		<conts><%=useMsg%></conts>
		<ip><%=useIp%></ip>
	</items>
	<%
		}

		for(int i=1; i<=chatcounts; i++){
			String useNameM = (String)application.getAttribute("names" + i);
			String useMsgM = (String)application.getAttribute("msgs" + i);
			String useIpM = (String)application.getAttribute("ips" + i);
	%>
	<items>
		<name><%=useNameM%></name>
		<conts><%=useMsgM%></conts>
		<ip><%=useIpM%></ip>
	</items>
	<%
		}
	}

//	else if(Request("clien") == Application("chatcounts")){
//	}
	%>
</ppl>

⌨️ 快捷键说明

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