问题描述
当输入一个url时会发送好多request,比如输入http://www.baidu.com/会有许多request-response对如下GET/HTTP/1.1Host:www.baidu.comUser-Agent:Mozilla/5.0(Windows;U;WindowsNT5.1;zh-CN;rv:1.9.2.13)Gecko/20101203Firefox/3.6.13Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language:zh-cn,zh;q=0.5Accept-Encoding:gzip,deflateAccept-Charset:GB2312,utf-8;q=0.7,*;q=0.7Keep-Alive:115Proxy-Connection:keep-aliveCookie:BAIDUID=26AA7D2A981F60F6493F8AA858D31137:FG=1HTTP/1.0200OKDate:Tue,25Jan201102:00:49GMTContent-Length:2973Content-Type:text/html;charset=gb2312Cache-Control:privateExpires:Tue,25Jan201102:00:49GMTContent-Encoding:gzipConnection:keep-aliveProxy-Connection:keep-alive----------------------------------------------------------http://www.baidu.com/js/bdsug.js?v=1.0.3.0GET/js/bdsug.js?v=1.0.3.0HTTP/1.1Host:www.baidu.comUser-Agent:Mozilla/5.0(Windows;U;WindowsNT5.1;zh-CN;rv:1.9.2.13)Gecko/20101203Firefox/3.6.13Accept:*/*Accept-Language:zh-cn,zh;q=0.5Accept-Encoding:gzip,deflateAccept-Charset:GB2312,utf-8;q=0.7,*;q=0.7Keep-Alive:115Proxy-Connection:keep-aliveReferer:http://www.baidu.com/Cookie:BAIDUID=26AA7D2A981F60F6493F8AA858D31137:FG=1If-Modified-Since:Fri,21Jan201112:50:40GMTIf-None-Match:"2655-49a5ab28a8800"HTTP/1.0304NotModifiedDate:Tue,25Jan201102:00:49GMTEtag:"2655-49a5ab28a8800"Vary:Accept-Encoding,User-AgentConnection:keep-aliveProxy-Connection:keep-alive----------------------------------------------------------http://www.baidu.com/s?wd=GET/s?wd=HTTP/1.1Host:www.baidu.comUser-Agent:Mozilla/5.0(Windows;U;WindowsNT5.1;zh-CN;rv:1.9.2.13)Gecko/20101203Firefox/3.6.13Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language:zh-cn,zh;q=0.5Accept-Encoding:gzip,deflateAccept-Charset:GB2312,utf-8;q=0.7,*;q=0.7Keep-Alive:115Proxy-Connection:keep-aliveReferer:http://www.baidu.com/Cookie:BAIDUID=26AA7D2A981F60F6493F8AA858D31137:FG=1HTTP/1.0200OKDate:Tue,25Jan201102:00:51GMTContent-Length:94Content-Type:text/html;charset=gbkCache-Control:privateConnection:keep-aliveProxy-Connection:keep-alive----------------------------------------------------------http://www.baidu.com/GET/HTTP/1.1Host:www.baidu.comUser-Agent:Mozilla/5.0(Windows;U;WindowsNT5.1;zh-CN;rv:1.9.2.13)Gecko/20101203Firefox/3.6.13Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language:zh-cn,zh;q=0.5Accept-Encoding:gzip,deflateAccept-Charset:GB2312,utf-8;q=0.7,*;q=0.7Keep-Alive:115Proxy-Connection:keep-aliveCookie:BAIDUID=26AA7D2A981F60F6493F8AA858D31137:FG=1HTTP/1.0200OKDate:Tue,25Jan201102:00:51GMTContent-Length:2973Content-Type:text/html;charset=gb2312Cache-Control:privateExpires:Tue,25Jan201102:00:51GMTContent-Encoding:gzipConnection:keep-aliveProxy-Connection:keep-alive----------------------------------------------------------http://www.baidu.com/js/bdsug.js?v=1.0.3.0GET/js/bdsug.js?v=1.0.3.0HTTP/1.1Host:www.baidu.comUser-Agent:Mozilla/5.0(Windows;U;WindowsNT5.1;zh-CN;rv:1.9.2.13)Gecko/20101203Firefox/3.6.13Accept:*/*Accept-Language:zh-cn,zh;q=0.5Accept-Encoding:gzip,deflateAccept-Charset:GB2312,utf-8;q=0.7,*;q=0.7Keep-Alive:115Proxy-Connection:keep-aliveReferer:http://www.baidu.com/Cookie:BAIDUID=26AA7D2A981F60F6493F8AA858D31137:FG=1If-Modified-Since:Fri,21Jan201112:50:40GMTIf-None-Match:"2655-49a5ab28a8800"HTTP/1.0304NotModifiedDate:Tue,25Jan201102:00:51GMTEtag:"2655-49a5ab28a8800"Vary:Accept-Encoding,User-AgentConnection:keep-aliveProxy-Connection:keep-alive如何写代码输入这些request-response?
解决方案
解决方案二:
网上到处都是,下面代码:publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Mapmap=newHashMap();EnumerationparamNames=request.getParameterNames();while(paramNames.hasMoreElements()){StringparamName=(String)paramNames.nextElement();String[]paramValues=request.getParameterValues(paramName);if(paramValues.length==1){StringparamValue=paramValues[0];if(paramValue.length()!=0){System.out.println("参数:"+paramName+"="+paramValue);map.put(paramName,paramValue);}}}}
解决方案三:
由于本人水平很菜,能不能给个例子呢?这个函数我不知道怎么调用……
解决方案四:
事实证明你不会JAVA,这是标准servlet的函数,使用eclipse新建servlet就可以,类似如下:ComputerFacor.java:importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassComputerFactorextendsHttpServlet{publicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//获得一个向客户发送数据的输出流:PrintWriterout=response.getWriter();response.setContentType("text/html;charset=GB2312");//设置响应的MIME类型。out.println("<HTML>");out.println("<BODY>");Stringnumber=request.getParameter("number");//获取客户提交的信息。doublen=0;try{n=Double.parseDouble(number);out.println("<H1>factorsof"+n+":</H1>");//求n的全部因数:for(inti=1;i<=n;i++){if(n%i==0)out.println(i);}}catch(NumberFormatExceptione){out.print("<H1>inputnumberletterplease!</H1>");}}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Mapmap=newHashMap();EnumerationparamNames=request.getParameterNames();while(paramNames.hasMoreElements()){StringparamName=(String)paramNames.nextElement();String[]paramValues=request.getParameterValues(paramName);if(paramValues.length==1){StringparamValue=paramValues[0];if(paramValue.length()!=0){System.out.println("参数:"+paramName+"="+paramValue);map.put(paramName,paramValue);}}}}}
解决方案五:
谢谢,虽然我对servlet不熟,我自己弄弄!