萦芑

萦芑

Hi!歡迎來到我的Blog,這是一個去中心化的Blog,在這裏我將分享對你可能感興趣的科技技術

IDEA使用JSP使用者登錄案例

導讀#

JSP 實現使用者登錄、註銷

準備工作#

IDEA、tomcat、JDK18

登錄介面 HTML#


<title>使用者登錄</title>
</head>
<body>
<form action="index.jsp" method="post" >
    <h3><span>登錄</span></h3>
    <a>使用者名稱:</a><input type="text" name="username"><br>
    <a >密碼:</a><input type="password" name="password"><br>
    <br>
    <div>
        <button >登錄</button>
        <INPUT TYPE="reset" value="重置" >
        <INPUT TYPE="button" value="註冊" >
    </div>
</form>

登錄資訊密碼帳號正確或錯誤處理程式碼#


<%
    request.setCharacterEncoding("UTF-8");
    String name=request.getParameter("username");
    String password=request.getParameter("password");
%>
<%
    if(name.equals("小夢")&&password.equals("666"))
    {
        out.print("輸入正確");
        request.setCharacterEncoding("UTF-8");
        session.setAttribute("username",name);
        request.getRequestDispatcher("welcome.jsp").forward(request, response);

    }else{%>

<% }%>
帳號或密碼錯誤,等待 3s 跳轉登錄<br>
<input type="button"  value="返回登錄" onclick='location.href=("login.html")'/>
<% response.setHeader("Refresh", "3,URL=login.html");%>

登錄成功介面#


<% String name=(String)session.getAttribute("username");
    if(name!=null){%>

<div >
    <form action="index.jsp" method="post" >
        <h3><%=session.getAttribute("username")%><a>,歡迎光臨,訪問個人空間</a></h3>
        <div >
            <INPUT TYPE="reset" value="註銷"onclick='location.href=("kill.jsp")' >
            <input type="button"  value="返回登錄" onclick='location.href=("login.html")'/>
        </div>
    </form>
</div>
<%}else{ %>
<% response.setHeader("Refresh", "0,URL=kill.jsp");%>
<%} %>

註銷帳號介面#


<% String name=(String)session.getAttribute("username");
    if(name!=null){%>
<div  >
    <form action="index.jsp" method="post" >
        <h3><%=session.getAttribute("username")%><a>,正在註銷帳號登錄,請等待5s</a></h3>
        <div>
            <%session.invalidate();
                response.setHeader("Refresh", "5,URL=login.html");
            %>
        </div>
            <%}else{ %>
        <div >
            <form action="index.jsp" method="post" >

                <h3><%=session.getAttribute("username")%><a>,原因:您未登錄請登錄後再拭,5s後自動回登錄</a></h3>
                <div style="text-align: center">
                    <input type="button"  value="返回登錄" onclick='location.href=("login.html")'/>
                </div>
            </form>
        </div>
            <% response.setHeader("Refresh", "5,URL=login.html");%>
            <%} %>
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。