时间: 2021-07-31 作者:daque
全文字笔迹检验索从来都是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"> </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">
<br>
<font color="red" size="+1">证明:即使有多个查问前提,中央用</font><font size="+2">+</font><font color="red" size="+1">隔绝。如:1+2+3+4...</font></td>
</tr>
[page_break] </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去掉过剩的字符,消息是本人天生的文献,不妨尽管多的简略过剩字符
[page_break] 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=utf8" %>
<%@ 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";
[page_break]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="621" border="0">
<tr>
<td colspan=5>
</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="624" 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="http://edu.chinaz.com/get/program/jsp/images/dot.gif" align = "absmiddle">
<a href="#" onclick="open_window(<%=mynews.getcontent()%>)"> <%=mynews.gettitle()%></a>
[page_break]</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>
</td>
</tr>
</table>
<p align=center> 共探求到消息 <%=j%> 条
</body>
</html>