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

JSP
JSP的login程序代码
编写线程安全的JSP程序
通过JSP的预编译消除性能瓶颈
JSP学习心得
使用JSP开发WebMail系统
在jsp中用bean和servlet联合实现用户注册、登录
在Linux环境下安装JSP
jsp页面调用applet实现人民币的大小写转换
JSP+XML构架网站的实例
用缓冲技术提高JSP应用的性能和稳定性
配置Web应用环境实现JSP留言簿
用JSP创建可重用的图形背景
轻松使用JSP生成饼图
Jdbc连Sybase数据库的几种方法
用定制标签库和配置文件实现对JSP页面元素的访问控制
Jsp结合XML+XSLT将输出转换为Html格式
用JSP操作Cookie
安装resin+mysql+IIS+JDK的总结
JSP的运行内幕
指南:想成为一个JSP网站程序员吗?

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 84 ::
收藏到网摘: 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);
}
}