/*
该程序模拟搜索引擎的实现过程。用本程序接受搜索引擎搜索到的结果
然后对字符串进行处理,最后在输出页面给用户。
*/
package common.engine;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.regex.*;
public class Engine extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException {
String strStartPage="";
String strName;
int m=0;
byte[] buffer=new byte[2500];
String temp="";
InputStream input=null;
String www="href='/DaLianNetWeb/engine";
strName=(String)request.getParameter("word");
strStartPage=request.getParameter("page");
// byte[] strkkk=strName.getBytes("ISO8859-1");
// byte[] strkkk=strName.getBytes("iso8859-1","gb2312");
// String strKEY=new String(strkkk);
strName=new String(strName.getBytes("iso8859-1"),"gb2312");
String strKEY;
strKEY=strName;
ServletOutputStream ow=response.getOutputStream();
OutputStreamWriter out=new OutputStreamWriter(ow,"GB2312");
out.write("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/" /"http://www.w3.org/TR/html4/loose.dtd/">");
out.write("<html><head>");
out.write("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/">");
out.write("<title>大连网搜索</title>");
out.write("<link href=/"../DaLianNetWeb/common/css/css.css/" rel=/"stylesheet/" type=/"text/css/">");
out.write("</head><body>");
out.write("<table width=/"777/" height=/"115/" border=/"0/" cellpadding=/"0/" cellspacing=/"0/" id=/"search-list/">");
out.write("<tr><td width=/"198/" height=/"71/"><img src=/"../DaLianNetWeb/common/images/logo2.gif/" width=/"198/" height=/"71/"></td>");
out.write("<td width=/"579/" valign=/"top/" class=/"top-center-1/"><table border=/"0/" cellpadding=/"0/" cellspacing=/"0/" id=/"top-nav/">");
out.write("<tr><td><a href=/"/DaLianNetWeb/index.html/">网站首页</a></td>");
out.write("<td><a href=/"/DaLianNetWeb/NetSys/zhineng.htm/">中心简介</a></td>");
out.write("<td><a href=/"/DaLianNetWeb/Special/zhuantifagui1.html/">行业指导</a></td>");
out.write("<td><a href=/"/DaLianNetWeb/Special/index.html/">专题资源</a></td>");
out.write("<td><a href=/"#/">公共数据</a></td>");
out.write("<td><a href=/"/DaLianNetWeb/search.htm/">大连导航</a></td>");
out.write("</tr></table></td></tr>");
out.write("<tr> <td height=/"44/" colspan=/"2/"><table width=/"100%/" border=/"0/" cellpadding=/"0/" cellspacing=/"0/">");
out.write("<tr><td width=/"637/" height=/"34/" class=/"nav/">");
out.write("<form name=f1 action=/DaLianNetWeb/engine>");
out.write(" <div align=/"center/">");
out.write("<input type=/"text/" name=/"word/" value="+strKEY+" >");//修改
out.write(" ");
out.write("<input type=/"submit/" name=/"Submit/" value=/"大连网搜索/"> ");//修改
out.write(" ");
out.write("<input type=/"submit/" name=/"Submit2/" value=/"在结果中搜索/"> ");
out.write("</div>");
out.write("</form>");
out.write("</td> </tr></table></td></tr></table>");
out.write("<table width=/"922/" border=/"0/" cellpadding=/"0/" cellspacing=/"0/" class=/"center-table/" id=/"search-list/">");
out.write("<tr><td height=/"315/">");
try{
URL hp = new URL("http://www.baidu.com/baidu?word=" + strKEY +
"&rn=10&pn=" + strStartPage + "");
URLConnection hpCon = hp.openConnection();
input = hpCon.getInputStream();
StringBuffer strTemp = new StringBuffer();
while ( (m = input.read(buffer, 0, 2500)) != -1) {
strTemp.append(new String(buffer, 0, m));
}
strTemp.delete(0, strTemp.indexOf("<p class=p2>"));
strTemp.delete(strTemp.lastIndexOf("<br clear=all>"),
strTemp.lastIndexOf("</table>"));
temp = new String(strTemp);
Pattern pat1 = Pattern.compile("<font color=/"#666666/">.*?上的更多结果");
Matcher mc1 = pat1.matcher(temp);
temp = mc1.replaceAll("");
Pattern pat2 = Pattern.compile("<font color=#008000>.*?百度快照");
Matcher mc2 = pat2.matcher(temp);
temp = mc2.replaceAll("");
Pattern pat3 = Pattern.compile("color=#008000>.*?推广");
Matcher mc3 = pat3.matcher(temp);
temp = mc3.replaceAll("");
out.write("<table width='95%'><tr><td>" + temp + "</td></tr></table>");
out.write(" <div align=/"center/">");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=0'>[1]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=10'>[2]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=20'>[3]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=30'>[4]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=40'>[5]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=50'>[6]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=60'>[7]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=70'>[8]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=80'>[9]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=90'>[10]</A> ");
out.write("<A " + www + "?&word=" + strKEY +
"&rn=10&page=100'>下一页</A> ");
out.write("</div>");
out.write("</td></tr></table>");
out.write(
"<table border=/"0/" cellpadding=/"0/" cellspacing=/"0/" id=/"bottom-table/">");
out.write(
"<tr><td height=/"34/"><p><strong>大连市信息资源管理中心版权所有</strong></p></td>");
out.write("</tr>");
out.write("<tr>");
out.write("<td>--------- <a href=/"/DaLianNetWeb/NetSys/friend.html/">合作伙伴 </a>"+
"<a href=/"/DaLianNetWeb/NetSys/copyright.html/">版权声明 </a>"+
"<a href=/"/DaLianNetWeb/NetSys/wangzhan.html/">网站简介 </a>"+
"<a href=/"/DaLianNetWeb/NetSys/law.html/">法律声明 </a>"+
"<a href=/"/DaLianNetWeb/NetSys/zhineng.htm/">用户指南 </a> "+
"---------<span class=/"style5/"></span></td>");
out.write(" </tr>");
out.write("</table>");
out.write("</body>");
out.write("</html>");
input.close();
}catch(Exception e){
out.write("没有查询结果!");
}
out.close();
ow.close();
}
}