程序运行结果:
HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1026
Date: Mon, 28 Mar 2016 02:45:16 GMT
Connection: close
目前位置还没有找到解决方法,我的html文件确实存在于这个路径下,但就是找不到,
1 packageWebProgramingDemo;2
3 importjava.io.InputStream;4 importjava.io.PrintStream;5 importjava.net.Socket;6 importjava.net.UnknownHostException;7
8 public classMyBrowser {9
10 /**
11 *@paramargs12 *@throwsException13 *@throwsUnknownHostException14 */
15 //模拟一个浏览器来获取服务器信息
16 public static void main(String[] args) throwsException {17 Socket s = new Socket("192.168.2.103", 8080);18 PrintStream out = newPrintStream(s.getOutputStream());19 out.println("GET /myweb/index.html HTTP/1.1");20 out.println("Accept: */*");21 out.println("Host: 192.168.2.103:8888");22 out.println("Connection: close");23 out.println();24 out.println();25 InputStream in =s.getInputStream();26 byte[] buf = new byte[1024];27 int len =in.read(buf);28 System.out.println(new String(buf, 0, len));29 }30
31 }
问题已经解决:我把out.println("GET /myweb/index.html HTTP/1.1");这一行改成了out.println("GET /index.jsp HTTP/1.1");
index.jsp是我从webapps目录下拷贝过来的文件,去除了/myweb,Get请求默认的就是/webapps/index.jsp
运行结果:
忘了贴运行结果了:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 28 Mar 2016 02:59:13 GMT
Connection: close