(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-8242763509535969", enable_page_level_ads: true }); '분류 전체보기' 카테고리의 글 목록 (2 Page) :: 깜냥깜냥

 


getter, setter,getProperty ,setProperty를 이용한 자바 빈즈 예제 입니다.



위처럼 package를 만들어 주세요.


BeanTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package test;
 
public class BeanTest {
    private String name = "honggildong";
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
}
 

 


BeanTest.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 <jsp:useBean id="beantest" class="test.BeanTest" scope="page" />
<%--자바빈 객체의 멤버변수를 직접 수정하는 것이 아니라 setter호출
    <jsp:setProperty name="beantest" property="name" value="BeanTest!"/>--%>
<!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>JavaBean Test</title>
</head>
<body>
    <b>자바빈 사용 예제</b>
     <h3><%=beantest.getName()%></h3>
    <%beantest.setName("shallot");%>
     <h3><%=beantest.getName()%></h3>
 
    <%--자바빈 객체의 멤버변수를 접근 하는 것이 아니라 getter 호출--%>
    <jsp:setProperty name="beantest" property="name" value="sleep!"/>
<h3><jsp:getProperty name="beantest" property="name"/></h3> 
</body>
</html>



class="test.BeanTest"는 test 패키지 안에 있는 말이겠죠?



Application.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
41
42
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("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>Application.jsp</title>
</head>
<body>
    <center>
        <h2>영역과 속성 테스트</h2>
        <form name="f1" method="post" action="Session.jsp">
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <td colspan="2" align="center">Application 영역에 저장할 내용들</td>
                </tr>
                <tr>
                    <td>이름</td>
                    <td><input type="text" name="username" style="border: 0px;"
                        size="20"></td>
                </tr>
                <tr>
                    <td>아이디</td>
                    <td><input type="text" name="id" style="border: 0px;"
                        size="20"></td>
                </tr>
                <tr>
                    <td>패스워드</td>
                    <td><input type="text" name="pass" style="border: 0px;"
                        size="20"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="전송"></td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

Session.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
41
42
43
44
45
46
47
48
49
50
51
52
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("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>Session.jsp</title>
</head>
<body>
    <center>
        <h2>영역과 속성 테스트</h2>
        <%    String username=request.getParameter("username");
            String id=request.getParameter("id");
            String pass = request.getParameter("pass");
            application.setAttribute("username", username);
            application.setAttribute("id", id);
            
        %>
        <h3>${username}님
            반갑습니다.<br> ${username}님의 아이디는 ${id}입니다.
        </h3>
 
        <form name="f2" method="post" action="Save.jsp?pass=<%=pass%>">
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <td colspan="2" align="center">Session 영역에 저장할 내용들</td>
                </tr>
                <tr>
                    <td>e-mail 주소</td>
                    <td><input type="text" name="email" style="border: 0px;"
                        size="30"></td>
                </tr>
                <tr>
                    <td>집주소</td>
                    <td><input type="text" name="address" style="border: 0px;"
                        size="40"></td>
                </tr>
                <tr>
                    <td>전화번호</td>
                    <td><input type="text" name="tel" style="border: 0px;"
                        size="20"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="전송"></td>
                </tr>
            </table>
        </form>
</body>
</html>

Save.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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("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>Save.jsp</title>
</head>
<body>
    <center>
        <h2>영역과 속성 테스트</h2>
        <%
            String email = request.getParameter("email");
            String address = request.getParameter("address");
            String tel = request.getParameter("tel");
            String pass = request.getParameter("pass");
            application.setAttribute("username", application.getAttribute("username"));
        %>
        <%
            application.setAttribute("email", email);
            application.setAttribute("address", address);
            application.setAttribute("tel", tel);
        %>
        ${username}님의 정보가 모두 저장되었습니다.<br> <br> <a
            href="Check.jsp?pass=<%=pass%>">확인하러 가기</a>
    </center>
</body>
</html>

check.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("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>Check.jsp</title>
</head>
<body>
    <center>
        <h2>영역과 속성 테스트</h2>
        <br> <br>
        <%
            application.setAttribute("username", application.getAttribute("username"));
            application.setAttribute("id", application.getAttribute("id"));
            session.setAttribute("email", session.getAttribute("email"));
            session.setAttribute("address", session.getAttribute("address"));
            session.setAttribute("tel", session.getAttribute("tel"));
            String pass = request.getParameter("pass");
        %>
        <form>
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <td colspan="2" align="center">Application 영역에 저장할 내용들</td>
                </tr>
                <tr>
                    <td>이름</td>
                    <td>${username}</td>
                </tr>
                <tr>
                    <td>아이디</td>
                    <td>${id}</td>
                </tr>
                <tr>
                    <td>패스워드</td>
                    <td><%=pass %></td>
                </tr>
            </table>
        </form>
        <br> <br>
        <form>
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <td colspan="2" align="center">Session 영역에 저장할 내용들</td>
                </tr>
                <tr>
                    <td>e-mail 주소</td>
                    <td>${email}</td>
                </tr>
                <tr>
                    <td>집주소</td>
                    <td>${address}</td>
                </tr>
                <tr>
                    <td>전화번호</td>
                    <td>${tel}</td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>


request는 get 방식에서 가져오기 힘듭니다! 그래서 action이나 경로에서 표시를 해줘야합니다.


 



Top.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("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>top.jsp</title>
</head>
<body>
    Login | Join
</body>
</html>
 

left.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"%>
<%
    request.setCharacterEncoding("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>left.jsp</title>
</head>
<body>
    <center>
        <a href="./template.jsp?page=newitem">신상품</a> <br>
        <a href="./template.jsp?page=bestitem">인기상품</a>
    </center>
</body>
</html>

newitem.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"%>
<%
    request.setCharacterEncoding("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>newitem.jsp</title>
</head>
<body>
    <center>
        <h2>신상품 목록입니다.</h2>
    </center>
</body>
</html>

bestitem.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ 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>bestitem.jsp</title>
</head>
<body>
    <center>
        <h2>인기상품 목록입니다.</h2>
    </center>
</body>
</html>

bottom.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"%>
<%
    request.setCharacterEncoding("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>bottom.jsp</title>
</head>
<body>
 
    <center>Since 2017</center>
 
</body>
</html>

template.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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("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>template.jsp</title>
</head>
<body>
    <%
        String pagefile = request.getParameter("page");
        if (pagefile == null) {
            pagefile = "newitem";
        }
    %>
    <form name="f1" method="get">
        <table border="1" cellpadding="15" cellspacing="0" width="800"
            height="300">
            <tr>
                <td colspan="2"><jsp:include page="top.jsp"></jsp:include></td>
            </tr>
            <tr>
                <td><jsp:include page="left.jsp"></jsp:include></td>
                <td width="650"><jsp:include page='<%=pagefile + ".jsp"%>'></jsp:include></td>
            </tr>
            <tr>
                <td colspan="2"><jsp:include page="bottom.jsp"></jsp:include></td>
            </tr>
        </table>
    </form>
</body>
</html>


include 액션 태그를 이용하여 페이지를 합쳐줍니다.


template에서 실행한 첫 화면입니다.

인기 상품을 눌렸을 때의 화면입니다. 인기 상품을 눌리면 주소값에 '?page=bestitem'이 추가 되신 걸 볼 수 있습니다.


이 화면은 신상품을 눌렸을 때의 화면입니다. 인기 상품을 눌리면 주소값에 '?page=newitem'이 추가 되신 걸 볼 수 있습니다.


여기서 가장 중요한 건  left.jsp와 template.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
41
<%@ 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>request test</title>
<script language="javascript">
    document.cookie = "test=OK.";
</script>
 
</head>
<body>
    <h2 align="center">request 테스트 폼</h2>
    <hr>
    <form name="f1" action=01_1.jsp method=post>
        <table border="1" cellpadding="10" cellspacing="0" align="center">
            <tr>
                <td>이름</td>
                <td><input type="text" name=name width="15" size="10"></td>
            </tr>
            <tr>
                <td>직업</td>
                <td><select name="job"><option selected>학생</option>
                        <option>회사원</option>
                        <option>주부</option></select></td>
            </tr>
            <tr>
                <td>관심분야</td>
                <td><input type="checkbox" name="inter" value="정치">정치 <input
                    type="checkbox" name="inter" value="사회">사회 <input
                    type="checkbox" name="inter" value="정보통신">정보통신</td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="확인"
                    name="B1"> <input type="reset" value="취소" name="B2">
            </tr>
        </table>
    </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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("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></title>
</head>
<body>
    <h2 align="center">request 테스트 결과 - 1</h2>
    <hr>
    <table border=1 cellpadding="10" cellspacing="0" align="center">
        <tr>
            <td>이름</td>
            <td><%=request.getParameter("name")%></td>
        </tr>
        <tr>
            <td>직업</td>
            <td><%=request.getParameter("job")%></td>
        </tr>
        <tr>
            <td>관심분야</td>
            <td>
                <%
                    String a1[] = request.getParameterValues("inter");
                    for (int i = 0; i < a1.length; i++) {
                        out.println(a1[i] + "<br>");
                    }
                %>
            </td>
        </tr>
    </table>
    <hr>
    <center>
        <h2>request 테스트 결과 -2</h2>
        <table>
            <tr>
                <td>1.클라이언트 IP 주소: <%=request.getRemoteAddr()%><br>
                2.요청메서드: <%=request.getMethod()%><br>
                <%Cookie cookie[] = request.getCookies();%>
                3.<%=cookie[0].getName()%>에 설정된 쿠기값: <%=cookie[0].getValue()%><br>
                4.프로토콜:<%=request.getProtocol()%></td>
            </tr>
        </table>
    </center>
</body>
</html>




get 방식에서 한글이 깨진다면 
C:\Program Files\Apache Software Foundation\Tomcat 8.0\conf의 server.xml 파일 중

1
2
3
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
에서 

1
2
3
<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" URIEncoding="UTF-8" />
즉, URIEncoding="UTF-8" 이나 URIEncoding="EUC-KR"을 추가해주시면 됩니다.


+ Recent posts