본문 바로가기

개발하자/JSP&Servlet

쿠키 이용한 아이디 기억하기 예제

welcome.jsp

===================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
로그인해주세요~

 

top.jsp

===================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<h1>서울IT교육센터</h1>
<div align="right">Guest님 즐거운시간되세요~</div>

template.jsp

====================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String contentPage="welcome.jsp";
if(request.getParameter("contentP")!=null)
contentPage=request.getParameter("contentP");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">
<html>
<style type="text/css">
table { width:500px; height:400px; }
td { border:2px solid navy; }
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-

KR">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td colspan="2">
<jsp:include page="top.jsp" />
</td>
</tr>
<tr>
<td>
<jsp:include page="menu.jsp" />
</td>

<td>
<jsp:include page="<%=contentPage%>" />
</td>
</tr>
</table>
</body>
</html>

 

menu.jsp

=========================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
총방문자수:<br>
현재접속자수:<br>
회원리스트<br>
직원리스트<br>
<a href="template.jsp?contentP=loginForm.jsp">로그인</a><br>
<a href="logout.jsp">로그아웃</a><br>

 

logout.jsp

=========================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
 <%
  session.invalidate();
  response.sendRedirect("template.jsp");
 
 %>
</body>
</html>

 

loginSuccess.jsp

===============================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
response.sendRedirect("template.jsp");
%>
</body>
</html>

 

loginRe.jsp

==============================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<%
 String id = request.getParameter("id");
 String pw = request.getParameter("pw");
 String check = request.getParameter("check");

 String contentP = "loginFailure.jsp";
 if (id.equals(pw)) {

  if (check != null) {
   Cookie coo = new Cookie("id", id);
   coo.setMaxAge(60 * 5);
   response.addCookie(coo);

  } else {
   Cookie[] coos = request.getCookies();
   if (coos != null)
    for (Cookie coo : coos) {
     if (coo.getName().equals("id")) {
      Cookie coo2 = new Cookie("id", "");
      coo2.setMaxAge(0);
      response.addCookie(coo2);
     }
    }
  }
  contentP = "loginSuccess.jsp";
 }
 response.sendRedirect("template.jsp?contentP=" + contentP);
%>
<body>

</body>
</html>

 

loginForm.jsp

=============================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>

<%
 String id="";
 Cookie[] coos = request.getCookies();
 if (coos != null) {
  for (Cookie coo : coos) {
   if (coo.getName().equals("id")) { //"id"
    id = coo.getValue();
   }
  }
 }
%>


<form action="loginRe.jsp">
 ID:<input type="text" name="id" value=<%=id%>><br> PW:<input
  type="text" name="pw"><br> <input type="checkbox"
  name="check"> 아이디기억하기<br> <input type="submit"
  value="로그인">
</form>

 

=======================================================================

쿠키예제

 

coo1.jsp

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

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
 <%
  Cookie coo1 = new Cookie("aa", "bb");
  response.addCookie(coo1);
 %>
 <a href="coo2.jsp">쿠키정보보기</a>
</body>
</html>

 

coo2.jsp

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

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<!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=EUC-

KR">
<title>Insert title here</title>
</head>
<body>
 <%
  Cookie[] coos = request.getCookies();
  for (Cookie coo : coos) {
   out.println(coo.getName() + ":");
   out.println(coo.getValue() + "<br>");
  }
 %>
</body>
</html>

'개발하자 > JSP&Servlet' 카테고리의 다른 글

EL)paramValues  (0) 2015.03.12
EL)param  (0) 2015.03.12
useBean  (0) 2015.03.10
doPost  (0) 2015.03.05
load-on-startup  (0) 2015.03.05