?? results.java~34~
字號:
package newsserver;
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;
/**
* <p>Title: 新聞搜索引擎</p>
* <p>Description: 畢業設計</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author 計算機99630 沈晨
* @version 1.0
*/
public class Results
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String QC = request.getParameter("QueryContent");
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='/NewsServer/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 + "'>" + doc.get("title") +
"</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() {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -