实例比较DWR和AJAX应用的优缺点

http://blog.sina.com.cn/s/blog_4c925dca01009xrb.html

常,我们在一些网站上,都会有注册功能,在注册的时候,我们通常要判断,该用户名是否已经存在,在用户输入完后,离开时触发事件onblur,数据库中如该用户存在,会有提示信息,不存在,也会有提示信息,那么这个功能是如何实现的呢?
<wbr><wbr><wbr><wbr>下面,阿堂结合单位的一个考试系统中,判断出“试卷”时,该试卷标题是否存在,给出相应信息.</wbr></wbr></wbr></wbr>
这里阿堂给出两种方法,一种方法是用ajax,一种方法是用dwr框架(实际上也是对ajax的封装),阿堂认为用dwr框架会更简单

<wbr><wbr><wbr></wbr></wbr></wbr>效果如下

<wbr></wbr>

实例比较DWR和AJAX应用的优缺点<wbr></wbr>

<wbr><wbr><wbr>两者的优缺点 ajax:配置少,js代码写得多<wbr>;dwr配置多,js代码很少<wbr>,其实配置也得很简单的,朋友们如有建议,欢迎给阿堂留言</wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>第一种情况:用ajax 技术

main.jsp文件(主要代码,下面同上,不再作说明)

<wbr>&lt;td width="17%" bgcolor="#6666FF"&gt;&lt;strong&gt;&lt;font color="#FFFFFF" size="2"&gt;试卷标题&lt;/font&gt;&lt;/strong&gt;&lt;/td&gt;<br><wbr>&lt;td width="83%"&gt;&lt;input name="title" type="text" id="title" onblur="return checkTitle(this.value)"&gt;&lt;/td&gt;<br><wbr><wbr><wbr>&lt;/tr&gt;<br><wbr><wbr><wbr>&lt;tr bgcolor="#FFFFFF"&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;td bgcolor="#6666FF"&gt;&lt;strong&gt;&lt;font size="2" color="#FFFFFF"&gt;试题组别&lt;/font&gt;&lt;/strong&gt;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;td&gt; &lt;font size="2" &gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;select name="examgroup" id="examgroup"&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3仓务组X-SVR" selected&gt;ISH3仓务组X-SVR&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3仓务组P-SVR"&gt;ISH3仓务组P-SVR&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3进货组"&gt;ISH3进货组&lt;/option&gt;<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3L/S组"&gt;ISH3L/S组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH3RPR组"&gt;ISH3RPR组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH1仓务组"&gt;ISH1仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH2仓务组"&gt;ISH2仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="PM仓务组"&gt;PM仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="德宝MBL仓务组"&gt;德宝MBL仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="德宝其它客户仓务组"&gt;德宝其它客户仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH1进货组"&gt;ISH1进货组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH2进货组"&gt;ISH2进货组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH1RPR组"&gt;ISH1RPR组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH2RPR组"&gt;ISH2RPR组&lt;/option&gt;<br><wbr><wbr>&lt;option value="德宝RPR组"&gt;德宝RPR组&lt;/option&gt;<wbr><br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH1L/S组"&gt;ISH1L/S组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH2L/S组"&gt;ISH2L/S组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH2调度组"&gt;ISH2调度组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="沙井综合物流仓务组"&gt;沙井综合物流仓务组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/select&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/font&gt;&lt;/td&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>对应的js代码</wbr>
<wbr>&lt;script language="javascript"&gt;<br><wbr><wbr>var XMLHttpReq;<wbr><wbr><br><wbr><wbr>// alert(x);<br><wbr><wbr>// onblur="return checkTitle(this.value);"<br><wbr><wbr>//创建XMLHttpRequest对象<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr>function createXMLHttpRequest()<br><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr>if(window.XMLHttpRequest) { //Mozilla 浏览器<br><wbr><wbr><wbr><wbr>XMLHttpReq = new XMLHttpRequest();<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>else if (window.ActiveXObject) { // IE浏览器<br><wbr><wbr><wbr><wbr>try {<br><wbr><wbr><wbr><wbr><wbr>XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");<br><wbr><wbr><wbr><wbr>} catch (e) {<br><wbr><wbr><wbr><wbr><wbr>try {<br><wbr><wbr><wbr><wbr><wbr><wbr>XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");<br><wbr><wbr><wbr><wbr><wbr>} catch (e) {}<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr><br><wbr><wbr>function processResponse() {<br><wbr><wbr><wbr>// alert(XMLHttpReq.responseText);<br><wbr><wbr><wbr><wbr><wbr>if (XMLHttpReq.readyState == 4) { // 判断对象状态<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>replaceContent();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} else { //页面不正常<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>window.alert("您所请求的页面有异常。");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>window.alert(XMLHttpReq.status);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><br><wbr><wbr>function checkTitle(value)<br><wbr><wbr>{<br><wbr><wbr><wbr>var i = document.getElementByIdx("examgroup").selectedIndex;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var x = document.getElementByIdx("examgroup").options[i].value;<br><wbr><wbr><wbr><wbr><wbr><wbr>var url = "</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<a href="http://localhost:8081/ishexam/checkTitle.jsp?title=" +value+"&remark="+x" style="text-decoration: none; color: rgb(62, 110, 43); ">http://localhost:8081/ishexam/checkTitle.jsp?title="+value+"&remark="+x;
<wbr><wbr><wbr><wbr><wbr><wbr>// alert(url); 此处传值正常<br><wbr><wbr><wbr><wbr><wbr><wbr>createXMLHttpRequest();<wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>XMLHttpReq.onreadystatechange = processResponse;<br><wbr><wbr><wbr><wbr><wbr><wbr>XMLHttpReq.open("GET", url, true);<br><wbr><wbr><wbr><wbr><wbr><wbr>XMLHttpReq.send(null);<wbr><br><wbr><wbr>}<br><wbr><wbr><br><wbr><wbr><wbr>function replaceContent()<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>// alert(XMLHttpReq.responseText);<br><wbr><wbr><wbr>var content=XMLHttpReq.responseText;<br><wbr><wbr><wbr>var str=content.substr(0,3);<br><wbr><wbr><wbr>// alert(content);<wbr><br><wbr><wbr><wbr>if (str=="yes")<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>alert("对不起,这个组的这套试题已经出过,请重新出");<br><wbr><wbr><wbr><wbr>document.getElementByIdx("title").focus();<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>else<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>&lt;/script&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

checkTitle.jsp文件

<%@ page language="java" import="java.util.*,com.ish.struts.util.*" pageEncoding="gbk"%>
<%

<wbr>Tools tool=new Tools();<br><wbr>request.setCharacterEncoding("GBK");<br><wbr>String title = new String(request.getParameter("title").trim().getBytes("iso8859-1"),"GBK");<br><wbr>String remark = new String(request.getParameter("remark").trim().getBytes("iso8859-1"),"GBK");<br><wbr>String tempstr;<br><wbr><br><wbr>if(tool.isExistTitleAndRemark(title,remark))<br><wbr>{<br><wbr><wbr>tempstr="yes";</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>}<br><wbr>else<br><wbr>{<br><wbr><wbr>tempstr="no";</wbr></wbr></wbr></wbr></wbr>

<wbr>}<br><wbr>out.clear();<br><wbr>out.print(tempstr.trim());<br><wbr><br><wbr>%&gt;</wbr></wbr></wbr></wbr></wbr>

Tools.java文件中的调用方法

<wbr>//查询exam_paper表是否存在如下title和remark的记录<br><wbr>public boolean isExistTitleAndRemark(String title,String remark)<br><wbr>{<br><wbr><wbr>boolean flag=false;<br><wbr><wbr>conn=gd.getConnection();<wbr><wbr><br><wbr><wbr>String sql="select * from exam_paper where title=? and<wbr>remark=?";<br><wbr><wbr>//String sql1="select title,remark from exam_paper where title='ISH12仓' and<wbr>remark='德宝MBL仓务组'";<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr><br><wbr><wbr><wbr>ps=conn.prepareStatement(sql);<br><wbr><wbr><wbr>ps.setString(1, title.trim());<br><wbr><wbr><wbr>ps.setString(2, remark.trim());<br><wbr><wbr><wbr>ResultSet rs=ps.executeQuery();<br><wbr><wbr><wbr>if(rs.next())<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>flag=true;<br> //<wbr><wbr><wbr><wbr>System.out.println(rs.getString(1));<br> //<wbr><wbr><wbr><wbr>System.out.println(rs.getString(2));<wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr>catch(Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>e.printStackTrace();<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try{<br><wbr><wbr><wbr><wbr>if(conn!=null)<br><wbr><wbr><wbr><wbr><wbr>conn.close();<br><wbr><wbr><wbr>}catch(Exception ex){<br><wbr><wbr><wbr><wbr><wbr>ex.printStackTrace();<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr><br><wbr><wbr>return flag;<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

<wbr><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:21px"><strong>第二种方法。用dwr来实现<span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">(导入dwr.jar)</span></strong></span><br></wbr>main.jsp文件

<wbr><wbr><wbr>&lt;tr bgcolor="#FFFFFF"&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;td width="17%" bgcolor="#6666FF"&gt;&lt;strong&gt;&lt;font color="#FFFFFF" size="2"&gt;试卷标题&lt;/font&gt;&lt;/strong&gt;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>&lt;td width="83%"&gt;&lt;input name="title" type="text" id="title" onblur="return checkTitle1(this.value)"&gt;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>&lt;/tr&gt;<br><wbr><wbr><wbr>&lt;tr bgcolor="#FFFFFF"&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;td bgcolor="#6666FF"&gt;&lt;strong&gt;&lt;font size="2" color="#FFFFFF"&gt;试题组别&lt;/font&gt;&lt;/strong&gt;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;td&gt; &lt;font size="2" &gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;select name="examgroup" id="examgroup"&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3仓务组X-SVR" selected&gt;ISH3仓务组X-SVR&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3仓务组P-SVR"&gt;ISH3仓务组P-SVR&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3进货组"&gt;ISH3进货组&lt;/option&gt;<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH3L/S组"&gt;ISH3L/S组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH3RPR组"&gt;ISH3RPR组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH1仓务组"&gt;ISH1仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH2仓务组"&gt;ISH2仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="PM仓务组"&gt;PM仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="德宝MBL仓务组"&gt;德宝MBL仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="德宝其它客户仓务组"&gt;德宝其它客户仓务组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH1进货组"&gt;ISH1进货组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH2进货组"&gt;ISH2进货组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH1RPR组"&gt;ISH1RPR组&lt;/option&gt;<br><wbr><wbr>&lt;option value="ISH2RPR组"&gt;ISH2RPR组&lt;/option&gt;<br><wbr><wbr>&lt;option value="德宝RPR组"&gt;德宝RPR组&lt;/option&gt;<wbr><br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH1L/S组"&gt;ISH1L/S组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH2L/S组"&gt;ISH2L/S组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;option value="ISH2调度组"&gt;ISH2调度组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;option value="沙井综合物流仓务组"&gt;沙井综合物流仓务组&lt;/option&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/select&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/font&gt;&lt;/td&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:21px">对应的js代码</span><br><wbr>&lt;script type='text/javascript' src='/ishexam/dwr/interface/tools.js'&gt;&lt;/script&gt;<br><wbr>&lt;script type='text/javascript' src='/ishexam/dwr/engine.js'&gt;&lt;/script&gt;<br><wbr>&lt;script type='text/javascript' src='/ishexam/dwr/util.js'&gt;&lt;/script&gt;<br><wbr><wbr>&lt;script&gt;<br><wbr><wbr><wbr>function checkTitle1(value)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>var i = document.getElementByIdx("examgroup").selectedIndex;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var x = document.getElementByIdx("examgroup").options[i].value;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>tools.isExistTitleAndRemark1(value,x,callBackResult);<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr><wbr>function callBackResult(data)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>if(data!="")<br><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr>alert(data);<br><wbr><wbr><wbr><wbr><wbr><wbr>document.getElementByIdx("title").focus();<br><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr>else<br><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr><br><wbr>&lt;/script&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Tools.java文件中对应的方法如下

<wbr>public String isExistTitleAndRemark1(String title,String remark)<br><wbr>{<br><wbr><wbr>String flag="";<br><wbr><wbr>conn=gd.getConnection();<wbr><wbr><br><wbr><wbr>String sql="select * from exam_paper where title=? and<wbr>remark=?";<br><wbr><wbr>//String sql1="select title,remark from exam_paper where title='ISH12仓' and<wbr>remark='德宝MBL仓务组'";<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr><br><wbr><wbr><wbr>ps=conn.prepareStatement(sql);<br><wbr><wbr><wbr>ps.setString(1, title.trim());<br><wbr><wbr><wbr>ps.setString(2, remark.trim());<br><wbr><wbr><wbr>ResultSet rs=ps.executeQuery();<br><wbr><wbr><wbr>if(rs.next())<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>flag="对不起,这个组的这套试题已经出过,请重新出";<br> //<wbr><wbr><wbr><wbr>System.out.println(rs.getString(1));<br> //<wbr><wbr><wbr><wbr>System.out.println(rs.getString(2));<wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr>catch(Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>flag="";<br><wbr><wbr><wbr>e.printStackTrace();<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try{<br><wbr><wbr><wbr><wbr>if(conn!=null)<br><wbr><wbr><wbr><wbr><wbr>conn.close();<br><wbr><wbr><wbr>}catch(Exception ex){<br><wbr><wbr><wbr><wbr><wbr>ex.printStackTrace();<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr><br><wbr><wbr>return flag.trim();<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>

<wbr></wbr>


<wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:21px">web.xml文件中配置</span><br><wbr>&lt;servlet&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;servlet-class&gt; org.directwebremoting.servlet.DwrServlet &lt;/servlet-class&gt;<br><wbr><wbr><wbr>&lt;init-param&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;param-name&gt;debug&lt;/param-name&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;param-value&gt;true&lt;/param-value&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/init-param&gt;<br><wbr>&lt;/servlet&gt;<br><wbr><br><wbr>&lt;servlet-mapping&gt;<wbr><wbr><wbr><br><wbr><wbr><wbr>&lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;<br><wbr><wbr><wbr>&lt;url-pattern&gt;/dwr/*&lt;/url-pattern&gt;<br><wbr>&lt;/servlet-mapping&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

<wbr>在web-inf目录下生成一个<strong>dwr.xml</strong>文件,配置如下</wbr>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting
2.0//EN" "
http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<wbr><br> &lt;dwr&gt;<br><wbr>&lt;allow&gt;<br><wbr><wbr><wbr>&lt;create creator="new" javascript="tools"&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;param name="class" value="com.ish.struts.util.Tools" /&gt;<br><wbr><wbr><wbr>&lt;/create&gt;<br><wbr>&lt;/allow&gt;<br> &lt;/dwr&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

导入包dwr.jar包


你可能感兴趣的:(Ajax)