威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
JSP教程>> JSP基础教程 JSP高级教程 JSP常见问题
当前位置:首页 > JSP教程 > JSP常见问题
上一节 下一节
 jsp源码实例4(搜索引擎)
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);
}
}

申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!
上一节 下一节
相关教程  
其他教程  
JSP基础教程
JSP高级教程
JSP常见问题

违法和不良信息举报中心】邮箱:254677821@qq.com
Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821