1. 前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="login" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
2. 后端登錄
package EnableUserLogin;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
// 1. 獲取到用戶提交的用戶名和密碼
String username = req.getParameter("username");
String password = req.getParameter("password");
// 2. 判定用戶名密碼是否正確
if (!username.equals("admin") || !password.equals("123")) {
// 登陸失敗
resp.getWriter().write("登陸失敗");
return;
}
// 登陸成功
System.out.println("登陸成功");
// 設(shè)置 Session
HttpSession session = req.getSession(true);
session.setAttribute("username", "admin");
session.setAttribute("loginCount", "0");
resp.sendRedirect("index");
}
}
3. 后端檢驗(yàn)頁(yè)面
package EnableUserLogin;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
// 1. 判定當(dāng)前用戶是否已經(jīng)登陸
HttpSession session = req.getSession(false);
if (session == null) {
// 用戶沒(méi)有登陸, 重定向到 login.html
resp.sendRedirect("login.html");
return;
}
// 2. 如果已經(jīng)登陸, 則從 Session 中取出訪問(wèn)次數(shù)數(shù)據(jù)
String userName = (String)session.getAttribute("username");
String countString = (String)session.getAttribute("loginCount");
int loginCount = Integer.parseInt(countString);
loginCount += 1;
session.setAttribute("loginCount", loginCount + "");
// 3. 展示到頁(yè)面上.
StringBuilder html = new StringBuilder();
html.append(String.format("<div>用戶名: %s</div>", userName));
html.append(String.format("<div>loginCount: %d</div>", loginCount));
resp.getWriter().write(html.toString());
}
}
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-733292.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-733292.html
到了這里,關(guān)于servlet實(shí)現(xiàn)登錄功能【當(dāng)用戶當(dāng)前未登陸,跳轉(zhuǎn)登錄頁(yè)面才能訪問(wèn),若已經(jīng)登錄了,才可以直接訪問(wèn)】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!