当前位置: 首页 > 图文教程 > 网络编程 > JSP > jsp源码实例4(搜索引擎)

JSP
Apache+Servlet+Jsp环境设置(中)
Apache+Servlet+Jsp环境设置(下)
基于EJB技术的商务预订系统的开发
让JSP页面过期, 保证每次JSP页面都是最新的.
JSP发送邮件实例
也谈用JSP实现新郎、sohu新闻系统的技术。
JSP抓取网页代码的程序
JSP应用的安全问题
将TOMCAT装入IIS全攻略
通过JDBC连接oracle数据库的十大技巧
JSP安全性初探
JSP漏洞大观
关于JSP中文问题的解决方法
JSP/JAVABEAN+TOMCAT4.0.5+MYSQL组合建站总结
WIN2000+PHP+MYSQL+TOMCAT+JSP完全整合安装手册
jsp连接数据库大全
用JSP下载word文件(不会直接用IE打开)
几则JSP入门知识总结
JSP迅速入门
Jsp如何实现网页的重定向

JSP 中的 jsp源码实例4(搜索引擎)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 85 ::
收藏到网摘: n/a

package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;

public class SearchEngines extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String searchString = request.getParameter("searchString");
if ((searchString == null) //
(searchString.length() == 0)) {
reportProblem(response, "Missing search string.");
return;
}
// The URLEncoder changes spaces to "+" signs and other
// non-alphanumeric characters to "%XY", where XY is the
// hex value of the ASCII (or ISO Latin-1) character.
// Browsers always URL-encode form values, so the
// getParameter method decodes automatically. But since
// we're just passing this on to another server, we need to
// re-encode it.
searchString = URLEncoder.encode(searchString);
String numResults = request.getParameter("numResults");
if ((numResults == null) //
(numResults.equals("0")) //
(numResults.length() == 0)) {
numResults = "10";
}
String searchEngine =
request.getParameter("searchEngine");
if (searchEngine == null) {
reportProblem(response, "Missing search engine name.");
return;
}
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
for(int i=0; i<commonSpecs.length; i++) {
SearchSpec searchSpec = commonSpecs;
if (searchSpec.getName().equals(searchEngine)) {
String url =
searchSpec.makeURL(searchString, numResults);
response.sendRedirect(url);
return;
}
}
reportProblem(response, "Unrecognized search engine.");
}
private void reportProblem(HttpServletResponse response,
String message)
throws IOException {
response.sendError(response.SC_NOT_FOUND,
"<H2>" + message + "</H2>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}