1. 程式人生 > >基於Java手寫web伺服器(簡易版)

基於Java手寫web伺服器(簡易版)

<div class="main">
	<h2>登入</h2>
	<form id="myform">
		<table>
			<tr>
				<td>使用者名稱:</td><td><input type="text" name="user" id="user"></td>
			</tr>
			<tr>
				<td>密  碼:</td><td><input type="password" name="password" id="pwd"></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="提交"> <a href="register.html">還沒帳號?現在註冊</a>
				</td>
			</tr>
		</table>
	</form>
</div>
	<script type="text/javascript">
		var form = document.getElementById('myform');
		form.onsubmit = function (e) {
			var user = document.getElementById('user').value;
			var pwd = document.getElementById('pwd').value;
			var data = "user="+user+"&password="+pwd;
			dosome(data, function (rel){
				alert(rel);
			});
			e.preventDefault();
		}
		function dosome (param, callback) {
			var xhr = new XMLHttpRequest();
			xhr.open("POST", "http://localhost:8080/login", true);
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xhr.send(param);
			xhr.onreadystatechange = function () {
				if (xhr.readyState == 4 && xhr.status == 200) {
					callback(xhr.responseText);
				}
			}
		}
	</script>

這裡我用ajax請求模擬登入操作