Let's live hard

자바빈


반복적인 작업을 효율적으로 하기 위해 빈을 사용한다. 빈이란? JAVA언어의 데이터(속성)와 기능(메소드)으로 이루어진 클래스이다.


jsp페이지를 만들고, 액션태그를 이용하여 빈을 사용한다. 그리고 빈의 내부 데이터를 처리한다.


자바빈을 사용하기 위한 액션태그

<jsp:useBean id="student" class="com.ch.ex0429.Student" scope="page" />
cs
                  자바빈이름          클래스 이름                사용범위




scope

  • page : 생성된 페이지 내에서만 사용 가능
  • request : 요청된 페이지 내에서만 사용 가능
  • session : 웹브라우저의 생명주기와 동일하게 사용가 가능
  • application : 웹 어플리케이션 생명주기와 동일하게 사용 가능





자바빈 사용하기


input.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link href="../css/style.css" rel="stylesheet">
</head>
<body>
<!-- 사용자로부터 이름, 나이, 학년 학번을 입력받아 -->
<!--  -->
    <form action="st6.jsp">
    <table>
        <caption> 학생 입력창</caption>
        <tr>
        <td>이름</td>
        <td><input type="text" name="name" required="required"></td>
        </tr>
        <tr>
        <td>나이</td
        <td><input type="number" name="age" required="required" min="0" max="150"></td>
        </tr>
        <tr>
        <td>학년</td>
        <td><input type="number" name="grade" required="required" min="1" max="4"></td>
        </tr>
        <tr>
        <td>학번</td>
        <td><input type="text" name="studentNum" required="required"></td>
        </tr>
        <tr><td colspan="2">
        <input type="submit" value="입력">
        <input type="reset" value="취소">
    </table>
    </form>
</body>
</html>

cs임시저장 (...)






단계1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="st" class="com.ch.beanex.Student" scope="request"></jsp:useBean>
<!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>Insert title here</title>
</head>
<body>
    <jsp:setProperty name="st" property="name" value="<%=request.getParameter(\"name\") %>"/>
    <jsp:setProperty name="st" property="age" value="<%=Integer.parseInt(request.getParameter(\"age\"))%>"/>
    <jsp:setProperty name="st" property="grade" value="<%=Integer.parseInt(request.getParameter(\"grade\")) %>"/>
    <jsp:setProperty name="st" property="studentNum" value="<%=request.getParameter(\"studentNum\") %>"/>
    <jsp:forward page="stResult.jsp"/>
</body>
</html>
cs






단계2

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="st" class="com.ch.beanex.Student" scope="request"></jsp:useBean>
<!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>Insert title here</title>
</head>
<body>
    <jsp:setProperty name="st" property="name" param="name" />
    <jsp:setProperty name="st" property="age" param="age" />
    <jsp:setProperty name="st" property="grade" param="grade" />
    <jsp:setProperty name="st" property="studentNum" param="studentNum"/>
    <jsp:forward page="stResult.jsp"/>
</body>
</html>
cs


단계3


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<jsp:useBean id="st" class="com.ch.beanex.Student" scope="request"></jsp:useBean>
<jsp:setProperty name="st" property="*" />
<jsp:forward page="stResult.jsp"></jsp:forward>
</body>
</html>




댓글 로드 중…

블로그 정보

it를 공부하고 있습니다.

최근에 게시된 글