最近在看《how tomcat works》,在第一章模拟服务器时有一个错误,不知道如何解决。
当我在浏览器中输入localhost:8080/yes时,无论是firefox还是chrome,都会发送多次请求,下面是我服务器打印出得请求信息
[code="java"]
start
accepted
GET /yes HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
start
accepted
GET /favicon.ico HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
start
accepted
GET /favicon.ico HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
start
accepted
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.stillness.character1.Request.parseURI(Request.java:97)
at com.stillness.character1.Request.getURI(Request.java:105)
at com.stillness.character1.HttpServer.await(HttpServer.java:55)
at com.stillness.character1.HttpServer.main(HttpServer.java:70)
[/code]
分别请求 /yes /favicon.ico /favicon.ico
其中请求/yes时,返回404错误;而请求/favicon.ico时,我的目录下没有该图标,返回的应该也是404;
第四次请求,根本不存在请求头文件什么的,似乎只是建立连接,然后不发送信息就断掉了。
错误在chrome中每次都会出,在firefox中偶尔会出。
---------------源文件就是下面的了
2, 对于favicon.ico ,我想做过网站的人不知道这个有点说不过去。 这个是你的浏览器输入网址前面的那个图标。这在chrome和firefox中是会自动获取的,ie中好像是不获取的,需要通过设置。这个你可以看下tomcat的Tomcat 6.0\webapps\ROOT下面的favicon.icon.
应该是你/yes的jsp文件里有这个ico的链接吧
[quote]at com.stillness.character1.Request.parseURI(Request.java:97) [/quote]
第四次请求 URL本身的格式就错了
发送多次请求是正常的,因为HTTP/1.1协议中一次TCP连接可以发送多次请求,而HTML中的图片标签和其他的是分多次请求来完成了,这样保证了更友好的用户体验,当一个网页图片很多的时候,其他内容可以显示出来,而图片会在后面的请求之后显示出来。
/yes这个虚拟路径应该是你要请求的URI吧,你的tomcat下没有/yes这个虚拟路径,所以报404是正常的。