我正在嘗試使用servlet創建註冊頁面。我創建了一個基本的HTML頁面,其中有一個輸入用戶名和密碼的表單。現在我需要做的是使用cookies /會話存儲提交給表單的信息。然後在登錄頁面上,用戶必須能夠使用他們之前提供的信息進行登錄。 所以基本上我需要知道如何存儲用戶名和密碼。使用cookie /會話存儲用戶名,密碼 - Java Servlets
因此,如果我註冊用戶名:admin和密碼123,然後註冊用戶名:user和password:12345,我不應該能夠用admin和12345或user和123登錄。 !
HTML FORM
<html>
<head>
<title>Registration</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body bgcolor="lightblue">
<center>
<h1></h1>
<br>
<hr>
<br><br>
<form action="/Registration" method="get">
<h3> Please register to start </h3>
Username: <input type="text" name="userName">
<br>
Password: <input type="password" name="password">
<br>
<br>
<input type="submit" value="Register">
<br><br>
</form>
</center>
</body>
</html>
Java Servlet的
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
// Create cookies for first and last names.
Cookie userName = new Cookie("userName",
request.getParameter("userName"));
Cookie password = new Cookie("password",
request.getParameter("password"));
// Set expiry date after 24 Hrs for both the cookies.
userName.setMaxAge(60*60*24);
password.setMaxAge(60*60*24);
// Add both the cookies in the response header.
response.addCookie(userName);
response.addCookie(password);
歡迎來到Stack Overflow。本網站面向具體問題並附有具體答案。你已經給了我們一個相當高的要求和一段代碼,這是這個網站的主題。你應該閱讀一些教程,嘗試一下,如果你有一個具體的問題,你可以在這裏提問。一句警告:任何與安全有關的事情都是棘手的,做錯的後果是非常嚴重的,所以你最好使用一個能夠爲你處理它的框架。 – yshavit 2014-10-05 20:38:52