팝업창

it/JSP 2014. 8. 25. 18:16 Posted by 하얀나다
loginform.html


<!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> 로그인 </title>

</head>

<BODY>

<CENTER><FONT color=blue size=5>사용자 로그인</FONT> 

<P>


<FORM action="02_LoginService.jsp" method="post">

아이디: <INPUT name='user' type='text'><P>

비밀번호: <INPUT name='pass' type='password'><P>

<INPUT type='submit' value='Login'> 

</FORM>


</CENTER>

</P>

</BODY>

</html>


loginservice.jsp

<%@ page import="java.sql.*"%>

<%@ page import="java.io.*"%>

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

pageEncoding="EUC-KR"%>


<%

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

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


String rspass = "";


Connection con= null;

Statement ps =null;

ResultSet rs =null;


// 1. 드라이버 로딩

Class.forName("oracle.jdbc.driver.OracleDriver");

// 2. 연결객체 얻어오기

con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");


// 3. sql 문장 만들기


String sql = "select pass from imsi where id = '" +id+"'  " ;


// 4. sql 전송객체 얻어오기

ps = con.createStatement();

// 5. 전송

rs = ps.executeQuery(sql);

while(rs.next()){

rspass = rs.getString("pass");

}


%>

<!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>로그인확인</title>

</head>

<body >

<%

// 0. 실제로는 DB에서 가져와야하는 값

//String saveUser = "ljs7908";

//String savePass = "pass";


// 1. 이전화면 폼에서 넘겨받는 값


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

//String pass = request.getParameter("pass");

// 2. 실제값과 1번의 넘겨받은 값이 같을 때 로그인 성공 

/* if(rspass.equals("")){

out.println("아이디가 없다.");

}else{

out.println("아이디가 있다."+rspass);

} */

if (!rspass.equals("")&&rspass.equals(pass)) {

//1. 쿠키 생성 (name : login, value : 아이디값)

Cookie c = new Cookie("login", id);

//2. 쿠키를 클라이언트로 전송

response.addCookie(c);

%>

<h2>

<%=id %>님, 성공적으로 로그인하셨음돠.

</h2>

<p>

<a href="02_MainPage.jsp">들어가기</a>


<%

// 3. 그렇지 않으면 로그인 실패

}else{

// response.sendRedirect("02_LoginForm.html");

%>

<h2>로그인에 실패했슴다...</h2>

<p>

<a href="02_LoginForm.html"> 되돌아가기 </a>


<%

}

%>

</body>

</html>


mainpage.jsp

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

pageEncoding="utf-8"%>

<%

//1. 클라이언트로 부터 쿠기 얻어오기

Cookie ck[] = request.getCookies();

String name = "";

//2. 그 쿠키 중에 login 이라는 이름의 쿠키가 있는지 확인

//3. 만일 없다면 - 로그인 하지 않았기 떄문,

//    - 로그인 폼으로 이동하고 리턴

for (int i = 0; ck != null && i < ck.length; i++) {


if ((ck[i].getName()).equals("login")) {

//out.println( ck[i].getValue()+" 님이 접속중 <br>");

name = ck[i].getValue();

//out.println(name);

}


}

if (name.equals("")) {

response.sendRedirect("02_LoginForm.html");

}

%>

<!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>

<script type="text/javascript">

function showPopup(){

<%

   Cookie[] cookies=request.getCookies();

   boolean isShow = true;

   if(cookies!=null){

       for(Cookie tmp: cookies){

           String name1=tmp.getName();

           String value=tmp.getValue();

           if(name1.equals("popup") && value.equals("popup_no")){

               isShow=false;

               break;

           }

       }

   }

   if(isShow){

%>

    window.open("event.jsp", "팝업창", "width=300, height=400"); 

<%

  }

%>

}

</script>

</head>

<body onload="showPopup();">

<center>

<h2>이 페이지는 로그인을 하셔야만 볼 수 있는 페이지 입니다</h2>

<br /> <br /> <br />

<%=name%>님, 로그인 중입니다.


</center>

</body>

</html>


event.jsp

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


<!DOCTYPE html>

<html>

<head>

<title>안녕하세요.팝업창입니다.</title>

<script>

  function closeWindow(){

   //폼전송하기

   document.popupForm.submit();

  }

</script>

</head>


<body bgcolor="skyblue">

<%

  String chkpopup=request.getParameter("chkbox1");

  System.out.println(chkpopup);


  if(chkpopup!=null){ 

   Cookie cook=new Cookie("popup","popup_no");

   cook.setMaxAge(10); //60초

   response.addCookie(cook);

%>

<script>self.close();</script>

<% 

  }


%>   




<form method="post" action="event.jsp" name="popupForm">

  <input type="checkbox" name="chkbox1" value="popup_no" />

  <a href="javascript:closeWindow();">60초동안 안보이게 하기</a>

</form>


</body>

</html>