index.html如下:
<html>
<head>
<title>订书</title>
</head>
<body>
<form action = "/servlet/GetUser" method = "post">
用户名称:<input type = "text" name = "id" size = "8"><br>
用户密码:<input type = "password" name = "passwd" size = "8">
<input type = "submit" value = "提交">
</form>
</body>
</html>
GetUser如下:
public class GetUser extends HttpServlet {Hashtable hastTable = new Hashtable();public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
res.setContentType("text/html;charset=GB2312");
ServletOutputStream sos = res.getOutputStream();
PrintWriter pw = new PrintWriter(sos,true);
String host = req.getRemoteHost();
String ip = req.getRemoteAddr();
String userId = req.getParameter("id");
String userPasswd = req.getParameter("passwd");
pw.println("您的主机是:" + host + "<br>");
pw.println("您的地址是:" + ip +"<br>");
if(userId == null)
pw.println("欢迎光临!<br>");
else{
pw.print("欢迎您," + "<br>");
sos.print(userId + "!<br>");
Date lastAccess = (Date) hastTable.get(userId);
if(lastAccess == null)
pw.println("这是您第一次光临本站!<br>");
else 
pw.println("您上次光临本站时间是:<br>" + hastTable.get(userId) + "<br>");
hastTable.put(userId,new Date());
}
}
}第一次运行的结果如下:
您的主机是:127.0.0.1
您的地址是:127.0.0.1
aa!
欢迎您,
这是您第一次光临本站!问:
  为什么先打出“aa!”,然后再打出“欢迎您,”?