下面介绍用Servlet实现一站式搜索引擎的方法,当然其实绘制网页主页的部分用JSP会更加直观就是了,大家可以自行修改,这也不难的。
--------------------------------------------------------------------------------
SearchSpec.java // 搜索引擎类
public class SearchSpec {
private String name; // 搜索引擎的名字
private String baseURL; // 搜索引擎的搜索路径
public SearchSpec(String name, String baseURL){
this.name = name;
this.baseURL = baseURL;
}
// 将搜索路径和搜索词组合成新的URL
public String makeURL(String searchString){
return this.baseURL + searchString;
}
public String getName(){
return this.name;
}
}
--------------------------------------------------------------------------------
SearchUtilities.java // 搜索引擎功能类
public class SearchUtilities {
// 虽然这个很长,但其实这是个搜索引擎的数组成员,而不是个方法
// 要扩建也很简单,只要进入你要的搜索引擎,随便搜索一下,就知道它的基本搜索路径了
private static SearchSpec[] commonSpecs = {
new SearchSpec("Baidu", "http://www.baidu.com/s?wd="),
new SearchSpec("Google", "http://www.google.com/search?q="),
new SearchSpec("Yahoo", "http://search.yahoo.com/bin/search?p="),
new SearchSpec("AllTheWeb", "http://www.alltheweb.com/search?q="),
new SearchSpec("AltaVista", "http://altavista.com/web/results?q="),
new SearchSpec("Bing", "http://cn.bing.com/search?q=")
};
public static SearchSpec[] getCommonSpecs(){
return commonSpecs;
}
// 用来判断用户选择的是哪个搜索引擎
public static String makeURL(String searchEngineName, String searchString){
SearchSpec[] searchSpecs = getCommonSpecs();
String searchURL = null;
for(int i = 0; i < searchSpecs.length; i++){
SearchSpec spec = searchSpecs[i];
if(spec.getName().equalsIgnoreCase(searchEngineName)){
searchURL = spec.makeURL(searchString);
break;
}
}
return searchURL;
}
}
--------------------------------------------------------------------------------
SearchEngine.java // 搜索引擎的Serlvet,注意创建的是Serlvet不是java类,上面两个都是java类
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SearchEngines extends HttpServlet {
// 这是netBeans的封装方法,doGet和doPost方法都会调用这个方法processRequest
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
try {
String searchString = request.getParameter("searchString");
// 判断搜索内容是否为空,或者是全是空格
if(null == searchString || 0 == searchString.trim().length()){
reportProblem(response, "Missing search string.");
return ;
}
// 不知道从NB从哪个版本开始encode就废弃了单个参数的方法了
// 现在都是两个参数的,一个是string,一个是编码方式
searchString = URLEncoder.encode(searchString, "utf-8");
String searchEngineName = request.getParameter("searchEngine");
// 下面这个错误基本是不会出现才对,除非在绘制页面内容时,自己多加了一个空引擎
if(null == searchEngineName || 0 == searchEngineName.length()){
reportProblem(response, "Missing search engine name.");
return ;
}
String searchURL = SearchUtilities.makeURL(searchEngineName, searchString);
// 下面这个错误主要会出现在字符编码的问题上,现在这个代码是不支持搜索引擎的名字使用中文的,会无法比较。
// 所以你看我在搜索引擎功能类中定制搜索引擎时,name属性都用英文。。
if(null != searchURL){
response.sendRedirect(searchURL);
}else{
reportProblem(response, "Unrecognized search engine.");
}
} finally {
out.close();
}
}
// 错误处理类,传入的是response对象和一个错误信息
private void reportProblem(HttpServletResponse response, String message)
throws IOException{
response.sendError(response.SC_NOT_FOUND, message);
}
// 下面还有一些被折叠起来的代码,NetBeans默认的,千万不要删了,主要是doGet和doPost的原始方法
--------------------------------------------------------------------------------
SearchEngineForm.java // 绘制网页内容的Serlvet,可以使用JSP的页面来代替,如果是使用Setlvet当首页,则要修改web.xml中的welcome属性
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;
/**
*
* @author Administrator
*/
public class SearchEngineForm extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
try {
String title = "一 站 式 搜 索";
String actionURL = "SearchEngines";
String DOCTYPE = "<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.0" +
"Transitional//EN/">/n";
out.println(DOCTYPE +
"<html>/n" +
"<head><title>" + title + "</title></head>/n" +
"<body bgcolor=/"#fdf5e6/">/n" +
"<center>/n" +
"<br><h1>" + title + "</h1>/n" +
"<form action=/"" + actionURL + "/">/n<br><br><br><br>" +
"<input type=/"text/" name=/"searchString/">/n");
SearchSpec[] specs = SearchUtilities.getCommonSpecs();
out.println("<select name=/"searchEngine/" id=/"searchEngine/">");
for(int i = 0 ; i < specs.length; i++){
String searchEngineName = specs[i].getName();
out.println("<option value=/"" + searchEngineName +"/">/n" +
searchEngineName + "</option>");
}
out.println("</select><input type=/"submit/">/n" +
"</form>/n" +
"</center></body></html>");
} finally {
out.close();
}
}
// 下面还有一些被折叠起来的代码,NetBeans默认的,千万不要删了,主要是doGet和doPost的原始方法
--------------------------------------------------------------------------------
好了,一站式搜索的代码差不多就这样了,值得注意的是,上面的代码方式不支持中文。。。。。。哈