使用Cookie保存用戶名密碼,再次登陸時將Cookie用戶名密碼取出來並直接放置到用戶名密碼文本框中


     如下分為兩份代碼:登陸頁“test_6.11_login.jsp”,登陸成功頁“test_6.11_page.jsp”

     登陸頁中,用戶輸入用戶名密碼,點擊提交,后台對照mysq數據庫中,看是否有對應的用戶名,以及密碼是否正確。如果正確

則將用戶名密碼分兩份Cookie保存。頁面跳轉到登陸成功頁。

    用戶再次訪問登陸頁時,先取出Cookie,判斷是否存在用戶名密碼的Cookie,如果有,則將值保存在變量或者request中。接着

將Cookie的變量或request賦值給用戶名密碼文本輸入框。到此完成了保存用戶名密碼的功能。

   test_6.11_login.jsp

<%@ page language="java" import="java.util.*,java.sql.*,java.net.*" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>

<html>
<head>
</head>

<body>
<%!
//定義數據庫驅動程序
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3307/yunmobile";
public static final String DBUSER="rood";
public static final String DBPASS="234567";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
%>
<%
String username_ = null;
Cookie c[]=request.getCookies();
if(c!=null)
{
for(int x=0;x<c.length;x++)
{
if(c[x].getName().equals("username"))
{
//在cookie值保存時如果編碼了,取cookie時就需要進行解碼。
//將cookie值取出來后,賦值給變量,用以之后的顯示
username_ = URLDecoder.decode(c[x].getValue(),"UTF-8");
}
else if(c[x].getName().equals("password"))
{
//將cookie值取出來后,賦值給request,用以之后的顯示
request.setAttribute("password",c[x].getValue());
}
}
}
%>

<form action="test_6.11_login.jsp" method="post" name="myform">
<!-- 實話說,下面的用戶名和密碼value的賦值,糾纏了好久。倆值一個用變量,一個用request保存 -->
用戶名:<input type="text" id="userNameId" name="username" value="<%=username_ %>"/><br/>
<!-- ${}是顯示變量password的值,如果值是null則不顯示,非null則顯示具體值 -->
密碼:<input type="password" id="passWordId" name="password" value="${password}"/><br/>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>


<%
//若用戶名密碼中有中文,雖然提交給當前頁,底下的設置編碼方式也是必須的
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("username");
String password=request.getParameter("password");
if(!(name==null||name.equals("")||password==null || password.equals("")))
{
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="SELECT * FROM member WHERE name=? AND password=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,password);
rs=pstmt.executeQuery();
if(rs.next())
{
if(rs.getString(3).equals(name) && rs.getString(2).equals(password))
{
//如果用戶名密碼和數據庫中值匹配,則跳轉到登陸成功頁
response.setHeader("refresh","3;URL=test_6.11_page.jsp");
//登陸成功頁面通過session傳遞用戶名,用於登陸成功頁面顯示用戶名
session.setAttribute("username",name);
//cookie保存時,如果用戶名或密碼有漢字或者其他特殊字符需要進行編碼
Cookie c1=new Cookie("username",URLEncoder.encode(name,"UTF-8"));
Cookie c2=new Cookie("password",password);
c1.setMaxAge(76000);
c2.setMaxAge(76000);
response.addCookie(c1);
response.addCookie(c2);
}
}
else
{
out.println("<h2>用戶名或密碼錯誤哦哦哦!</h2>");
}
}catch(Exception e){
out.println(e);
}
}
%>
</body>
</html>

 

 

test_6.11_page.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<html>
<head><body>
<%
if(!(session.getAttribute("username")==null))
{
%>
<h3>歡迎<%=session.getAttribute("username") %>光臨本系統</h3>
<%
}
else
{
%>
<h3>請先進行系統的<a href="login_6.11_login.jsp">登陸</a>!</h3>
<%} %>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM