java實(shí)現(xiàn)登錄案例
本文實(shí)例為大家分享了java實(shí)現(xiàn)登錄案例的具體代碼,供大家參考,具體內(nèi)容如下
一、環(huán)境搭建
JDK1.8 + Tomcat1.8
二、目錄結(jié)構(gòu)

三、代碼示例
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'>親, 你的用戶名或密碼輸入有誤!請(qǐng)重新輸入!</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 {
// 設(shè)置編碼格式
response.setContentType("text/html;charset=utf-8");// setContentType設(shè)置瀏覽器的編碼格式
// 1.信息輸出至瀏覽器
PrintWriter writer = response.getWriter();
String html = "";
/**
* 接收request域?qū)ο蟮臄?shù)據(jù) String loginName =
* (String)request.getAttribute("loginName",userName);
*
*/
/**
* 在用戶主頁,判斷session對(duì)象不為空且存在指定的屬性則登錄成功 才能訪問資源。從session域?qū)ο笾腥〕鰰?huì)話數(shù)據(jù)
*
*
* */
// 2.得到session對(duì)象
HttpSession session = request.getSession(false);
// 2.1如果不存在session對(duì)象,登錄不成功,跳轉(zhuǎn)到登錄頁面
if (session == null) {
response.sendRedirect(request.getContextPath()
+ "/Login.html");
return;
}
// 2.2沒有在session對(duì)象域中找到相應(yīng) session唯一標(biāo)識(shí)ID 則登錄不成功,跳轉(zhuǎn)到登錄頁面
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;
/**
* 登錄的邏輯
* 設(shè)置編碼格式
* 根據(jù)參數(shù)名獲取參數(shù)值
* 判斷邏輯(使用session域?qū)ο螅?
*
*
*/
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 設(shè)置編碼格式
request.setCharacterEncoding("utf-8");// setCharacterEncoding設(shè)置服務(wù)器的編碼格式
// 1.根據(jù)參數(shù)名獲取參數(shù)值
String userName = request.getParameter("UserName");
String userPwd = request.getParameter("UserPwd");
// 2.登錄是否的邏輯判斷
if("eric".equals(userName) && "123456".equals(userPwd)){
/**分析使用技術(shù):
* context域?qū)ο螅翰缓线m,可能會(huì)覆蓋數(shù)據(jù)
* request.setAttribute("loginName",userName);
*
* request域?qū)ο螅翰缓线m,整個(gè)網(wǎng)站必須得使用轉(zhuǎn)發(fā)技術(shù)來跳轉(zhuǎn)
* request.getRequestDispatcher("/IndexServlet").forward(request,response);
*
* session域?qū)ο螅汉线m
* response.sendRedirect(request.getContextPath()+"/IndexServlet")
* */
//2.1 登錄成功
// 2.1.1創(chuàng)建session對(duì)象 用于保存數(shù)據(jù)
HttpSession session = request.getSession();
// 2.1.1把數(shù)據(jù)保存到session域中
session.setAttribute("loginName", userName); // session對(duì)象的唯一標(biāo)識(shí)"loginName" 唯一標(biāo)識(shí)名稱 userName
//session.setMaxInactiveInterval(1*60*60*24*30); // session對(duì)象的有效時(shí)長(zhǎng) 可以配置全局的有效時(shí)長(zhǎng)
//2.1.3跳轉(zhuǎn)到用戶主頁
response.sendRedirect(request.getContextPath() + "/IndexServlet"); //sendRedirect()重定向 getContextPath()請(qǐng)求路徑
}else{
//2.2登錄失敗 請(qǐng)求重定向
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對(duì)象中指定的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);
}
}
3.6、總結(jié)
知道了如何實(shí)現(xiàn)前端頁面與后端的數(shù)據(jù)交互
疑惑:如果有多個(gè)用戶難道還一個(gè)一個(gè)的去判斷他存不存在么?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot基于RabbitMQ實(shí)現(xiàn)消息可靠性的方法
RabbitMQ 提供了 publisher confirm 機(jī)制來避免消息發(fā)送到 MQ 過程中丟失,這種機(jī)制必須給每個(gè)消息指定一個(gè)唯一ID,消息發(fā)送到MQ以后,會(huì)返回一個(gè)結(jié)果給發(fā)送者,表示消息是否處理成功,本文給大家介紹了SpringBoot基于RabbitMQ實(shí)現(xiàn)消息可靠性的方法,需要的朋友可以參考下2024-04-04
Java常用數(shù)字工具類 數(shù)字轉(zhuǎn)漢字(1)
這篇文章主要為大家詳細(xì)介紹了Java常用數(shù)字工具類,數(shù)字轉(zhuǎn)漢字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
java數(shù)據(jù)結(jié)構(gòu)與算法之插入算法實(shí)現(xiàn)數(shù)值排序示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之插入算法實(shí)現(xiàn)數(shù)值排序的方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了插入算法的節(jié)點(diǎn)操作與排序相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
JAVA 實(shí)現(xiàn)磁盤文件加解密操作的示例代碼
這篇文章主要介紹了JAVA 實(shí)現(xiàn)磁盤文件加解密操作的示例代碼,幫助大家利用Java實(shí)現(xiàn)文件的加解密,感興趣的朋友可以了解下2020-09-09
JAVA Swing實(shí)現(xiàn)窗口添加課程信息過程解析
這篇文章主要介紹了JAVA Swing實(shí)現(xiàn)窗口添加課程信息過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

