国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

servlet實(shí)現(xiàn)登錄功能【當(dāng)用戶當(dāng)前未登陸,跳轉(zhuǎn)登錄頁(yè)面才能訪問(wèn),若已經(jīng)登錄了,才可以直接訪問(wèn)】

這篇具有很好參考價(jià)值的文章主要介紹了servlet實(shí)現(xiàn)登錄功能【當(dāng)用戶當(dāng)前未登陸,跳轉(zhuǎn)登錄頁(yè)面才能訪問(wèn),若已經(jīng)登錄了,才可以直接訪問(wèn)】。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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

到了這里,關(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包