我正在尝试使用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