為什么輸入用戶名后登陸成功查看用戶信息用戶名變成了和密碼一樣的字符?
這個是查看用戶信息
<h1>查看用戶信息</h1>
? <hr>
? <%
? request.setCharacterEncoding("utf-8");
? String username = "";
? String password = "";
? ? Cookie[] cookies = request.getCookies();
? ? ?if(cookies!=null && cookies.length>0)
? ? ?{
? ? ?for(Cookie c : cookies)
? ? ?{
? ? ? ? if(c.getName().equals("username") || c.getName().equals("password"))
? ? ? ? {
? ? ? ? ? ? username = URLDecoder.decode(c.getValue(),"utf-8");
? ? ? ? ? ? password = URLDecoder.decode(c.getValue(),"utf-8");
? ? ? ? }
? ? ?}
? ? ?}
? ?%>
? <br>
? <br>
? <br>
? 用戶名:<%=username %><br>
? 密碼:<%=password %><br>
這個是登錄成功驗證頁面
<h1>登陸成功</h1>
? <hr>
? <br>
? <br>
? <br>
? <%
??
? request.setCharacterEncoding("utf-8");
? //首先判斷用戶是否選擇了記住登錄狀態(tài)
? String[] isUseCookie =request.getParameterValues("isUseCookie");
? if(isUseCookie!=null && isUseCookie.length>0)
? {
? ? ?//把用戶名和密碼保存在Cookie對象里面
? ? ?String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
? ? ?//使用URLEncoder解決無法在Cookie當(dāng)中保存字符串問題
? ? ?String password = URLEncoder.encode(request.getParameter("password"),"utf-8");
? ? ?
? ? ?
? ? ?
? ? ?
? ? ?Cookie usernameCookie = new Cookie("username",username);
? ? ?Cookie passwordCookie = new Cookie("password",password);
? ? ?usernameCookie.setMaxAge(864000);//設(shè)置最大生存期限為十天
? ? ?passwordCookie.setMaxAge(864000);
? ? ?response.addCookie(usernameCookie);
? ? ?response.addCookie(passwordCookie);
? }
? else
? {
? ? ?Cookie[] cookies = request.getCookies();
? ? ?if(cookies!=null && cookies.length>0)
? ? ?{
? ? ?for(Cookie c : cookies)
? ? ?{
? ? ? ? if(c.getName().equals("username") || c.getName().equals("password"))
? ? ? ? {
? ? ? ? ? ? c.setMaxAge(0);//設(shè)置Cookie失效
? ? ? ? ? ? response.addCookie(c);
? ? ? ? ? ??
? ? ? ? }
? ? ?}
? ? ?}
? }
? ?%>
? <a href="users.jsp" target="_blank">查看用戶信息</a>
這個是用戶登錄頁面
?<h1>用戶登錄</h1>
? <hr>
? ?<%
? ?request.setCharacterEncoding("utf-8");
? String username = "";
? String password = "";
? ? Cookie[] cookies = request.getCookies();
? ? ?if(cookies!=null && cookies.length>0)
? ? ?{
? ? ?for(Cookie c : cookies)
? ? ?{
? ? ? ? if(c.getName().equals("username") || c.getName().equals("password"))
? ? ? ? {
? ? ? ? ? ? username = URLDecoder.decode(c.getValue(),"utf-8");
? ? ? ? ? ? password = URLDecoder.decode(c.getValue(),"utf-8");
? ? ? ? }
? ? ?}
? ? ?}
? ?%>
? <form name="loginForm" action="dologin.jsp" method="post">
? <table>
? <tr>
? <td>用戶名:</td>
? <td><input type="text" name="username" value="<%= username%>"/></td>
? </tr>
? <tr>
? <td>密碼:</td>
? <td><input type="password" name="password" value="<%= password%>"/></td>
? </tr>
? <tr>
? <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked" >十天內(nèi)記住我的登錄狀態(tài)</td>
? </tr>
? <tr>
? <td colspan="2" align="center"><input type="submit" value="登錄"/><input type="reset" value="取消"/></td>
? </tr>
? </table>
2015-12-02
? ? ? ? if(c.getName().equals("username") || c.getName().equals("password"))
? ? ? ? {
??????????? username = URLDecoder.decode(c.getValue(),"utf-8");
? ? ? ? ? ? password = URLDecoder.decode(c.getValue(),"utf-8");
? ? ? ? }
這個不能寫在一起 如下:
??? ???if(c.getName().equals("username")) {
??? ????username = URLDecoder.decode(c.getValue(),"utf-8");
??? ???}
??? ???if(c.getName().equals("password")) {
??? ????password = c.getValue();
??? ???}