Intellij JSP - Session Login (세션 로그인 예제)

1 minute read

Session Login (세션 로그인 예제 )

  • Project - CookieLogin

    • s_home.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
                <title>Home</title>
        </head>
        <body>
        <%
                if(session.getAttribute("id")==null)
                        pageContext.include("s_login.jsp");
                else
                        pageContext.include("s_user_info.jsp");
        %>
        <input type="button" value="Get Cookie List" onclick="location.href='session_info.jsp'">
        </body>
        </html>
      
    • s_login.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
        </head>
        <body>
        <form action="create_session.jsp">
                <label>ID : <input type="text" name="id"></label><br/>
                <label>Password : <input type="text" name="pw"></label><br/>
                <input type="submit" value="Login">
        </form>
        </body>
        </html>
      
    • s_user_info.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
                <title>Session Information</title>
        </head>
        <body>
        <h2>Session Information</h2><br/>
        <hr>
        <%
                Enumeration e = session.getAttributeNames();
                while(e.hasMoreElements()){
                        String sName = e.nextElement().toString();
                        String sValue = session.getAttribute(sName).toString();
                        out.println("Session Name : "+sName+"<br>");
                        out.println("Session Value : "+sValue+"<hr><br>");
                }
      
                out.println("Session MaxInactiveInterval : "+session.getMaxInactiveInterval()+"<br>");
                out.println("Session ID : "+session.getId()+"<br>");
      
        %>
        <input type="button" value="Go Home" onclick="location.href='s_home.jsp'">
        </body>
        </html>
      
    • create_session.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
        </head>
        <body>
        <%
                String id, pw;
                id = request.getParameter("id");
                pw = request.getParameter("pw");
                if (id.equals("qwer") && pw.equals("1234")) {
                        session.setAttribute("id",id);
                        session.setAttribute("count",0);
                        response.sendRedirect("s_home.jsp");
                } else
        %>
        <script>
                if (!alert('아이디와 비밀번호를 확인해주세요')) document.location = 's_home.jsp'
                // if (confirm('아이디와 비밀번호를 확인해주세요')) document.location = 'home.jsp'
        </script>
        </body>
        </html>
      
    • s_logout.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
                <title>Logout</title>
        </head>
        <body>
        <%
                session.invalidate();
        %>
        <script>
                if (!alert('로그아웃 되었습니다')) document.location = 's_home.jsp'
        </script>
        </body>
        </html>
      
    • session_info.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
                <title>Session Information</title>
        </head>
        <body>
        <h2>Session Information</h2><br/>
        <hr>
        <%
                Enumeration e = session.getAttributeNames();
                while(e.hasMoreElements()){
                        String sName = e.nextElement().toString();
                        String sValue = session.getAttribute(sName).toString();
                        out.println("Session Name : "+sName+"<br>");
                        out.println("Session Value : "+sValue+"<hr><br>");
                }
      
                out.println("Session MaxInactiveInterval : "+session.getMaxInactiveInterval()+"<br>");
                out.println("Session ID : "+session.getId()+"<br>");
      
        %>
        <input type="button" value="Go Home" onclick="location.href='s_home.jsp'">
        </body>
        </html>
      
    • 로그인 (아이디나 비밀번호가 틀릴경우)
      • ID = qwer
      • Password = 1234

    • 로그인 성공

    • 로그아웃

    • 세션 정보

  • 신입SW인력을 위한 실전 JSP Servlet

Categories:

Updated:

Leave a comment