ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JSP 회원 쿠키
    삼성SDS_멀티캠퍼스/Web Application Server 2015. 10. 27. 16:49
    반응형


    <main.jsp>

    --------------------------------------------------------------------

    <%@page import="java.net.URLDecoder"%>

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>로그인 폼</title>

    </head>

    <body>

    <%

    request.setCharacterEncoding("UTF-8");

    %>

    <%

    String viewPageURI = "";

    Cookie[] cookies = request.getCookies();

    out.println(cookies);

    if (cookies != null && cookies.length > 1) {

    viewPageURI = "select.jsp";

    } else {

    viewPageURI = "loginForm.jsp";

    }

    %>


    <jsp:forward page="<%=viewPageURI%>"></jsp:forward>


    </body>

    </html>







    <loginForm.jsp>

    --------------------------------------------------------------------


    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>로그인 폼</title>

    </head>

    <body>


    <form action="/Project/cookie/doLogin.jsp" method="post">

    <table width="20%" border="1">

    <tr>

    <td colspan="3">

    id:<input type="text" name="id" size="30%"><br>

    pw:<input type="text" name="pw" size="30%">

    <td colspan="2"><input type="submit" value="전송"></td>

    </td>

    </tr>

    </table>

    </form>


    </body>

    </html>






    <doLogin.jsp>

    --------------------------------------------------------------------

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>

    <%

    request.setCharacterEncoding("UTF-8");

    %>

    <%

    String myId = "dnrl";

    String myPw = "dnrl";

    String id = request.getParameter("id");

    String pw = request.getParameter("pw");

    String viewPageURI = "";

    if (id.equals(myId) && pw.equals(myPw))

    viewPageURI = "loginSucc.jsp";

    else

    viewPageURI = "loginFail.jsp";

    %>

    <jsp:forward page="<%=viewPageURI%>"></jsp:forward>




    </body>

    </html>







    <loginSucc.jsp>

    --------------------------------------------------------------------

    <%@page import="java.net.URLEncoder"%>

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>


    <%

    Cookie cookie = new Cookie("name", URLEncoder.encode("우기"));

    response.addCookie(cookie);

    response.sendRedirect("/Project/cookie/main.jsp");

    %>



    </body>

    </html>










    <loginFail.jsp>

    --------------------------------------------------------------------

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>

    로그인에 실패했습니다.

    <%

    String viewPageURI = "main.jsp";

    %>

    <jsp:forward page="<%=viewPageURI%>"></jsp:forward>

    </body>

    </html>









    <select.jsp>

    --------------------------------------------------------------------


    <%@page import="java.net.URLDecoder"%>

    <%@page import="java.net.URLEncoder"%>

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>


    <%

    Cookie[] cookies = request.getCookies();

    if (cookies != null && cookies.length > 0) {

    %>

    <form action="view.jsp">

    보고싶은 페이지 선택 <select name="code">

    <option value="A">A페이지</option>

    <option value="B">B페이지</option>

    <option value="C">C페이지</option>

    </select> <input type="submit" value="이동">

    </form>

    <%

    }

    %>


    <form action="deleteCookie.jsp">

    <input type="submit" value="로그아웃">

    </form>


    </body>

    </html>







    <deleteCookie.jsp>

    --------------------------------------------------------------------

    <%@page import="java.net.URLDecoder"%>

    <%@page import="java.net.URLEncoder"%>

    <%@page import="com.sun.corba.se.spi.ior.MakeImmutable"%>

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Delete 쿠키</title>

    </head>

    <body>

    <%

    request.setCharacterEncoding("UTF-8");

    %>

    <%

    String viewPageURI = "main.jsp";

    Cookie[] cookies = request.getCookies();

    if (cookies != null && cookies.length > 0) {

    for (Cookie c : cookies) {

    String id = URLDecoder.decode(c.getName(), "UTF-8");

    Cookie cookie = new Cookie(id, "");

    cookie.setMaxAge(0);

    response.addCookie(cookie);


    }

    }

    %>

    <%

    response.sendRedirect("/Project/cookie/main.jsp");

    %>


    </body>

    </html>








    실행하면






    틀리면 loginForm에 머무르고


    ID pw맞으면 select.jsp로 빠진다






    여기서 고를수 있다



    B, C도 마찬가지로 볼 수 있고






    여기서 로그아웃을 누르면


    다시 로그인화면으로 빠진다.








    반응형

    '삼성SDS_멀티캠퍼스 > Web Application Server' 카테고리의 다른 글

    36일 차 세션  (0) 2015.10.29
    ORM할 때 깨닫게 될 것  (0) 2015.10.28
    34일 차 쿠키  (0) 2015.10.27
    33일 차 오늘 한 것  (0) 2015.10.26
    33일 차 액션태그를 사용하여 페이지 모듈화  (0) 2015.10.26
Designed by Tistory.