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

所属分类: 网络编程 / JSP编程 阅读数: 1995
收藏 0 赞 0 分享
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);
}
}
更多精彩内容其他人还在看

jsp中使用frameset框架 边框固定不让更改边框的大小

有时候可能要对自己布局好的页面不让用户更改边框的大小,这样我们可以在frame里面添加noresize="noresize"属性就可以实现其中的功能
收藏 0 赞 0 分享

response.getWriter().write()向前台打印信息乱码问题解决

本节主要介绍了response.getWriter().write()向前台打印信息乱码问题解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp页面中如何将时间戳字符串格式化为时间标签

本节主要介绍了jsp页面中如何将时间戳字符串格式化为时间标签,需要的朋友可以参考下
收藏 0 赞 0 分享

获取上一页面的URL和本页的URL的方法

本节主要介绍了获取上一页面的URL和本页的URL的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

window.top[_CACHE]实现多个jsp页面共享一个js对象

两个js页面要共享一个就js对象,想了半天用window.top['_CACHE']来存放这个变量,即可实现,不同Jsp页面直接的对象共享
收藏 0 赞 0 分享

通过过滤器(Filter)解决JSP的Post和Request中文乱码问题

这篇文章主要介绍了jsp中通过过滤器(Filter)解决JSP的Post和Request中文乱码问题的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP页面的动态包含和静态包含示例及介绍

这篇文章主要介绍了JSP页面的动态包含和静态包含示例及介绍,本文讲解了它们的区别并给出了相应例子,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP中实现判断客户端手机类型并跳转到app下载页面

这篇文章主要介绍了JSP中实现判断客户端手机类型并跳转到app下载页面,实现的原理,是检测浏览器的 USER-AGENT 这个header,然后根据正则表达式来确定客户端类型,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp实现点击help打开chm文件

有个javaweb项目,需要在portal上面点击help即可打开“帮助.chm”文件,下面与大家分享下jsp如何打开chm文件
收藏 0 赞 0 分享

JSP自定义分页标签TAG全过程

这篇文章主要介绍了JSP自定义分页标签TAG全过程,比较实用,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多