【发布时间】:2014-11-30 03:57:48
【问题描述】:
我正在尝试使用 servlet 创建一个注册页面。我创建了一个基本的 HTML 页面,其中包含一个输入用户名和密码的表单。现在我需要做的是使用 cookie/会话存储提交到表单的信息。然后在登录页面上,用户必须能够使用他们之前提供的信息登录。 所以基本上我需要知道如何存储用户名和密码。
因此,如果我使用用户名:admin 和密码 123 注册,然后使用用户名:user 和密码:12345 进行注册,我应该无法使用 admin 和 12345 或 user 和 123 登录。谢谢!
HTML 表单
<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。这个网站是针对具体的问题和具体的答案。您给了我们一个相当高级别的要求和一堵代码墙,这与本网站无关。你应该阅读一些教程,试一试,如果你有一个特定的问题,你可以在这里提问。一个警告:任何与安全有关的事情都是棘手的,做错的后果是相当严重的,所以你最好使用一个为你处理它的框架。
标签: java forms session servlets cookies