๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

JSP ์•ก์…˜ ํƒœ๊ทธ์™€ jsp:useBean ์‚ฌ์šฉ ์˜ˆ์ œ

by Leica 2020. 2. 13.
๋ฐ˜์‘ํ˜•

JSP Action์ด๋ž€?

JSP์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํƒœ๊ทธ๋“ค์˜ ์ง‘ํ•ฉ์„ 'JSP Action'์ด๋ผ ํ•œ๋‹ค.

JSP Action์€ ๊ฐ ํƒœ๊ทธ์— ๋งž๋Š” ์ž๋ฐ” ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

์ฆ‰ JSP Action ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ์Šคํฌ๋ฆฝํŠธ๋ฆฟ <% %>์— ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋  ์ผ์ด๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด JSP Action์€ ์™œ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ผ๊นŒ?

 

JSP Action ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•  ์ค„ ์•ˆ๋‹ค๋ฉด ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ JSP๋กœ ์›น ํŽ˜์ด์ง€๋ฅผ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋‹ค.

๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ํ™”๋ฉด์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์„ ๋‚˜๋ˆ ์„œ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ธฐ์ˆ ์ด๋‹ค.

 

๋‹ค์Œ์€ JSP 2.2์—์„œ ์ œ๊ณตํ•˜๋Š” Standard Action์ด๋‹ค.

 

์•ก์…˜ ์„ค๋ช…
<jsp:useBean> ์ž๋ฐ” ์ธ์Šคํ„ด์Šค๋ฅผ ์ค€๋น„ํ•œ๋‹ค. ๋ณด๊ด€์†Œ์—์„œ ์ž๋ฐ” ์ธ์Šคํ„ด์Šค๋ฅผ ๊บผ๋‚ด๊ฑฐ๋‚˜ ์ž๋ฐ” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด ๋ณด๊ด€์†Œ์— ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
(์ž๋ฐ” ์ธ์Šคํ„ด์Šค = Java Bean)
<jsp:setProperty> ์ž๋ฐ” ๋นˆ์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.
์ž๋ฐ” ๊ฐ์ฒด์˜ settet()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
<jsp:getProperty> ์ž๋ฐ” ๋นˆ์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๊บผ๋‚ธ๋‹ค.
์ž๋ฐ” ๊ฐ์ฒด์˜ getter()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋‘๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
<jsp:include> ์ •์ (HTML, ํ…์ŠคํŠธ ํŒŒ์ผ ๋“ฑ) ๋˜๋Š” ๋™์ (์„œ๋ธ”๋ฆฟ/JSP) ์ž์›์„ includingํ•˜๋Š” ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
<jsp:forward> ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์‹คํ–‰์„ ๋ฉˆ์ถ”๊ณ  ๋‹ค๋ฅธ ์ •์ , ๋™์  ์ž์›์œผ๋กœ forwardingํ•˜๋Š” ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
<jsp:param> jsp:include, jsp:forard, jsp:params์˜ ์ž์‹ ํƒœ๊ทธ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
ServletRequest ๊ฐ์ฒด์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
<jsp:plugin> OBJECT ๋˜๋Š” EMBED HTML ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
<jsp:element> ์ž„์˜์˜ XML ํƒœ๊ทธ๋‚˜ HTML ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

<jsp:useBean>

<jsp:useBean>์€ application, session, request, page ๋ณด๊ด€์†Œ์— ์ €์žฅ๋œ ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ ๋ณด๊ด€์†Œ์— ์ €์žฅ๋œ ๊ฐ์ฒด๊ฐ€ ์—†๋‹ค๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์—ฌ ํ•ด๋‹น ๋ณด๊ด€์†Œ์— ์ €์žฅํ•œ๋‹ค.

 

<jsp:useBean> ๋ฌธ๋ฒ•

<jsp:useBean id="์ด๋ฆ„" scope="page | request | session | application" class="ํด๋ž˜์Šค๋ช…" type="ํƒ€์ž…๋ช…" />

 

<jsp:useBean> ์‚ฌ์šฉ ์˜ˆ

1
2
3
4
<jsp:useBean id="members"
             scope="request"
             class="java.util.ArrayList"
             type="java.util.ArrayList<spms.vo.Member>"/>
cs

์œ„ JSP ์ฝ”๋“œ๋Š” ๋‹ค์Œ ์ž๋ฐ” ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

1
2
3
4
5
java.util.ArrayList<spms.vo.Member> members = (java.util.ArrayList<spms.vo.Member>) request.getAttribute("members");
if(members = null) {
    members = new java.util.ArrayList();
    request.setAttribute("members", members);
}
cs

 

<jsp:useBean> - id ์†์„ฑ

๊ฐ์ฒด์˜ ์ด๋ฆ„์„ ์„ค์ •ํ•œ๋‹ค. ์ด ์ด๋ฆ„์€ 3๊ตฐ๋ฐ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

โ‘  ๊บผ๋‚ธ ๊ฐ์ฒด์˜ ์ฐธ์กฐ ๋ณ€์ˆ˜ ์ด๋ฆ„

โ‘ก getAttribute()๋กœ ๊ฐ’์„ ๊บผ๋‚ผ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฆ„

โ‘ข ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๊ฒฝ์šฐ ๋ณด๊ด€์†Œ์— ์ €์žฅํ•˜๋Š” key๊ฐ’์˜ ์ด๋ฆ„

 

<jsp:useBean> - scope ์†์„ฑ

๋ณด๊ด€์†Œ๋ฅผ ์ง€์ •ํ•œ๋‹ค. page(๊ธฐ๋ณธ๊ฐ’), request, session, application ์ค‘ ํ•˜๋‚˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • page = JspContext(๊ธฐ๋ณธ๊ฐ’)
  • request = ServletRequest
  • session = HttpSession
  • application = ServletContext

 

<jsp:useBean> - class ์†์„ฑ

class ์†์„ฑ์€ ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค ์ด๋ฆ„์„ ์ง€์ •ํ•œ๋‹ค.

๋ฐ˜๋“œ์‹œ ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ํฌํ•จํ•ด์•ผํ•œ๋‹ค. (QName)

new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์˜ฌ ์ˆ˜ ์—†๋‹ค.

scope์—์„œ ์ง€์ •ํ•œ ๋ณด๊ด€์†Œ์—์„œ ๊ฐ์ฒด๋ฅผ ์ฐพ์ง€ ๋ชปํ•  ๊ฒฝ์šฐ class์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” scope ๋ณด๊ด€์†Œ์— ์ž๋™์œผ๋กœ ์ €์žฅ๋œ๋‹ค.

class ์†์„ฑ์ด ์—†์œผ๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.

 

<jsp:useBean> - type ์†์„ฑ

type ์†์„ฑ์—๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•  ํƒ€์ž…(ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค)์„ ์ง€์ •ํ•œ๋‹ค.

๋ฐ˜๋“œ์‹œ ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ํฌํ•จํ•œ QName์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

type ๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด class ์†์„ฑ์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

<jsp:useBean> ์‚ฌ์šฉ ์˜ˆ์ œ

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์„ ์ถœ๋ ฅํ•˜๋Š” 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
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ํ•™์ƒ ๋ชฉ๋ก</title>
</head>
<body>
<jsp:include page="/Header.jsp"/>
<h1>ํ•™์ƒ ๋ชฉ๋ก</h1>
<p><a href="add">์‹ ๊ทœ ํ•™์ƒ</a></p>
<%
    List<Student> students = (ArrayList<Student>)request.getAttribute("students");
    for(Student student: students) {
%>
<%=student.getStudentNo()%>,
<%=student.getDepartment()%>,
<a href="update?student_no=<%=student.getStudentNo()%>"><%=student.getStudentName()%></a>,
<%=student.getGrade()%>,
<%=student.getGender()%>,
<%=student.getAge()%>,
<%=student.getPhoneNumber()%>,
<%=student.getAddress()%><br>
<%
    }
%>
<jsp:include page="/Tail.jsp"/>
</body>
</html>
cs

 

์ด๋ฅผ <jsp:useBean>์„ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. (13๋ผ์ธ์ด ์ถ”๊ฐ€๋จ)

 

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
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ํ•™์ƒ ๋ชฉ๋ก</title>
</head>
<body>
<jsp:include page="/Header.jsp"/>
<h1>ํ•™์ƒ ๋ชฉ๋ก</h1>
<p><a href="add">์‹ ๊ทœ ํ•™์ƒ</a></p>
<jsp:useBean id="students" scope="request" class="java.util.ArrayList" type="java.util.List<com.atoz_develop.spms.vo.Student>" />
<%
    // <jsp:useBean> ์‚ฌ์šฉ(์ƒ๋‹จ ์ฝ”๋“œ)ํ•˜์—ฌ ์ฃผ์„์ฒ˜๋ฆฌ
//    List<Student> students = (ArrayList<Student>)request.getAttribute("students");
    for(Student student: students) {
%>
<%=student.getStudentNo()%>,
<%=student.getDepartment()%>,
<a href="update?student_no=<%=student.getStudentNo()%>"><%=student.getStudentName()%></a>,
<%=student.getGrade()%>,
<%=student.getGender()%>,
<%=student.getAge()%>,
<%=student.getPhoneNumber()%>,
<%=student.getAddress()%><br>
<%
    }
%>
<jsp:include page="/Tail.jsp"/>
</body>
</html>
cs

 

๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ ๋‹ค์Œ์€ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” JSP์ด๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    Student student = (Student) session.getAttribute("student");
%>
<div style="background-color: darkcyan; color: aliceblue; height: 20px; padding: 5px;">
    SPMS
    <% if(student.getStudentNo() != null) { %>
    <span style="float: right;"><%=student.getStudentName()%>
        <a style="color: white;" href="<%=request.getContextPath()%>/auth/logout">๋กœ๊ทธ์•„์›ƒ</a>
    </span>
    <% } %>
</div>
cs

 

<jsp:useBean>์„ ์‚ฌ์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="student" scope="session" class="com.atoz_develop.spms.vo.Student" />
<%
//    Student student = (Student) session.getAttribute("student");
%>
<div style="background-color: darkcyan; color: aliceblue; height: 20px; padding: 5px;">
    SPMS
    <% if(student.getStudentNo() != null) { %>
    <span style="float: right;"><%=student.getStudentName()%>
        <a style="color: white;" href="<%=request.getContextPath()%>/auth/logout">๋กœ๊ทธ์•„์›ƒ</a>
    </span>
    <% } %>
</div>
cs

 

์‚ฌ์‹ค ์‹ค์ œ ํ™”๋ฉด ๊ฐœ๋ฐœ์€ JavaScript ๊ธฐ๋ฐ˜ ๊ธฐ์ˆ ์ด ๋งŽ์ด ํ™œ์šฉ๋œ๋‹ค.

JSP ๊ธฐ์ˆ ์— ์ข…์†๋˜๊ธฐ๋ณด๋‹ค๋Š” ์„œ๋ฒ„ ์ธก ๊ธฐ์ˆ ์— ๋…๋ฆฝ์ ์ธ ํ‘œ์ค€ ์›น ๊ธฐ์ˆ ์„ ๋ฐฐ์šฐ๊ณ  ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€