动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Jsp → 阅读文章

 开发实例:JSP中实现全文检索

作者来源: 
阅读 1343 人次 , 2006-4-19 1:41:00 

JSP中的全文检索

全文检索一直都是web方面的关键技术,如何在浩如烟海的信息中找到自己想要的信息是人们最关心的。鼎鼎大名的GOOGLE就是一个很成功的例子,网络上的人们大部分都用GOOGLE来查找自己需要的内容。全文检索主要有两个技术指标:快速和精确。 前一段时间做了一个新闻系统,老板要加上全文检索的功能,想了很久才用一个不太高明的方法实现了。现在分享一下,希望是抛砖引玉吧,如果大家有更好的办法请跟在后边:)

先介绍一下我的新闻系统:数据库里存新闻的基本信息,如标题,发布人,发布时间,主体新闻的文件名。新闻主体是html格式的静态页(第一是要提高速度,减少数据库的压力。第二是数据库处理大字符串的时候会有问题。)。全文检索的思路是:先从数据库里把所有的新闻检索出来,把主体新闻找到,然后通过io操作把主体新闻读到一个字符串中。再去掉多余的东西,象html标记什么的,再用正则表达式对这个字符串查找,如果找到符合条件的信息,就记录这条新闻。最后返回所有的符合条件的新闻显示给用户。

下面这段代码是输入查询条件的代码,查询关键字用”+”隔开:search.jsp

<html>


<head>


<link rel="stylesheet" href="css/style3.css">


<title>新闻搜索</title>


<script language="javascript">     


function subform()


{  


 if (document.zl_form.keyword.value=="")


 {


  alert("请输入关键字!");


  document.zl_form.keyword.focus();


  return false;


 }    


 return true;      


}


</script>


</head>


<body bgcolor="#F0F6E2">


<form name="zl_form"  target="_new" method="post" action="aftsearch.jsp" onsubmit="return subform()">


  <table width="600" bgcolor="#F0F6E2">


    <tr>


      <td colspan="4" height="10">&nbsp; </td>


    </tr>


    <tr>


      <td width="14%">输入查询关键字:</td>


      <td align="left" width="65%">


        <input size="50" type="text" name="keyword" style="font-size: 9pt">


        <input type="submit" name="submit" value="搜索" style="font-size: 9pt">


      </td>


    </tr>


    <tr>


      <td colspan="2" height="9" align="left">


        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>


        <font color="red" size="+1">说明:如果有多个查询条件,中间用</font><font size="+2">+</font><font color="red" size="+1">隔开。如:1+2+3+4...</font></td>


    </tr>


  </table>


</form>


</body>


</html>


下面的代码是全文检索主体javabean的代码:newsSearch.java


package NEWS;


import java.sql.*;


import java.lang.*;


import java.text.*;


import java.util.*;


import java.io.*;


import java.util.regex.*;


import DBstep.iDBManager2000;//数据库操作的bean


public class newsSearch {


  private String filePath=null;//主体新闻存放的目录


  private String keyWord=null;//查询关键字


  private Vector news = new Vector();//存放符合条件的结果


  public newsSearch() { }


  public void setFilePath(String s) {


    this.filePath=s;


  }


  public void setKeyWord(String s) {


    this.keyWord=s;


  }


  public Vector getResult() {


    return news;


  }


  public void search() {


  //打开数据库


ResultSet result=null;


   String mSql=null;


   PreparedStatement prestmt=null;


   DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();


   DbaObj.OpenConnection();


   try {


  //检索所有的新闻


   mSql="select * from t_news_detail  order by release_time desc";


   result=DbaObj.ExecuteQuery(mSql);


   while(result.next())


   {


    String id=result.getString("id");


    String title=result.getString("title");


    String release_time=result.getString("release_time");


    String news_type=result.getString("type");


    String content=result.getString("content");


    String man_add=result.getString("man_add");


      //按行读文件


      String trace=filePath+content+".html";


      FileReader  myFileReader=new FileReader(trace);


    BufferedReader myBufferedReader=new BufferedReader(myFileReader);


    String myString=null;


    String resultString=new String();


    while((myString=myBufferedReader.readLine())!=null)


    { 


     resultString=resultString+myString;


     }


      //去掉多余字符


   HtmlEncode.HtmlEncode Html=new HtmlEncode.HtmlEncode();//这个bean去掉多余的字符,新闻是自己生成的文件,可以尽量多的删除多余字符


   resultString=Html.TextEncode(resultString);


   myFileReader.close();


   //取出查询关键字


   Pattern p=null;


   Matcher m=null;


   p = Pattern.compile("\\+");


   String[] a=p.split(keyWord);//把关键字用+分开


   //全文检索


   String searchResult="1";//检索结果


   int i;


   for(i=0;i<a.length;i++)//逐个按关键字查找,如果所有的关键字都符合,则记录结果


  {


   p = Pattern.compile(a[i].toString());


   m = p.matcher(resultString);


   if (!(m.find())) {


    searchResult="0";


      }


     }


  //记录符合条件的新闻    


  if(searchResult.equals("1")) 


  {


   News resultNews=new News();//存放结果的类,和数据库的结构基本一致


   resultNews.content=content;


   resultNews.release_time=release_time;


   resultNews.type=news_type;


   resultNews.man_add=man_add;


   resultNews.title=title;


   news.addElement(resultNews);//最后的结果集,要返回客户端


   }


   }


  //关闭数据库


  DbaObj.CloseConnection() ; 


    /SPAN>}catch(Exception e){


        System.out.println(e.toString());


      }


  }


 public class News { //存放结果的类


    String content;


    String release_time;


      String type;


      String man_add;


      String title;


    public String getContent() { return this.content; }


      public String getTitle() { return this.title; }


    public String getTime() { return this.release_time; }


      public String getType() { return this.type; }


    public String getMan_add() { return this.man_add; }


  }


}


下面的代码是调用的:aftsearch.jsp


<%@ page contentType="text/html; charset=gb2312" %>


<%@ page import="java.util.*" %>


<%


 request.setCharacterEncoding("GB2312");


 String keyword=request.getParameter("keyword");  //接收关键字


 String trace=getServletContext().getRealPath("/")+"xwxx\\news\\";//主体新闻存放路径


 NEWS.newsSearch newsSearch=new NEWS.newsSearch();//初始化检索的bean


 newsSearch.setFilePath(trace);//设置主体新闻路径


 newsSearch.setKeyWord(keyword);//设置关键字


 newsSearch.search();//检索


 Vector news=newsSearch.getResult();//取到结果


%>


<html>


<head>


<title>新闻搜索</title>


<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">


<link rel="stylesheet" href="../css/style3.css">


&l;script LANGUAGE="javascript">


 function open_window(id)


{


  locat="./news/"+id+".html";


  window.open(locat,"new","width=550,height=500 ,scrollbars=yes")


}


</script>


</head>


<object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">


<param name="Command" value="Maximize"></object>


<body bgcolor=#F5FAF3 leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">


<script>


hh2.Click();


</script>


<table width="100%"" border="0">


  <tr>


    <td colspan=5>


      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


       </font>


    </td>


  </tr>


  <tr valign="middle">


    <td width="45%" height="22">


      <div align="center" class = "t_header"> </div>


    </td>


    <td width="15%" height="22">


      <div align="center" class = "t_header"> </div>


    </td>


      <td width="15%" height="22">


      <div align="center" class = "t_header"> </div>


    </td>


    <td width="25%" height="22">


      <div align="center" class = "t_header"> </div>


    </td>


  </tr>


  <tr bgcolor="#B7D79F" valign="middle">


    <td colspan="4" height="2"></td>


  </tr>


</table>


<table width="100%"" border="0" bordercolor="#99CCFF">


<%


 String color=null;


 int j=0;


 if(!(news.size()==0)) {


 for (int i = 0; i < news.size(); i++) {


 j++;


 NEWS.newsSearch.News  myNews=(NEWS.newsSearch.News)news.get(i);


  if(i%2==0)


  { color="#F5FAF3"; }


  else { color="#DBF7ED";   }


%>


           <tr  bgcolor = "<%=color%>">


             <td width="45%" height="20">


             <img src="./images/dot.gif" align = "absmiddle">


<a href="#"  onClick="open_window(<%=myNews.getContent()%>)"> <%=myNews.getTitle()%></a>


             </td>


                     <td width="15%" height="20" align="center">


<%=myNews.getType()%>


          &nbs;  </td>


             <td width="15%" height="20" align="center">


<%=myNews.getMan_add()%>


             </td>


             <td width="25%" height="20" align="center">


<%=myNews.getTime()%>


             </td>


          </tr>


<% } } else{ out.println("对不起,没有搜索到您要查找的新闻");}  //和最前边的else对应,判断是否有记录 %>               


  <tr bgcolor="#B7D79F">


    <td colspan="4" height="2"></td>


  </tr>


             <tr>


             <td colspan=4>


<p align=right>


&nbsp;&nbsp;


             </td>


             </tr>


       </table>


<P align=center> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;共搜索到新闻 <%=j%>  


</body>


</html>


  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用WebWork、JSP、Velocity建立注册页面
· 下一篇:JAVA/JSP学习系列之十二(JSP生成jpeg图片用于投票)
· JAVA/JSP学习系列之一
· 在WIN2000下的jsp的安装
· 编写Enterprise bean的客户端(上)
· html与jsp开发分离技术
· 使用lomboz调试JSP


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077