“一个搜索引擎”
首先获取表单:
------------------------------
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
//QC取值
    String QC = request.getParameter("QueryContent");
    if (QC == null) {
      QC = "";
    }
    else {
      QC = input(QC);
    }
    String TYPE = request.getParameter("RadioGroup1");
//发送到客户端的相应的内容的类型
    response.setContentType(CONTENT_TYPE);
//当我们调用response.getWriter()这个对象同时获得了网页的画笔,这时你就可以通过这个画笔在网页上画任何你想要显示的东西。
    PrintWriter out = response.getWriter();
    
    try {
      Search(QC, out,TYPE);
    }
    catch (Exception ex) {
  //getMessage : 从调用线程的消息队列里取得一个消息并将其放于指定的结构
      System.out.println(ex.getMessage());
    }
  }
------------------------------------------
下面是处理:
------------------------------------------
public void Search(String qc, PrintWriter out,String type) throws Exception {
String file_address = "d:\\mysource\\news\\index\\sports";
    // 初始化IndexSearcher设置索引存放的路径,让查询器能定位索引而进行搜索
    IndexSearcher _searcher = new IndexSearcher(file_address);    // 创建Analyzer分析器,主要用于文本分词
    Analyzer analyzer = new ChineseAnalyzer();
    // 查询条件
    String line = qc;
    // Query是一个抽象类   QueryParser:解析查询表达式
    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='#000000'>新闻搜索引擎</font>:" +
                "<input type='text' name='QueryContent' size='20'>" +
                "<input type='submit' name='submit' value='开始搜索'>" +
                "</form></center>"
                );
    out.println("type"+"\"d:\\mysource\\news\\index\\"+type+"\"");//为了验证是否获取表单成功而加入这行    out.println("<p>搜索关键字:<font color=red>" + query.toString("title") +
                "</font></p>");
//Hits:在搜索完成之后,需要把搜索结果返回并显示给用户,只有这样才算是完成搜索的目的。
//在lucene中,搜索的结果的集合是用Hits类的实例来表示的。
    Hits hits = _searcher.search(query);
    out.println(" 总共找到<font color=red>" + hits.length() + "</font>条新闻<br>");    final int HITS_PER_PAGE = 10;
//length:返回搜索结果的总数
    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++) {
    //返回第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) {
    //replaceAll:替代
    return title.replaceAll(keyword, "<font color='red'>" + keyword + "</font>");
  };  //清理资源
  public void destroy() {
  }
}
-------------------------------------------------------
这种情况下可以正常运行。
可当我把file_address的地址用(红色部分)改为一个判断
因为我的索引数据在三个文件夹下,想要搜索不同类型的新闻就要去不同的文件夹,现在就是想通过html中的单选按钮组来实现String file_address = null;

if(type == "sports"){
file_address = "d:\\mysource\\news\\index\\sports";
}
else if(type =="ent"){
file_address = "d:\\mysource\\news\\index\\ent";
}
else if(type =="finance"){
file_address = "d:\\mysource\\news\\index\\finance";
}可是在这种情况下,点击html页面中的“搜索”按钮,页面就变成一片空白,什么都没有了
难道是不能对type做判断?还是qc&type只能同时运行一个?程序是我更改别人的,自己也是刚开始学习,请各位高手指点。

解决方案 »

  1.   

    String file_address = null; if(type == "sports"){ 
    file_address = "d:\\mysource\\news\\index\\sports"; 

    else if(type =="ent"){ 
    file_address = "d:\\mysource\\news\\index\\ent"; 

    else if(type =="finance"){ 
    file_address = "d:\\mysource\\news\\index\\finance"; 

    程序太多,看不过来,挑最后一段代码提几个问题:
    1、字符串判断,最好用equals方法
    2、字符串判断,最好是常量在前:"finance".equals(type)
    3、如果type不等于其中的任何一个,这段代码的后面肯定会抛NPE
      

  2.   

    我按照你说的方法试了,还是不行呀,如果我不用上面的判断,直接给定地址:String file_address = "d:\\mysource\\news\\index\\sports";   程序就可以运行,非常不解??