使用 JSP 的 Web 应用程序
为什么客户端每次登录时都要重新输入用户名:
<%
String userName = request.getParameter("username");
String password = request.getParameter("password");
String rm_me = request.getParameter("rm_me");
String rm_uname = request.getParameter("rm_uname");
if (userName != null && password != null) {
if (rm_me != null) {
Cookie ckU = new Cookie("username", userName);
Cookie ckP = new Cookie("password", password);
response.addCookie(ckP);
} else {
if (rm_uname != null) {
Cookie ckU = new Cookie("username", userName);
}
}
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("username")) {
userName = cookies[i].getValue();
}
if (cookies[i].getName().equals("password")) {
password = cookies[i].getValue();
}
}
}
%>
Why is the username being retyped each time the client logs in:
<%
String userName = request.getParameter("username");
String password = request.getParameter("password");
String rm_me = request.getParameter("rm_me");
String rm_uname = request.getParameter("rm_uname");
if (userName != null && password != null) {
if (rm_me != null) {
Cookie ckU = new Cookie("username", userName);
Cookie ckP = new Cookie("password", password);
response.addCookie(ckP);
} else {
if (rm_uname != null) {
Cookie ckU = new Cookie("username", userName);
}
}
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("username")) {
userName = cookies[i].getValue();
}
if (cookies[i].getName().equals("password")) {
password = cookies[i].getValue();
}
}
}
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为浏览器的自动表单归档功能( http://www. ehow.com/how_7503496_clear-browser-autofill-forms.html )?
Could it because of the browser's auto-form-filing feature ( http://www.ehow.com/how_7503496_clear-browser-autofill-forms.html )?
这是因为这就是您的代码的作用。如果未发送参数且未设置 cookie,您将不会在表单中看到任何内容。但是,如果用户输入用户名并按下提交按钮,则用户/密码将存储在 cookie 中。
现在,如果再次使用不带参数的表单,则无论如何都会从 cookie 中读取数据并用于初始化
userName
和password
的值。然后我相信这些价值观被用来启动形式上的价值观。您还没有发送此代码,但这是我的感觉。
It is because this is what your code does. If parameters are not sent and cookies are not set you do not see anything in your form. But if user once typed user name and pushed submit button the user/password are stored in cookie.
Now if use comes again to the form without parameters the data is read from cookies anyway and used to initiate values of
userName
andpassword
.Then I believe these values are used to initiate values in form. You have not sent this code but that is my feeling.