- A+
所属分类:Web前端
【JSP+Ajax实现无刷新判断日志重复或注册用户名是否重复】
JSP+Ajax技术实现无刷新查询数据库并进行相关提示,多用注册用户是否被占用或日志查重等。下面介绍一下我最近在做的一个程序所用到的日志查重功能。
logadd.jsp代码如下:
- <%@ page language="java" import="java.util.*,jform.Allclass" contentType="text/html;charset=gb2312"%>
- <jsp:useBean id="log" class="jBean.InsertMessage" scope="page"/>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>添加日志</title>
- <link rel="stylesheet" href="../css/main.css" type="text/css" />
- <script type="text/javascript">
- function check_fir()
- {
- if (document.log.log_title.value== "")
- {
- window.alert("请填写日志标题!");
- document.log.log_title.focus();
- return (false);
- }
- if (document.log.showmen.value== "")
- {
- window.alert("请填写日志作者!");
- document.log.showmen.focus();
- return (false);
- }
- if (document.log.content.value== "")
- {
- window.alert("请填写日志内容!");
- document.log.content.focus();
- return (false);
- }
- if (document.log.log_title.length>2000 || document.log.log_title.length<16)
- {
- window.alert("请填写您的用户名!");
- document.log.log_title.focus();
- return (false);
- }
- }
- //创建XMLHttpRequest对象
- function GetO()
- {
- var ajax=false;
- try
- {
- ajax = new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch (e)
- {
- try
- {
- ajax = new ActiveXObject("Microsoft.XMLHTTP");
- }
- catch (E)
- {
- ajax = false;
- }
- }
- if (!ajax && typeof XMLHttpRequest!='undefined')
- {
- ajax = new XMLHttpRequest();
- }
- return ajax;
- }
- function getMyHTML(serverPage, objID) {
- var ajax = GetO();
- //得到了一个html元素,在下面给这个元素的属性赋值
- var obj = document.all[objID];
- //设置请求方法及目标,并且设置为异步提交
- ajax.open("post", serverPage, true);
- ajax.onreadystatechange = function()
- {
- if (ajax.readyState == 4 && ajax.status == 200)
- {
- //innerHTML是HTML元素的属性,如果您不理解属性那就理解为HTML元素的变量
- //ajax.responseText是服务器的返回值,把值赋给id=log_title_msg的元素的属性
- //innerHTML这个属性或说这个变量表示一组开始标记和结束标记之间的内容
- obj.innerHTML = ajax.responseText;
- }
- }
- //发送请求
- ajax.send(null);
- }
- function checkLogTitle()
- {
- getMyHTML("logrepeat.jsp?log_title="+document.getElementById("log_title").value, "log_title_msg");
- }
- //这个函数的作用是当用户的焦点从其他地方回到log_title这个输入框时再给属性赋回原内容
- function logTitleMsg(msg)
- {
- if(msg=='log_title_msg')
- {
- document.all[msg].innerHTML = "<div style='color:red;' align='left'>系统会自动判断日志标题是否重复,请关注此提示以避免发布重复文章。</div>";
- }
- }
- </script>
- </head>
- <body>
- <form action="addlog.jsp" method="post" enctype="multipart/form-data" name="log" onSubmit="return check_fir()">
- <table class="ta" width="600" border="0">
- <tr>
- <td width="522" height="189" valign="top"><table width="600" border="0">
- <tr>
- <td colspan="2" align="center"><strong>发表日志</strong></td>
- </tr>
- <tr>
- <td width="60" align="left">日志标题:</td>
- <td width="452" align="left"><input id="log_title" name="log_title" type="text"size="40" onBlur="javaScript:checkLogTitle();" onFocus="return logTitleMsg('log_title_msg');" />
- <span id="log_title_msg" class="intro">*</span></td>
- </tr>
- <tr>
- <td align="left">日志分类:</td>
- <td align="left"><select name="class_name">
- <%
- Collection c=log.showLogClass();
- Iterator it=c.iterator();
- while(it.hasNext())
- {
- Allclass log_class=(Allclass)it.next();
- %>
- <option value="<%=log_class.getClassname()%>"><%=log_class.getClassname()%></option>
- <%}%>
- </select>
- <span class="intro">*</span> </td>
- </tr>
- <tr>
- <td align="left">发 布 人:</td>
- <td align="left"><input name="showmen" type="text" /></td>
- </tr>
- <tr>
- <td align="left">上传文件:</td>
- <td align="left"><input type="file" name="myfile" />
- </td>
- </tr>
- <tr>
- <td height="1" colspan="2" align="left" bgcolor="00759b"></td>
- </tr>
- <tr>
- <td colspan="2" align="left">日志内容:<span class="intro">(内容字数限制在2000字以内)</span></td>
- </tr>
- <tr>
- <td colspan="2" align="left" valign="top"><textarea class="content" rows="10" cols="50" name="log_content"></textarea></td>
- </tr>
- <tr>
- <td colspan="2" align="left"> <input name="Submit" type="submit" value="发布日志">
- <input name="reset" type="reset" id="reset" value="清除重写"></td>
- </tr>
- </table></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
查重前台处理页面logrepeat.jsp代码如下:
- <%@ page language="java" import="java.util.Date" contentType="text/html; charset=gb2312"%>
- <jsp:directive.page import="com.jspsmart.upload.SmartUpload"/>
- <jsp:useBean id="addlog" class="jform.Addlog" scope="request"/>
- <jsp:useBean id="insert" class="jBean.InsertLog" scope="request"/>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>日志上传</title>
- </head>
- <body>
- <%
- String is=new String(request.getParameter("log_title").getBytes("ISO-8859-1"), "GBK");
- try{
- int isSave=insert.logRepeat(is);
- if (isSave==-1){
- out.println("<div style='color:red;' align='left'>日志重复!</div>");
- }
- }
- catch(Exception ee){out.println("系统出错");}
- %>
- </body>
- </html>
日志查重后台处理文件InsertLog.java代码如下:
- /*
- * 日志查重
- */
- int isSave=1;
- public int logRepeat(String log_title)
- {
- ResultSet rs=null;
- Statement stm=null;
- StringBuffer sb = new StringBuffer();
- //sb.append("select * from log_add where log_title = ").append("'");
- //sb.append("这是一个测试的日志3").append("'");
- String sq="select * from log_add where log_title = ?";
- //String sql="select * from log_add where log_title=";
- try{
- stm= dd.con.createStatement();
- pstm=dd.con.prepareStatement(sq);
- pstm.setString(1, log_title);
- rs=pstm.executeQuery();
- //rs=stm.executeQuery(sb.toString());
- //rs=stm.executeQuery(sq);
- //rs.next();
- if(rs.next())isSave=-1;
- //System.out.println(sb.toString());
- //System.out.println(log_title);
- //System.out.println(dd.con);
- }
- catch(Exception e){
- System.out.print(e);
- }
- return isSave;
- }