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

JSP
JSP连接各类数据库大全(上)
JSP连接各类数据库大全(下)
JSP在win2000下的安装
JSP及语法概要
JSP中文问题解决方案
JSP连接SQL SERVER问题总结
在windows环境下安装tomcat
在NT上安装Apache+Servlet+jsp
jsp与ejb通信
关于JSP写文件的补充
win2k在apache1.3上配置tomcat3.1
在Linux上安装Tomcat
JSP数据库操作例程 - 数据分页显示 - JDBC 2.0 - Oracle
JSP数据库操作例程 - 存储过程 - JDBC-ODBC - SQL Server - 1.1版本
JSP实现在浏览器关闭cookies情况下的会话管理
jsp源码实例2(获取表单参数)
jsp源码实例1(输出)
JSP文件操作例程 - 读文件
在JSP中写text文件
在Linux上架设支持JSP+PHP的Web服务器

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


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