WEB服务器Socket实现
public static void main(String[] args) {
ServerSocket socket = null;
try {
socket = new ServerSocket(80);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
while (true) {
InputStream is = null;
Socket s = null;
try {
s = socket.accept();
is = s.getInputStream();
int c = -1;
while ((c = is.read()) != -1) {
System.out.print((char) c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
is = null;
}
if (s != null) {
s.close();
s = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
[list]
GET方式提交
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> New Document </title>
</head>
<body>
<form method="get" action="http://localhost/web/index.jsp">
<input type="text" name="name" value="me"/>
<input type="password" name="password" value="mypwd"/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
请求信息如下:
GET /web/index.jsp?name=me&password=mypwd&submit=submit HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MS-RTC LM 8; InfoPath.1; .NET CLR 2.0.50727; CIBA)
Host: localhost
Connection: Keep-Alive
POST方式提交
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> New Document </title>
</head>
<body>
<form method="post" action="http://localhost/web/index.jsp">
<input type="text" name="name" value="me"/>
<input type="password" name="password" value="mypwd"/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
请求信息如下:
POST /web/index.jsp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MS-RTC LM 8; InfoPath.1; .NET CLR 2.0.50727; CIBA)
Host: localhost
Content-Length: 36
Connection: Keep-Alive
Cache-Control: no-cache
name=me&password=mypwd&submit=submit
POST上传文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> New Document </title>
</head>
<body>
<form method="post" action="http://localhost/web/index.jsp" enctype="multipart/form-data">
<input type="text" name="name" value="me"/>
<input type="password" name="password" value="mypwd"/>
<input type="file" name="uploadfile"/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
请求信息如下:
POST /web/index.jsp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7dad71450b9e
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MS-RTC LM 8; InfoPath.1; .NET CLR 2.0.50727; CIBA)
Host: localhost
Content-Length: 954
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------7dad71450b9e
Content-Disposition: form-data; name="name"
me
-----------------------------7dad71450b9e
Content-Disposition: form-data; name="password"
mypwd
-----------------------------7dad71450b9e
Content-Disposition: form-data; name="uploadfile"; filename="D:\temp\zhizhu\news.sql"
Content-Type: application/octet-stream
CREATE DATABASE IF NOT EXISTS sohunews;
USE sohunews;
--
-- Definition of table `news`
--
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
`newsid` int(11) NOT NULL auto_increment,
`newstitle` varchar(60) NOT NULL,
`newsauthor` varchar(20) NOT NULL,
`newscontent` text NOT NULL,
`newsurl` char(130) NOT NULL,
`newsdate` varchar(24) NOT NULL,
PRIMARY KEY (`newsid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-----------------------------7dad71450b9e
Content-Disposition: form-data; name="submit"
submit
-----------------------------7dad71450b9e--
[/list]