본문 바로가기

(Before)BitSchool

2014/05/30 JSP - session,cookie, javaBean

반응형

Session


login_form.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page session="true"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    session.setAttribute("id""1111");
    // session객체에 id라는 이름에 변수를 1111이라는 값을 추가한다.
    session.getValue("id");
    // id라는 이름을 갖는 변수의 값을 가져와라
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
 
    <form action="login.jsp" method="post">
        아이디 : <input type="text" name="id"/>
        패스워드 : <input type="password" name="pw"/>
        <input type="submit" value="로그인"/>
    </form>
</body>
</html>




login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@page import="sun.misc.Perf.GetPerfAction"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page session = "true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<%
    String id = request.getParameter("id");
    String pw = request.getParameter("pw");
    
    if(id.equals("bit") && pw.equals("1234")){
        //로그인 성공
        session.setAttribute("id", id);
        
    }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <%
        if(id.equals("bit") && pw.equals("1234")){
    %>
        로그인 성공
    <%
        }else{
    %>
        로그인 실패
    <%
        }    
    %>
</body>
</html>




logincheck.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 
<% 
    String id = (String)session.getValue("id");
    boolean isLogin = (id==null)?false:true;
%>
<!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>
    <%
        if(isLogin){
    %>
    <%=id %>님 환영합니다.
    <%}else{ %>
        로그인 정보가 없습니다. 이동 하시겠습니까?
        <%} %>
        
        <a href="login_form.jsp"> 다시 로그인 하기</a>
 
</body>
</html>




Cookie


session은 서버에서만 사용가능 cookie는 서버,클라이언트 양쪽다 사용가능




클라이언트에서 id저장, 북마크에서 많이 쓰인다. 


writerCookie.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import = "java.net.URLEncoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% 
    Cookie cookie = new Cookie("name",URLEncoder.encode("비트"));
    response.addCookie(cookie);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <% cookie.getName(); %> cookie value : <%= cookie.getValue() %>
    
</body>
</html>



cookie값 불러오기

ReadCookie.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<%@page import="java.net.URLDecoder"%>
<%@ 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[] cookies = request.getCookies(); // 쿠키는 배열로 저장되어 배열전체를 불러온다.
        if(cookies != null && cookies.length>0){
            for(int i=0;i<cookies.length;i++){
    %>
    <br>
    <%= cookies[i].getName() %>   
    <!--  쿠키 값 출력 -->
    <%= URLDecoder.decode(cookies[i].getValue()) %>
    <%
        }
        }
    %>
    
    
    
</body>
</html>



쿠키값 변경

Cookieupdate.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ page import="java.net.URLEncoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<%
    Cookie[] cookies = request.getCookies();
    if(cookies!=null && cookies.length >0){
        for(int i=0; i<cookies.length;i++){
            if(cookies[0].getName().equals("name")){
                Cookie cookie = new Cookie("name",URLEncoder.encode("비트2"));
                response.addCookie(cookie);
            }
        }
    }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    name쿠키의 값을 변경 했습니다.
</body>
</html>


쿠키값 삭제 

Cookiedelete.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ page import="java.net.URLDecoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    Cookie[] cookies = request.getCookies();
    if(cookies != null && cookies.length>0){
        for(int i=0; i<cookies.length;i++){
            if(cookies[0].getName().equals("name")){
                Cookie cookie = new Cookie("name","");
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }
        }
    }
%>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    name 쿠키의 값 삭제
    
    <%
        cookies = request.getCookies(); // 쿠키는 배열로 저장되어 배열전체를 불러온다.
        if(cookies != null && cookies.length>0){
            for(int i=0;i<cookies.length;i++){
    %>
    <br>
    <%= cookies[i].getName() %>   
    <!--  쿠키 값 출력 -->
    <%= URLDecoder.decode(cookies[i].getValue()) %>
    <%
        }
        }
    %>
</body>
</html>




JavaBean

- 데이터를 표현하는 것을 목적으로 하는 자바 클래스

- 데이터를 저장하는 필드, 데이터를 읽어 오는 메소드, 데이터를 저장하는 메소드로 구성

- 읽기 전용 프로퍼티 : get 또는 is메소드만 존재하는 프로퍼티

 - 읽기/쓰기 프로퍼티 : get/set 또는 is/set 메소드가 존재하는 프로퍼티


servlet은 외부에서 호출이 가능하다. Bean은 JSP에서만 접근가능하다.

JSP에서 java코드를 걷어내기위한 방법이 Bean이다.

javabean은 패키지를 줘야한다.


Regster_Form.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ 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>
    <form action="register.jsp" method="post">
        id : <input type="text" name="id" size="10"/><br>
        이름 : <input type="text" name="name" size="10"/><br>
        이메일 : <input type="text" name="email" size="10"/><br>
        <input type="submit" value="등록"/>
            
    </form>
</body>
</html>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package kr.bit;
 
public class Member {
    //1) 이름확인
    String id;
    String name;
    String email;
    
    //2) get/set 이름 확인
    // get/set 은 소문자로 변수 이름 첫글자는 대문자로
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}
 
 
 


register.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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">
 
<jsp:useBean id="member" class="kr.bit.Member"/>
<jsp:setProperty name="member" property="*"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    id : <jsp:getProperty property="id" name="member"/>
    이름 : <jsp:getProperty property="name" name="member"/>
    이메일 : <jsp:getProperty property="email" name = "member"/>
</body>
</html>



id : jsp페이지에서 자바빈 객체에 접근할 때 사용할 이름

class : 패키지 이름을 포함한 자바빈 클래스의 완전한 이름을 입력

scope : 자바빈 객체가 저장될 영역(page, request, session, application 중 하나 기본값 page)



반응형

'(Before)BitSchool' 카테고리의 다른 글

2014/06/03 XML  (0) 2014.06.03
2014/05/31 JSP - JDBC, connectionPool  (0) 2014.05.31
2014/05/29 Servlet, JSP  (0) 2014.05.29
2014/05/28 JSP - servlet, get방식, post방식  (0) 2014.05.28
2014/05/27 JSP  (0) 2014.05.27