JSP

Login&Logout 기본패턴

사라링 2012. 5. 8. 18:16

index.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>방가 방가 ~~/index.jsp</title>
</head>
<body >
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/main.css" />
    <div id="topMenu">
        <jsp:include page="/include/top.jsp" />
    </div>
    <h3>이쁜 선희의 웹페이지</h3>

<img src="<%=request.getContextPath() %>/img/5985.jpg" alt="절대경로 3" width="100px"  /><br />

</body>
</html>

top.jsp(include)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   
    <%--
    request.setCharacterEncoding("utf-8");--%>
<%-- // incloude/top.jsp --%>
<ul>
    <li>HOME</li>
    <li>회원관리</li>
    <li>게시판</li>
    <li>사이트맵</li>
<%--     <li>msg=<%=request.getParameter("msg") %></li>
 --%>
    <%
        String id = (String)session.getAttribute("USER_ID");
        if(id==null){   
        %>
        <li><a href="<%=request.getContextPath()%>/07/login.jsp">로그인</a>
        </li>
        <%   
        }else {
        %>
        <li><%=id %>님 방가 방가 ^^
        <a href="<%=request.getContextPath()%>/07/logout.jsp">로그아웃</a>
        </li>
        <%
        }
   
    %>

</ul>

login.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>로그인 07/ login.jsp</title>
<%
    String id="";
    String checked="checked='checked'";
    // 쿠키 배열을 읽어서 이름중에 "user_id" 가 있다면
    // 그 값을 id에 넣고 ,checked변수에는 "";
    Cookie cookie[] =request.getCookies();
    for(int i =0;i<cookie.length;i++){
        if(cookie[i].getName().equals("user_id")){
            id=cookie[i].getValue();
            checked="";
            break;
        }
    }

%>
</head>
<body>


<h4>로그인</h4>
<form action="logincheck.jsp" method="post">
    <label>아이디
        <input type="text" name="user_id" value="<%=id %>" />
    </label>
    <label>패스워드
        <input type="password" name="user_pw" />
    </label>
    <label>
        <input type="checkbox" <%=checked %> name="id_save" value="Y"/>
        ID 기억
    </label>
    <button type="submit">로그인</button>
</form>


</body>
</html>

loginchecked.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%!static Map memberMap;
    static {
        memberMap = new HashMap();
        memberMap.put("hwang", "java");
        memberMap.put("malja", "1004");
        memberMap.put("sunhee", "pretty");
        memberMap.put("sararing", "1234");
    }%>
   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>07 /logincheck.jsp</title>
</head>
<body>
<%
    //사용자가 입력한 파라미터 user_id  user_pass 를 받아서
    // 등록된 것과 비교를 해야함.
    //맞으면 세션에 "USER_ID" 키로 해당 user_id 저장
    // / index.jsp로 다이렉트 하고 
    //  틀리면 자바 스크립트로 메시지 창을 보여 주고 login.jsp로 이동
    String id = request.getParameter("user_id");
    String pw = request.getParameter("user_pw");
    String checked = request.getParameter("id_save");
    if (id != null) {
        if (memberMap.containsKey(id)) {
            String dbPass = (String) memberMap.get(id);
            if (dbPass.equals(pw)) {
                session.setAttribute("USER_ID", id);
                //sendRedirect 는 헤더에 3xx 대 상태 코드로 설정 하고
                // Location 헤더에 다시 들어올 경로를 알려 주는데
                // 클라이언트가 다시 와야하므로 웹경로를 포함 시켜야 한다.
                if(checked!=null){
                    /* "user_id",id, 한달,루트로 저장
                     */
                    Cookie cookie =new Cookie("user_id",id);
                    cookie.setMaxAge(60*60*24*30);
                    cookie.setPath("/ddit");
                    response.addCookie(cookie);
                }else {
                    Cookie cookie =new Cookie("user_id","");
                    cookie.setMaxAge(0);
                    cookie.setPath("/ddit");
                    response.addCookie(cookie);
                }
                response.sendRedirect(request.getContextPath()+"/index.jsp");
                return;
            }else {
                // 패스워드가 틀릴때
                // 자바 스크립트로 경고 메시지 출력후 login.jsp로 점프
                %>
            <script type="text/javascript">
            <!--
                alert("d야 패스워드가 틀려");
                location.href="login.jsp";
            //-->
            </script>
                <%
                return;
            }
           
        }
    }
    // id가 null 이거나 memberMap 에 없는 경우
%>
            <script type="text/javascript">
            <!--
                alert("아이디를 입력해주세요");
                location.href="login.jsp";
            //-->
            </script>
</body>
</html>

 

 

 

 

 

logout.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>07/logout.jsp</title>
</head>
<body>
<%
    //세션중 임의 키만 제거 은 밑
    //session.removeAttribute("USER_ID");

    // 기존세션 무효화,새로운 세션키 활당
    session.invalidate();
    response.sendRedirect(request.getContextPath()+"/index.jsp");


%>


</body>
</html>

 

 

이 글은 스프링노트에서 작성되었습니다.