import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class Results
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String QC = request.getParameter("QueryContent");
if (QC == null) {
QC = "";
}
else {
QC = input(QC);
}
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try {
Search(QC, out);
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void Search(String qc, PrintWriter out) throws Exception {
// 从索引目录创建索引
IndexSearcher _searcher = new IndexSearcher("c:\\news\\index");
// 创建标准分析器
Analyzer analyzer = new ChineseAnalyzer();
// 查询条件
String line = qc;
// Query是一个抽象类
Query query = QueryParser.parse(line, "title", analyzer);
out.println("<html>");
out.println("<head><title>搜索结果</title></head>");
out.println("<body bgcolor=#ffffff>");
out.println("<center>" +
"<form action='results' method='get'>" +
"<font face='华文中宋' color='#3399FF'>新闻搜索引擎</font>:" +
"<input type='text' name='QueryContent' size='20'>" +
"<input type='submit' name='submit' value='开始搜索'>" +
"</form></center>"
);
out.println("<p>搜索关键字:<font color=red>" + query.toString("title") +
"</font></p>");
Hits hits = _searcher.search(query);
out.println(" 总共找到<font color=red>" + hits.length() + "</font>条新闻<br>");
final int HITS_PER_PAGE = 10;
for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
int end = Math.min(hits.length(), start + HITS_PER_PAGE);
for (int i = start; i < end; i++) {
Document doc = hits.doc(i);
String url = doc.get("url");
if (url != null) {
out.println( (i + 1) + " <a href='" + url + "'>" +
replace(doc.get("title"), qc) +
"</a><br>");
}
else {
System.out.println("没有找到!");
}
}
}
out.println("</body></html>");
_searcher.close();
};
public String input(String str) {
String temp = null;
if (str != null) {
try {
temp = new String(str.getBytes("ISO8859_1"));
}
catch (Exception e) {
}
}
return temp;
}
public String replace(String title, String keyword) {
return title.replaceAll(keyword, "<font color='red'>" + keyword + "</font>");
};
//Clean up resources
public void destroy() {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java Web典型模块与项目实战大全源代码07-09 (105个子文件)
Results.class 4KB
GetShoppingCartServlet.class 4KB
AddShoppingCartServlet.class 3KB
LuceneSearchTest.class 3KB
LuceneIndexTest.class 3KB
SetInfornationServlet.class 3KB
HTMLParse.class 2KB
GetInfornationServlet.class 2KB
TestIndex.class 2KB
ShoppingCart.class 2KB
CartItem.class 1KB
CountFileHandler.class 1KB
Index.class 1KB
StringTool.class 1KB
StringTool.class 1KB
CounterListener.class 807B
Constants.class 385B
.classpath 433B
.classpath 362B
.classpath 362B
.classpath 340B
.classpath 340B
.classpath 306B
.classpath 303B
org.eclipse.wst.jsdt.ui.superType.container 49B
8.gif 1KB
0.gif 1KB
6.gif 1KB
4.gif 1KB
7.gif 1KB
1.gif 1KB
5.gif 1KB
3.gif 1KB
9.gif 1KB
2.gif 1KB
buybutton.gif 946B
showMerchandise.htm 3KB
cart.htm 888B
InfornationInput.htm 595B
cookieInput.htm 500B
InfornationInput.html 583B
seach.html 430B
lucene_cn.jar 136KB
lucene-1.2.jar 133KB
bot.jar 37KB
Results.java 3KB
GetShoppingCartServlet.java 2KB
AddShoppingCartServlet.java 2KB
LuceneSearchTest.java 2KB
SetInfornationServlet.java 2KB
LuceneIndexTest.java 2KB
ShoppingCart.java 2KB
GetInfornationServlet.java 1KB
StringTool.java 1KB
StringTool.java 1KB
HTMLParse.java 1KB
TestIndex.java 1KB
CartItem.java 992B
CountFileHandler.java 847B
Index.java 773B
CounterListener.java 456B
Constants.java 153B
game.jpg 7KB
mouse.jpg 7KB
lenovo.jpg 7KB
lcd.jpg 7KB
hppavilion.jpg 7KB
learning.jpg 7KB
.jsdtscope 500B
count2.jsp 849B
count.jsp 775B
showpeople.jsp 271B
MANIFEST.MF 39B
MANIFEST.MF 39B
MANIFEST.MF 39B
MANIFEST.MF 39B
MANIFEST.MF 36B
.mymetadata 328B
.mymetadata 319B
.mymetadata 313B
.mymetadata 310B
.mymetadata 303B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 21KB
org.eclipse.wst.validation.prefs 1KB
com.genuitec.eclipse.j2eedt.core.prefs 1KB
com.genuitec.eclipse.j2eedt.core.prefs 1KB
com.genuitec.eclipse.j2eedt.core.prefs 1KB
com.genuitec.eclipse.j2eedt.core.prefs 1KB
org.eclipse.jdt.ui.prefs 237B
com.genuitec.eclipse.core.prefs 171B
com.genuitec.eclipse.core.prefs 122B
org.eclipse.core.resources.prefs 88B
.project 1KB
.project 1KB
.project 1KB
.project 1KB
.project 1KB
.project 390B
.project 386B
共 105 条
- 1
- 2
资源评论
- qq_267321112015-03-20骗了我很多的积分,不良程序员!
Freequal
- 粉丝: 3
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功