Z
zyycc
Unregistered / Unconfirmed
GUEST, unregistred user!
刚开始学java/servlet,在书上看到一个例子,不太明白,代码如下:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class search extends HttpServlet{
public voiddo
Post(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ String searchString = URLEncoder.encode(request.getParameter("searchstring"));
String numResults = request.getParameter("numresults");
String searchEngine = request.getParameter("searchengine");
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
//3.谁能从编译的角度说说如何执行的?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (int i=0;i<commonSpecs.length;i++)
{
SearchSpec searchSpec = commonSpecs;
if (searchSpec.getName().equals(searchEngine))
{ String url = response.encodeURL(searchSpec.makeURL(searchString,numResults));
response.sendRedirect(url);
return;
}
}
response.sendError(response.SC_NOT_FOUND,"No recongnized search engine spedified.");
}
}
class SearchSpec //1. 开始定义类
~~~~~~~~~~~~~~~~
{
private String name, baseURL, numResultsSuffix;
private static SearchSpec[] commonSpecs = //2. 类定义中又声明一个本类的数组,这样可行吗
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
new SearchSpec("google","http://www.google.com/search?q=","&num="),
new SearchSpec("infoseek","http://www.infoseek.go.com/Titles?qt=","&nh="),
new SearchSpec("lycos","http://lycospro.lycos.com/cgi-bin/pursuit?query=","&maxhits="),
new SearchSpec("hotbot","http://www.hotbot.com/?MT","&DC=")
};
public SearchSpec(String name, String baseURL,String numResultsSuffix)
{
this.name=name;
this.baseURL=baseURL;
this.numResultsSuffix=numResultsSuffix;
}
public String makeURL(String searchString,String numResults)
{ return(baseURL+searchString+numResultsSuffix+numResults);}
public static SearchSpec[] getCommonSpecs() { return(commonSpecs);}
}
哪位大侠就代码中打波浪线的部分给说一哈,先谢过了!
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class search extends HttpServlet{
public voiddo
Post(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ String searchString = URLEncoder.encode(request.getParameter("searchstring"));
String numResults = request.getParameter("numresults");
String searchEngine = request.getParameter("searchengine");
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
//3.谁能从编译的角度说说如何执行的?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (int i=0;i<commonSpecs.length;i++)
{
SearchSpec searchSpec = commonSpecs;
if (searchSpec.getName().equals(searchEngine))
{ String url = response.encodeURL(searchSpec.makeURL(searchString,numResults));
response.sendRedirect(url);
return;
}
}
response.sendError(response.SC_NOT_FOUND,"No recongnized search engine spedified.");
}
}
class SearchSpec //1. 开始定义类
~~~~~~~~~~~~~~~~
{
private String name, baseURL, numResultsSuffix;
private static SearchSpec[] commonSpecs = //2. 类定义中又声明一个本类的数组,这样可行吗
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
new SearchSpec("google","http://www.google.com/search?q=","&num="),
new SearchSpec("infoseek","http://www.infoseek.go.com/Titles?qt=","&nh="),
new SearchSpec("lycos","http://lycospro.lycos.com/cgi-bin/pursuit?query=","&maxhits="),
new SearchSpec("hotbot","http://www.hotbot.com/?MT","&DC=")
};
public SearchSpec(String name, String baseURL,String numResultsSuffix)
{
this.name=name;
this.baseURL=baseURL;
this.numResultsSuffix=numResultsSuffix;
}
public String makeURL(String searchString,String numResults)
{ return(baseURL+searchString+numResultsSuffix+numResults);}
public static SearchSpec[] getCommonSpecs() { return(commonSpecs);}
}
哪位大侠就代码中打波浪线的部分给说一哈,先谢过了!