1 / 29

Java web programming

Java web programming. 2 일차. 자바빈 (JavaBean) 이란 ?. 자바빈은 프로퍼티 , 이벤트 , 지속성 등의 특징을 갖는 자바 객체로서 , 웹 어플리케이션에서는 프로퍼티만 갖는 자바빈이 주로 사용된다. 자바빈 프로퍼티. 프로퍼티는 자바빈에 저장되어 있는 값을 나타내며 , 메소드 이름을 사용해서 프로퍼티의 이름을 결정. Example. public void setMaxAge(int value) public int getMaxAge()

ghada
Télécharger la présentation

Java web programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java web programming 2일차

  2. 자바빈(JavaBean)이란? 자바빈은 프로퍼티, 이벤트, 지속성 등의 특징을 갖는 자바 객체로서, 웹 어플리케이션에서는 프로퍼티만 갖는 자바빈이 주로 사용된다. JSP 2.0 Programming

  3. 자바빈 프로퍼티 프로퍼티는 자바빈에 저장되어 있는 값을 나타내며, 메소드 이름을 사용해서 프로퍼티의 이름을 결정 Example public void setMaxAge(int value) public int getMaxAge() public boolean isFinished() public void setFinished(boolean finished) public int[] getMark() public void setMark(int[] values) public int getMark(int index) public void setMark(int value, int index) set, get을 제외한 나머지가 프로퍼티의 이름이다. 이 경우 maxAge가 프로퍼티의 이름이 된다. boolean 타입의 경우 get 대신에 is를 붙여도 된다. 배열을 프로퍼티 값의 타입으로 사용할 수도 있다. 배열의 개별 요소에 대해서도 값의 지정이 가능하다. JSP 2.0 Programming

  4. 자바빈 예제 Example public String getName() { return name; } public void setName(String val) { this.name = val; } public String getAddress() { return address; } public void setAddress(String val) { this.address = val; } public Timestamp getRegisterDate() { return registerDate; } public void setRegisterDate(Timestamp val) { this.registerDate = val; } public String getEmail() { return email; } public void setEmail(String val) { this.email = val; } } public class MemberInfo { private String id; private String password; private String name; private String address; private Timestamp registerDate; private String email; public String getId() { return id; } public void setId(String val) { this.id = val; } public String getPassword() { return password; } public void setPassword(String val) { this.password = val; } JSP 2.0 Programming

  5. 스크립트에서의 자바빈 객체의 사용 코드 Code <% MemberInfo mi = ....; // 객체 생성 %> 이름 - <%= mi.getName() %>, 아이디 - <%= mi.getId() %> // 객체 사용 • <%, <%= 그리고 %>를 사용함으로써 복잡한 JSP 코드 작성 • mi.getName() 등 자바 문법에 대한 이해 필요 JSP 2.0 Programming

  6. <jsp:useBean> 액션 태그: 자바빈 객체 생성 <jsp:useBean> 액션 태그를 사용해서 JSP에서 사용할 자바빈 객체를 생성한다. Syntax <jsp:useBeanid="[빈이름]" class="[자바빈클래스이름]" scope="[범위]" /> • id - JSP 페이지에서 자바빈 객체에 접근할 때 사용할 이름을 명시한다. • class - 패키지 이름을 포함한 자바빈 클래스의 완전한 이름을 입력한다. • scope - 자바빈 객체가 저장될 영역을 지정한다. page, request, session, application 중 하나를 값으로 갖는다. 기본값은 page 이다. Example <jsp:useBean id="info" class="madvirus.member.MemberInfo" scope="request" /> <% MemberInfo info = (MemberInfo)request.getAttribute("info"); if (info == null) { info = new MemberInfo(); request.setAttribute("info", info); } %> JSP 2.0 Programming

  7. <jsp:useBean> 액션 태그: scope 속성과 기본객체 scope 속성의 값에 따라서 생성한 자바빈 객체가 저장되는 기본 객체가 달라진다. • "page" - pageContext 기본 객체 • "request" - request 기본 객체 • "session" - session 기본 객체 • "application" - application 기본 객체 JSP 2.0 Programming

  8. <jsp:setProperty> 액션 태그: 프로퍼티 값 설정 <jsp:setProperty> 액션 태그를 사용해서 자바빈 프로퍼티의 값을 지정한다. Syntax <jsp:setPropertyname="[자바빈]" property="이름" value="[값]" /> • name - 프로퍼티의 값을 지정할 자바빈 객체의 이름. <jsp:useBean>액션 태그의 id 속성에서 지정한 값을 사용한다. • property - 값을 지정할 프로퍼티의 이름 • value - 프로퍼티의 값. 표현식을 사용할 수 있다. Example <jsp:useBean id="member" class="madvirus.member.MemberInfo" /> <jsp:setPropertyname="member" property="name" value="최범균" /> JSP 2.0 Programming

  9. <jsp:setProperty> 액션 태그: 파라미터값 지정 <jsp:setProperty> 액션 태그를 사용해서 파라미터의 값을 자바빈 프로퍼티의 값으로 지정할 수 있다. Example <jsp:setProperty name="member" property="id" param="memberid" /> → memberid 파라미터를 member 자바빈의 id 프로퍼티의 값으로 지정 <jsp:setProperty name="member" property="*" /> → 모든 파라미터에 대해서, 파라미터의 값을 member 자바빈의 같은 이름을 가진 프로퍼티의 값으로 지정 JSP 2.0 Programming

  10. <jsp:getProperty> 액션 태그: 프로퍼티 값 읽기 <jsp:getProperty> 액션 태그를 사용해서 자바빈 프로퍼티의 값을 출력한다. Syntax <jsp:getPropertyname="자바빈이름" property="프로퍼티이름" /> • name - <jsp:useBean>의 id 속성에서 지정한 자바빈 객체의 이름. • property - 출력할 프로퍼티의 이름 Example <jsp:useBean id="member" class="madvirus.member.MemberInfo" /> <jsp:getPropertyname="member" property="name" /> JSP 2.0 Programming

  11. 프로퍼티 타입에 따른 값의 매핑 JSP 2.0 Programming

  12. JSP 빈 만드는 방법 JSP에서 사용되는 빈 만들기 • 클래스 내의 멤버필드는 소문자로 시작 • 멤버필드에 값을 set하거나 get하는 메서드는 public • 값을 set하는 메서드의 이름은 set+ 대문자로 시작하는 멤버필드이름 • 값을 get하는 메서드의 이름은 get + 대문자로 시작하는 멤버필드이름 JSP빈의 규칙을 지키면서 만든 멤버필드와 멤버메서드 • str getStr(), setStr(String str) • id setId(int id) 일반적인 멤버메서드 • getContent()

  13. ActionTagTest.java package chap5; public class ActionTagTest{   private String str="";   public String getStr(){      return str;   }   public void setStr(String str){      this.str=str;    }   private String id="";   public void setId(String id){      this.id=id;    }   public String getContent(){     return id + ":" + str;   } }

  14. JSP빈 생성하는 방법-1 • new를 사용하여 생성하는 방법 NewTest.jsp <%@page import="chap5.*" %> <html><body><font size=5 color=blue> <% ActionTagTest t1=new ActionTagTest(); t1.setStr("JSP"); t1.setId("jabook"); out.print(t1.getStr()); %><br> <%= t1.getContent()%> </font></body></html>

  15. JSP빈 생성하는 방법-2 • jsp:useBean액션태그를 이용하는 방법 ActionTagTest.jsp <html><body><font size=5 color=blue> <jsp:useBean id="aTag" class="chap5.ActionTagTest" /> <jsp:setProperty name="aTag" property="str" value="Hello!"/> <jsp:setProperty name="aTag" property="id" value="jabook"/> <jsp:getProperty name="aTag" property="str"/><br> <jsp:getProperty name="aTag" property="content" /> </font></body></html>

  16. jsp:useBean의 활용 ActionTagTest.jsp <html><body><font size=5 color=blue> <jsp:useBean id="actTag" class="chap5.ActionTagTest" /> <jsp:setProperty name="actTag" property="str" value="MixTest" /> <jsp:getProperty name="actTag" property="str" /><br> <% actTag.setStr("Hi~!"); actTag.setId("domi"); out.print(actTag.getStr()); %> <jsp:getProperty name="actTag" property="content"/> </font></body></html>

  17. 액션태그를 이용한 초기화 ConScriptletTest.java package chap5; public class ConScriptletTest{ private String id=""; private String str=""; public String getContent(){ return id+":"+str; } public void setStr(String str){ this.str=str; } public void setId(String id){ this.id=id; } public ConScriptletTest(){ this.id="jabook"; this.str="not"; } public ConScriptletTest(String id, String str){ this.id=id; this.str=str; } }

  18. ConScriptletTest.jsp <%@page import="chap5.*" %> <html><body><font size=5 color=blue> <% String id=request.getParameter("id"); String str=request.getParameter("str"); ConScriptletTest t1=new ConScriptletTest(id,str); out.print(t1.getContent()); %><br> <% ConScriptletTest t2=new ConScriptletTest(); out.print(t2.getContent()); %> </font></body></html>

  19. ConScriptletTest.jsp <%@page import="chap5.*" %> <html><body><font size=5 color=blue> <% String id=request.getParameter("id"); String str=request.getParameter("str"); ConScriptletTest t1=new ConScriptletTest(id,str); out.print(t1.getContent()); %><br> <% ConScriptletTest t2=new ConScriptletTest(); out.print(t2.getContent()); %> </font></body></html>

  20. ConActionTest.jsp <html><body><font size=5 color=blue> <jsp:useBean id="cs1" class="chap5.ConScriptletTest"/> <jsp:getProperty name="cs1" property="content" /> <br> <jsp:useBean id="cs2" class="chap5.ConScriptletTest"> <jsp:setProperty name="cs2" property="*" /> </jsp:useBean> <jsp:getProperty name="cs2" property="content" /> </font></body></html>

  21. jsp:forward – 다른 페이지에 제어권 넘기기 pageContext의 forward, include와 jsp:forward • 호출한 페이지로 제어권 완전히 넘김 • 호출한 페이지로 이동할 때 실행중인 작업의 출력정보를 버리고 감 jsp:include • 호출한 페이지로 제어권 잠시 넘기고 다시 가져옴 • 호출한 페이지로 이동할 때 실행중인 작업 가지고 넘어감

  22. forward1.jsp <%@ page contentType= "text/html;charset = euc-kr"%> 처음 시작 페이지 입니다.<br> <h1>forward1.jsp의 Head</h1> <% String str = request.getParameter("name"); out.println(str);%> <jsp:forward page="forward2.jsp"/> <h1>forward1.jsp의 Tail</h1> ------------------------------------------------------------------------- forward2.jsp <%@ page contentType= "text/html;charset = euc-kr"%> forward2.jsp의 출력 : 나중에 만들어진 페이지입니다. 제어권이 넘어 왔습니다. <%=request.getParameter("name")%> <h1>forward2.jsp</h1>

  23. jsp:include – 다른 페이지에 제어권 빌려주기 include1.jsp <%@ page contentType= "text/html;charset = euc-kr"%> 처음 시작 페이지 입니다.<br> <h1>include1.jsp의 Head</h1> <% String str = request.getParameter("name"); out.println("include1.jsp의 출력:"+str+"<br><br>"); %> <jsp:include page="include2.jsp"/> <h1>include1.jsp의 Tail</h1>

  24. include2.jsp <%@ page contentType= "text/html;charset = euc-kr"%> include2.jsp의 출력 : 나중에 만들어진 페이지입니다. 제어권이 넘어 왔습니다. <%=request.getParameter("name")%> <h1>include2.jsp</h1>

  25. jsp:forward – 매개변수 포함하기 <jsp:forward page="path"> <jsp:param name= value=/> </jsp:forward> forward3.jsp?name=love&test=good <%@ page contentType= "text/html;charset = euc-kr"%> 처음 시작 페이지 입니다.<br> <h1>forward3.jsp의 Head</h1> <jsp:forward page="forward4.jsp"> <jsp:param name = "name" value="jabook"/> </jsp:forward> <h1>forward3.jsp의 Tail</h1>

  26. forward4.jsp <%@ page contentType= "text/html;charset = euc-kr"%> forward4.jsp의 출력 : 나중에 만들어진 페이지입니다. <br> 제어권이 넘어 왔습니다. <br> <%=request.getParameter("name")%><br> <%=request.getParameter("test")%> <h1>forward4.jsp</h1>

  27. jsp:include – 매개변수 포함하기 <jsp:include page="path"> <jsp:param name= value=/> </jsp:include>

  28. include3.jsp?name=love&test=good <%@ page contentType= "text/html;charset = euc-kr"%> 처음 시작 페이지 입니다.<br> <html> <body> <h1>include3.jsp의 Head</h1> <% out.println(request.getParameter("name")+"<br>"); out.println(request.getParameter("test")+"<br>"); %> <jsp:include page="include4.jsp"> <jsp:param name = "name" value="jabook"/> </jsp:include> <%=" 제어권이 돌아 왔습니다. : <br>"%> <%= request.getParameter("name")+"<br>"%> <%= request.getParameter("test")+"<br>"%> <h1>include3.jsp의 Tail</h1> </body> </html>

  29. include4.jsp <%@ page contentType= "text/html;charset = euc-kr"%> <br>include4.jsp의 출력 : 나중에 만들어진 페이지입니다. <br> 제어권이 넘어 왔습니다. <br> <%=request.getParameter("name")%><br> <%=request.getParameter("test")%> <h1>forward4.jsp</h1>

More Related