JAVA中DWR开发项目实战经验(转载)
来源:互联网 发布:java时间加减 分钟 编辑:程序博客网 时间:2024/05/17 13:11
自己在公司网站开发总用DWR开发的一个简单聊天
本人也是在网站开发地图时有到了这个技术,刚学习DWR半个月
就把随手写的一个简单聊天来给大家共享,切磋一下。
由于考虑到某些局部原因,只发表了一些重要技术方面的资料,没有把全部东西共享。请个人体谅。
由于DWR2.0有个Bug ,我用的DWR1.0,但是,它必须用JDK1.4
dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<init></init>
<allow>
<create creator="new" javascript="CityChatsend" scope="session">
<param name="class" value="com.aaa.CharChat.DWR.DWRCityChar_send"/>
<include method="Chat_send"/>
</create>
<create creator="new" javascript="CityChatRead" scope="session">
<param name="class" value="com.aaa.CharChat.DWR.DWRCityChar_Read"/>
<include method="ChatRead"/>
</create>
<create creator="new" javascript="CityChatInit" scope="application">
<param name="class" value="com.aaa.CharChat.DWR.DWRCityChar_Init"/>
<include method="getNew_Old"/>
<include method="setNew_Old"/>
</create>
</allow>
</dwr>
DWRCityChar_send JAVA文件
package com.aaa.CharChat.DWR;
import java.sql.*;
import java.util.*;
import com.db.DatabaseConnection;
import com.aaa.CharChat.initSQL.CityChart_InitSQL;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
*Title : 魅力城市聊天保存初始变量
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-09-9
**/
public class DWRCityChar_Init
{
private static int now_ChatId=0;
private static String InitSay="[公告^_^]:欢迎大家入住魅力城市^_^";
private static String InitError="[公告^_^]:对不起!系统故障!我们会尽快解决!";
private static String New_Old="0";
public static String getInitError()
{
return InitError;
}
public static String getNew_Old()
{
return New_Old;
}
public static void setNew_Old()
{
New_Old="0";
}
public static synchronized void setInitSay(String Say)
{
InitSay=Say;
}
public static synchronized int getnow_ChatId()
{
if(now_ChatId==0)
{
int temp_ID=getRenow_ChatId();
if(temp_ID>0)
{
setnow_ChatId(temp_ID);
}else
{
startnewChat();
}
}
New_Old="1";//加载聊天记录
return now_ChatId;
}
public static synchronized void setnow_ChatId(int ChatId)
{
now_ChatId=ChatId;
}
public static synchronized void startnewChat()
{
Connection conn=null;
PreparedStatement prepare=null;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getinsertnewChat());
Date online=new Date();
SimpleDateFormat fo=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String riqi=fo.format(online);
prepare.setString(1,riqi);
prepare.setString(2,InitSay);
int result=prepare.executeUpdate();
if(result>0)
{
setnow_ChatId(result);
}
else
{
startnewChat();
}
prepare.close();
conn.close();
}catch(Exception we)
{
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
}
public static int getRenow_ChatId()
{
Connection conn=null;
PreparedStatement prepare=null;
ResultSet result=null;
int Return=0;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getselectnewChatId());
result=prepare.executeQuery();
if(result.next())
{
Return=result.getInt("Id");
}
result.close();
prepare.close();
conn.close();
}catch(Exception we)
{
Return=-1;
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
return Return;
}
}
DWRCityChar_send JAVA文件
package com.aaa.CharChat.DWR;
import java.sql.*;
import java.util.*;
import com.db.DatabaseConnection;
import com.aaa.CharChat.DWR.DWRCityChar_Init;
import com.aaa.CharChat.initSQL.CityChart_InitSQL;
/**
*Title : 魅力城市聊天发送
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-9-9
**/
public class DWRCityChar_send
{
public int Chat_send(String User_name,String Content)
{
int Return=0;
try
{
String result="["+User_name+"]说:"+Content;
int nowId=DWRCityChar_Init.getnow_ChatId();
if(nowId>0)
{
Return=dosend(nowId,result);
}else
{
Return=-2;
}
}catch(Exception we)
{
}
return Return;
}
public int dosend(int nowId,String result)
{
Connection conn=null;
PreparedStatement prepare=null;
PreparedStatement prepare1=null;
ResultSet resu=null;
String Content="";
int Return=0;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getselectChatbyId());
prepare1=conn.prepareStatement(CityChart_InitSQL.getupdateChatbyId());
prepare.setInt(1,nowId);
resu=prepare.executeQuery();
if(resu.next())
{
Content=resu.getString("Content");
}
resu.close();
prepare.close();
if(Content.length()>4000)
{
DWRCityChar_Init.startnewChat();
nowId=DWRCityChar_Init.getnow_ChatId();
}
Content=result+"/n"+Content;
prepare1.setString(1,Content);
prepare1.setInt(2,nowId);
Return=prepare1.executeUpdate();
prepare1.close();
conn.close();
}catch(Exception we)
{
Return=-1;
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
prepare1.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
return Return;
}
}
DWRCityChar_Read java文件
package com.aaa.CharChat.DWR;
import java.sql.*;
import java.util.*;
import com.db.DatabaseConnection;
import com.aaa.CharChat.DWR.DWRCityChar_Init;
import com.aaa.CharChat.initSQL.CityChart_InitSQL;
/**
*Title : 魅力城市聊天发送
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-9-9
**/
public class DWRCityChar_send
{
public int Chat_send(String User_name,String Content)
{
int Return=0;
try
{
String result="["+User_name+"]说:"+Content;
int nowId=DWRCityChar_Init.getnow_ChatId();
if(nowId>0)
{
Return=dosend(nowId,result);
}else
{
Return=-2;
}
}catch(Exception we)
{
}
return Return;
}
public int dosend(int nowId,String result)
{
Connection conn=null;
PreparedStatement prepare=null;
PreparedStatement prepare1=null;
ResultSet resu=null;
String Content="";
int Return=0;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getselectChatbyId());
prepare1=conn.prepareStatement(CityChart_InitSQL.getupdateChatbyId());
prepare.setInt(1,nowId);
resu=prepare.executeQuery();
if(resu.next())
{
Content=resu.getString("Content");
}
resu.close();
prepare.close();
if(Content.length()>4000)
{
DWRCityChar_Init.startnewChat();
nowId=DWRCityChar_Init.getnow_ChatId();
}
Content=result+"/n"+Content;
prepare1.setString(1,Content);
prepare1.setInt(2,nowId);
Return=prepare1.executeUpdate();
prepare1.close();
conn.close();
}catch(Exception we)
{
Return=-1;
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
prepare1.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
return Return;
}
}
用到的JAVASCRIPT JS文件
/**
*Title : 魅力城市聊天
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-09-9
**/
var Send_state=0;//发送状态
var Chat_sendTime=500;
var Chat_sendStart;
var Chat_sendEnd;
var Chat_sendstop;
var Chat_result;
var Chat_startWrite;
var Chat_InitStart;
var Chat_Readtime=500;
function Chathotsend()
{
if(event.keyCode==10)
{
ChatSend();
}
}
function getChat_Content()
{
var Content=null;
var obj=document.getElementById('chat_input');//获取聊天内容
if(typeof(obj)=='object')
{
Content=obj.value;
if(Content!=null&&Content.length>400)
{
alert('对不起!你的聊天内容过长,请你说话要简练扼要!');
}
}else
{
Content=null;
alert('程序内部出现异常,抱歉!我们会尽快解决');
}
return Content;
}
function ChatSend()
{
var UserName="";
var obj=document.getElementById('Chat_User_name');//获取聊天内容
if(typeof(obj)=='object')
{
UserName=obj.value;
}else
{
UserName==null;
alert('程序内部出现异常,抱歉!我们会尽快解决');
return;
}
var Content=getChat_Content();
if(Content==null||Content=='')
{
ShowAlert('请您输入聊天内容!');
alert_time=setTimeout('ShowAlertCLose()',alert_showtime);
alert('请您输入聊天内容!');
return;
}
Chat_sendstop=setTimeout('ShowAlert()',Chat_sendTime);
doChatSend(UserName,Content);
var chatobj=document.getElementById('chat_input');
chatobj.value='';
chatobj.focus();
}
function doChatSend(UserName,Content)//聊天发送
{
Chat_sendStart=new Date();
CityChatsend.Chat_send(UserName,Content,Chat_doResult);
}
function Chat_doResult(Return)//结果显示
{
Send_state=Return;
Chat_sendEnd=new Date();
var temp=Chat_sendEnd-Chat_sendStart;
if(temp>50)
{
Chat_sendTime=temp;
}
}
function ShowAlert()
{
clearTimeout(Chat_sendstop);
if(Send_state==-1)
{
alert('聊天提交时,出现数据库插入异常!抱歉!请你重新输入');
}else if(Send_state==-2)
{
alert('对不起!聊天系统出现异常,给你带来不便。在此,我代表AAA国际向你表示抱歉……');
}
}
function ChatInit()
{
setInterval('ChatRead()',Chat_Readtime);
}
function ChatRead()
{
var chat_updiv=document.getElementById('chat_input_updiv');
if(chat_updiv.style.display=='')
{
CityChatRead.ChatRead(getChatContent);
Chat_startWrite=setTimeout('WriteChatContent()',1000);
}
}
function WriteChatContent()
{
clearTimeout(Chat_startWrite);
var obj=document.getElementById('chat_content');
if(typeof(obj)=='object')
{
if(Chat_result!=null&&Chat_result!='')
{
obj.value=Chat_result;
}else
{
obj.value='数据加载中......';
}
}else
{
alert('程序内部出现异常,抱歉!我们会尽快解决');
}
Chat_result=null;
}
function getChatContent(result)
{
Chat_result=result;
}
本人也是在网站开发地图时有到了这个技术,刚学习DWR半个月
就把随手写的一个简单聊天来给大家共享,切磋一下。
由于考虑到某些局部原因,只发表了一些重要技术方面的资料,没有把全部东西共享。请个人体谅。
由于DWR2.0有个Bug ,我用的DWR1.0,但是,它必须用JDK1.4
dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<init></init>
<allow>
<create creator="new" javascript="CityChatsend" scope="session">
<param name="class" value="com.aaa.CharChat.DWR.DWRCityChar_send"/>
<include method="Chat_send"/>
</create>
<create creator="new" javascript="CityChatRead" scope="session">
<param name="class" value="com.aaa.CharChat.DWR.DWRCityChar_Read"/>
<include method="ChatRead"/>
</create>
<create creator="new" javascript="CityChatInit" scope="application">
<param name="class" value="com.aaa.CharChat.DWR.DWRCityChar_Init"/>
<include method="getNew_Old"/>
<include method="setNew_Old"/>
</create>
</allow>
</dwr>
DWRCityChar_send JAVA文件
package com.aaa.CharChat.DWR;
import java.sql.*;
import java.util.*;
import com.db.DatabaseConnection;
import com.aaa.CharChat.initSQL.CityChart_InitSQL;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
*Title : 魅力城市聊天保存初始变量
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-09-9
**/
public class DWRCityChar_Init
{
private static int now_ChatId=0;
private static String InitSay="[公告^_^]:欢迎大家入住魅力城市^_^";
private static String InitError="[公告^_^]:对不起!系统故障!我们会尽快解决!";
private static String New_Old="0";
public static String getInitError()
{
return InitError;
}
public static String getNew_Old()
{
return New_Old;
}
public static void setNew_Old()
{
New_Old="0";
}
public static synchronized void setInitSay(String Say)
{
InitSay=Say;
}
public static synchronized int getnow_ChatId()
{
if(now_ChatId==0)
{
int temp_ID=getRenow_ChatId();
if(temp_ID>0)
{
setnow_ChatId(temp_ID);
}else
{
startnewChat();
}
}
New_Old="1";//加载聊天记录
return now_ChatId;
}
public static synchronized void setnow_ChatId(int ChatId)
{
now_ChatId=ChatId;
}
public static synchronized void startnewChat()
{
Connection conn=null;
PreparedStatement prepare=null;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getinsertnewChat());
Date online=new Date();
SimpleDateFormat fo=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String riqi=fo.format(online);
prepare.setString(1,riqi);
prepare.setString(2,InitSay);
int result=prepare.executeUpdate();
if(result>0)
{
setnow_ChatId(result);
}
else
{
startnewChat();
}
prepare.close();
conn.close();
}catch(Exception we)
{
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
}
public static int getRenow_ChatId()
{
Connection conn=null;
PreparedStatement prepare=null;
ResultSet result=null;
int Return=0;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getselectnewChatId());
result=prepare.executeQuery();
if(result.next())
{
Return=result.getInt("Id");
}
result.close();
prepare.close();
conn.close();
}catch(Exception we)
{
Return=-1;
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
return Return;
}
}
DWRCityChar_send JAVA文件
package com.aaa.CharChat.DWR;
import java.sql.*;
import java.util.*;
import com.db.DatabaseConnection;
import com.aaa.CharChat.DWR.DWRCityChar_Init;
import com.aaa.CharChat.initSQL.CityChart_InitSQL;
/**
*Title : 魅力城市聊天发送
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-9-9
**/
public class DWRCityChar_send
{
public int Chat_send(String User_name,String Content)
{
int Return=0;
try
{
String result="["+User_name+"]说:"+Content;
int nowId=DWRCityChar_Init.getnow_ChatId();
if(nowId>0)
{
Return=dosend(nowId,result);
}else
{
Return=-2;
}
}catch(Exception we)
{
}
return Return;
}
public int dosend(int nowId,String result)
{
Connection conn=null;
PreparedStatement prepare=null;
PreparedStatement prepare1=null;
ResultSet resu=null;
String Content="";
int Return=0;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getselectChatbyId());
prepare1=conn.prepareStatement(CityChart_InitSQL.getupdateChatbyId());
prepare.setInt(1,nowId);
resu=prepare.executeQuery();
if(resu.next())
{
Content=resu.getString("Content");
}
resu.close();
prepare.close();
if(Content.length()>4000)
{
DWRCityChar_Init.startnewChat();
nowId=DWRCityChar_Init.getnow_ChatId();
}
Content=result+"/n"+Content;
prepare1.setString(1,Content);
prepare1.setInt(2,nowId);
Return=prepare1.executeUpdate();
prepare1.close();
conn.close();
}catch(Exception we)
{
Return=-1;
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
prepare1.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
return Return;
}
}
DWRCityChar_Read java文件
package com.aaa.CharChat.DWR;
import java.sql.*;
import java.util.*;
import com.db.DatabaseConnection;
import com.aaa.CharChat.DWR.DWRCityChar_Init;
import com.aaa.CharChat.initSQL.CityChart_InitSQL;
/**
*Title : 魅力城市聊天发送
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-9-9
**/
public class DWRCityChar_send
{
public int Chat_send(String User_name,String Content)
{
int Return=0;
try
{
String result="["+User_name+"]说:"+Content;
int nowId=DWRCityChar_Init.getnow_ChatId();
if(nowId>0)
{
Return=dosend(nowId,result);
}else
{
Return=-2;
}
}catch(Exception we)
{
}
return Return;
}
public int dosend(int nowId,String result)
{
Connection conn=null;
PreparedStatement prepare=null;
PreparedStatement prepare1=null;
ResultSet resu=null;
String Content="";
int Return=0;
try
{
conn=DatabaseConnection.getConnection();
prepare=conn.prepareStatement(CityChart_InitSQL.getselectChatbyId());
prepare1=conn.prepareStatement(CityChart_InitSQL.getupdateChatbyId());
prepare.setInt(1,nowId);
resu=prepare.executeQuery();
if(resu.next())
{
Content=resu.getString("Content");
}
resu.close();
prepare.close();
if(Content.length()>4000)
{
DWRCityChar_Init.startnewChat();
nowId=DWRCityChar_Init.getnow_ChatId();
}
Content=result+"/n"+Content;
prepare1.setString(1,Content);
prepare1.setInt(2,nowId);
Return=prepare1.executeUpdate();
prepare1.close();
conn.close();
}catch(Exception we)
{
Return=-1;
System.out.println(we.getMessage());
}finally
{
try
{
if(!conn.isClosed())
{
prepare.close();
prepare1.close();
conn.close();
}
}
catch(Exception ew)
{
}
}
return Return;
}
}
用到的JAVASCRIPT JS文件
/**
*Title : 魅力城市聊天
*Author : JAVA^_^枭龙---孙德华
*Project: 数字城市
*Date : 2006-09-9
**/
var Send_state=0;//发送状态
var Chat_sendTime=500;
var Chat_sendStart;
var Chat_sendEnd;
var Chat_sendstop;
var Chat_result;
var Chat_startWrite;
var Chat_InitStart;
var Chat_Readtime=500;
function Chathotsend()
{
if(event.keyCode==10)
{
ChatSend();
}
}
function getChat_Content()
{
var Content=null;
var obj=document.getElementById('chat_input');//获取聊天内容
if(typeof(obj)=='object')
{
Content=obj.value;
if(Content!=null&&Content.length>400)
{
alert('对不起!你的聊天内容过长,请你说话要简练扼要!');
}
}else
{
Content=null;
alert('程序内部出现异常,抱歉!我们会尽快解决');
}
return Content;
}
function ChatSend()
{
var UserName="";
var obj=document.getElementById('Chat_User_name');//获取聊天内容
if(typeof(obj)=='object')
{
UserName=obj.value;
}else
{
UserName==null;
alert('程序内部出现异常,抱歉!我们会尽快解决');
return;
}
var Content=getChat_Content();
if(Content==null||Content=='')
{
ShowAlert('请您输入聊天内容!');
alert_time=setTimeout('ShowAlertCLose()',alert_showtime);
alert('请您输入聊天内容!');
return;
}
Chat_sendstop=setTimeout('ShowAlert()',Chat_sendTime);
doChatSend(UserName,Content);
var chatobj=document.getElementById('chat_input');
chatobj.value='';
chatobj.focus();
}
function doChatSend(UserName,Content)//聊天发送
{
Chat_sendStart=new Date();
CityChatsend.Chat_send(UserName,Content,Chat_doResult);
}
function Chat_doResult(Return)//结果显示
{
Send_state=Return;
Chat_sendEnd=new Date();
var temp=Chat_sendEnd-Chat_sendStart;
if(temp>50)
{
Chat_sendTime=temp;
}
}
function ShowAlert()
{
clearTimeout(Chat_sendstop);
if(Send_state==-1)
{
alert('聊天提交时,出现数据库插入异常!抱歉!请你重新输入');
}else if(Send_state==-2)
{
alert('对不起!聊天系统出现异常,给你带来不便。在此,我代表AAA国际向你表示抱歉……');
}
}
function ChatInit()
{
setInterval('ChatRead()',Chat_Readtime);
}
function ChatRead()
{
var chat_updiv=document.getElementById('chat_input_updiv');
if(chat_updiv.style.display=='')
{
CityChatRead.ChatRead(getChatContent);
Chat_startWrite=setTimeout('WriteChatContent()',1000);
}
}
function WriteChatContent()
{
clearTimeout(Chat_startWrite);
var obj=document.getElementById('chat_content');
if(typeof(obj)=='object')
{
if(Chat_result!=null&&Chat_result!='')
{
obj.value=Chat_result;
}else
{
obj.value='数据加载中......';
}
}else
{
alert('程序内部出现异常,抱歉!我们会尽快解决');
}
Chat_result=null;
}
function getChatContent(result)
{
Chat_result=result;
}
- JAVA中DWR开发项目实战经验(转载)
- JAVA中DWR开发项目实战经验
- JAVA中DWR开发项目简单聊天实战经验
- Redis在项目中实战经验
- 产品项目里的九个敏捷开发实战经验
- Android开发实战经验
- 转载】使用Unity开发项目的一点经验
- 【转载】使用Unity开发项目的一点经验
- jsp项目中使用dwr引起的Java heap space
- JAVA异常处理之重--项目实战经验
- Java项目实战经验—程序员成长的钥匙
- 在项目中开发中的一些经验
- 项目中dwr的使用
- 【转载】分享8年开发经验,浅谈java程序员职业规划
- Java web 项目中防止资源被盗链下载[参考:Java.Web开发实战1200例]
- 实战DWR
- 实战dwr
- 成功项目的经验(转载)
- Java程序员面试宝典--Super
- GetMessage PeekMessage SendMessage PostMessage
- Java程序员面试宝典--关于数字的智力测试
- java项目中的路径调用
- _tmain与main的区别
- JAVA中DWR开发项目实战经验(转载)
- Java程序员面试宝典--this
- Virtools教程系列
- 安装 ebox 防火墙
- 第一篇
- 从《我的青春我做主》说起
- 刚进的CSDN 高兴!
- Microsoft .NET Framework 3.5 SP1 /2.0 SP2 / 3.0 SP2 简体中文静默安装版
- oracle rac