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="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="./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>
</td>
</tr>
</table>
<P align=center> 共搜索到新闻 <%=j%> 条
</body>
</html> | |