Socket编程post提交为什么会连接两次?

我老公出差了 2012-11-06 01:40:05
自己写的Socket程序。然后自己访问。用post提交连接了两次。
假如我用用post提交,打印请求信息,可以看到先是post一次,然后get一次。

public class Login {

public static void main(String[] args) throws Exception {

ServerSocket ss = new ServerSocket(10000);
while(true) {
Socket s = ss.accept();
System.out.println(s.toString());//打印了两次
new Thread(new UserThread(s)).start();
}
}
}

class UserThread implements Runnable {

private Socket s;

public UserThread(Socket s) {
this.s = s;
}

public void run() {
try {
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("欢迎来访问<br />");
out.println("哈哈,怎么连接两次啊");
s.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}


表单
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册页面</title>
</head>
<body>
<!-- <div>
<form action="http://127.0.0.1:10000" method="get">
姓名<input name="name" type="text" /><br />
年龄<input name="age" type="text" /><br />
<input type="submit" value="get提交" />
</form>
</div><br /> -->
<div>
<form action="http://127.0.0.1:10000" method="post">
姓名<input name="name" type="text" /><br />
年龄<input name="age" type="text" /><br />
<input type="submit" value="post提交" />
</form>
</div>
</body>
</html>

...全文
330 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryanhuang2004 2012-11-09
  • 打赏
  • 举报
回复
可能是因为你在代码中PrintWriter pw = new PrintWriter(s.getOutputStream())这个地方. 可能是又建立了一个socket连接的关系.
我老公出差了 2012-11-06
  • 打赏
  • 举报
回复
而且我把run方法变成这样就无论是get还是post就只有一次了。
public void run() {
		try {
			s.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
}
我老公出差了 2012-11-06
  • 打赏
  • 举报
回复
引用 2 楼 AFer198215 的回复:
我怀疑先get 也是两次。
get只打印一次。
我老公出差了 2012-11-06
  • 打赏
  • 举报
回复
引用 1 楼 ryanhuang2004 的回复:
弱弱的问一句. 你试过把注释掉的代码删除掉了么?
html代码?注释和不注释一样的。
想喝咖啡的貓 2012-11-06
  • 打赏
  • 举报
回复
我怀疑先get 也是两次。
ryanhuang2004 2012-11-06
  • 打赏
  • 举报
回复
弱弱的问一句. 你试过把注释掉的代码删除掉了么?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧