您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關java如何實現登錄,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內容如下
一、環境搭建
JDK1.8 + Tomcat1.8
二、目錄結構
三、代碼示例
3.1、fail.html頁面
<!DOCTYPE html> <html> <head> <title>faill.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css" rel="external nofollow" rel="external nofollow" >--> </head> <body> <font color='red' size='3'>親, 你的用戶名或密碼輸入有誤!請重新輸入!</font> <br /> <a href="/project03/login.html" >返回登錄頁面</a> </body> </html>
3.2、Login.htm頁面
<!DOCTYPE html> <html> <head> <title>Login.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css" >--> </head> <body> <form action="/project03/LoginServlet" method="post"> 用戶名:<input type="text" name="UserName" /><br /> 密 碼:<input type="password" name="UserPwd" /><br /> <input type="submit" value="登錄" /> </form> </body> </html>
3.3、IndexServlet.java
package cn.itcase.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 用戶主頁邏輯 * */ public class IndexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置編碼格式 response.setContentType("text/html;charset=utf-8");// setContentType設置瀏覽器的編碼格式 // 1.信息輸出至瀏覽器 PrintWriter writer = response.getWriter(); String html = ""; /** * 接收request域對象的數據 String loginName = * (String)request.getAttribute("loginName",userName); * */ /** * 在用戶主頁,判斷session對象不為空且存在指定的屬性則登錄成功 才能訪問資源。從session域對象中取出會話數據 * * * */ // 2.得到session對象 HttpSession session = request.getSession(false); // 2.1如果不存在session對象,登錄不成功,跳轉到登錄頁面 if (session == null) { response.sendRedirect(request.getContextPath() + "/Login.html"); return; } // 2.2沒有在session對象域中找到相應 session唯一標識ID 則登錄不成功,跳轉到登錄頁面 String loginName = (String) session.getAttribute("loginName"); if (loginName == null) { response.sendRedirect(request.getContextPath() + "/Login.html"); return; } html = "<html><body>歡迎回來," + loginName + ",<a href='" + request.getContextPath() + "/LogoutServlet'>安全退出</a></body></html>"; writer.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3.4、LoginServlet.java
package cn.itcase.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 登錄的邏輯 * 設置編碼格式 * 根據參數名獲取參數值 * 判斷邏輯(使用session域對象) * * */ public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置編碼格式 request.setCharacterEncoding("utf-8");// setCharacterEncoding設置服務器的編碼格式 // 1.根據參數名獲取參數值 String userName = request.getParameter("UserName"); String userPwd = request.getParameter("UserPwd"); // 2.登錄是否的邏輯判斷 if("eric".equals(userName) && "123456".equals(userPwd)){ /**分析使用技術: * context域對象:不合適,可能會覆蓋數據 * request.setAttribute("loginName",userName); * * request域對象:不合適,整個網站必須得使用轉發技術來跳轉 * request.getRequestDispatcher("/IndexServlet").forward(request,response); * * session域對象:合適 * response.sendRedirect(request.getContextPath()+"/IndexServlet") * */ //2.1 登錄成功 // 2.1.1創建session對象 用于保存數據 HttpSession session = request.getSession(); // 2.1.1把數據保存到session域中 session.setAttribute("loginName", userName); // session對象的唯一標識"loginName" 唯一標識名稱 userName //session.setMaxInactiveInterval(1*60*60*24*30); // session對象的有效時長 可以配置全局的有效時長 //2.1.3跳轉到用戶主頁 response.sendRedirect(request.getContextPath() + "/IndexServlet"); //sendRedirect()重定向 getContextPath()請求路徑 }else{ //2.2登錄失敗 請求重定向 response.sendRedirect(request.getContextPath() + "/fail.html"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); doGet(request,response); } }
3.5、LogoutServlet.java
package cn.itcase.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 退出邏輯 * */ public class LogoutServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 安全退出 * 刪除session對象中指定的loginName屬性即可 * */ HttpSession session = request.getSession(false); if(session != null){ session.removeAttribute("loginName"); } //返回登錄頁面 response.sendRedirect(request.getContextPath() + "/Login.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
關于“java如何實現登錄”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。