이 블로그 검색

2011년 8월 23일 화요일

jsp에서 useBean 과 import 의 차이


--------------------------------- 질문 -------------------------------------------
---------------------------------------------------------------------------------

일전에 jsp 페이지 내에서 useBean으로 객체를 생성하는 것과 객체의 직접 생성에 관한 글을 어디선가 봤었는데 다시 찾아보려니 찾을 수가 없습니다 ㅠ.ㅠ
<%@ import="test.Test"%>
<jsp:useBean name="test" class="test.Test" scope="session"/>
이것과
<%@ import="test.Test"%>
<%
      Test test = new Test();
%>
이렇게 만드는 방식에서의 scope 에 관해서
혹시 일전의 링크 주소를 알고 계시거나 위에 관해서 설명해 주실수 있는 분 부탁드립니다.


-----------------------------------답변----------------------------------------------
-------------------------------------------------------------------------------------
첫번째 질문만 답 드리께요..제가 아는 선에서만요..
useBean으로 생성한거랑 new 해서 생성한거랑의 차이점을 소스로 한번 확인해보세요..

각각의 경우 .java로 변환된 work폴더아래의 jsp.java파일을 보시면 이부분이 틀릴겁니다.

useBean으로 객체생성하면 아래 소스가 _jspService라는 메소드에 추가되거든요..

jsp 라이프 사이클 보시면 _jspService는 알수 있으리라 생각합니다.

synchronized (pageContext) {
sb= (oil.common.board.GongiLogic)
pageContext.getAttribute("sb",PageContext.PAGE_SCOPE);
if ( sb == null ) {
_jspx_specialsb = true;
try {
sb = (oil.common.board.GongiLogic) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "oil.common.board.GongiLogic");
} catch (ClassNotFoundException exc) {
throw new InstantiationException(exc.getMessage());
} catch (Exception exc) {
throw new ServletException (" Cannot create bean of class "+"oil.common.board.GongiLogic", exc);
}
pageContext.setAttribute("sb", sb, PageContext.PAGE_SCOPE);
}
}

제 생각엔 scope를 쓸 수 있느냐..없느냐 차이 같습니다. 맞는지 아닌지 딴 분들도 의견 주셨음 합니다.

그 외엔 sb = (oil.common.board.GongiLogic) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "oil.common.board.GongiLogic");

이 코드가 뭔가 특별한 건지..아마 이 코드를 아시면 알고자 하시는 차이점을 정확히 아실수 있을것 같아요..
저도 궁금..

일단 scope을 쓸수 있느냐 없느냐의 차이고 뭐 jsp에서 지원해주는 기능 마다하고 new 해서 생성하는건 적절한 코딩에서 멀어지는 길이니...
그럼 수고하세요..

위처럼 객체를 생성시에 scope에 관해서 부탁드립니다.






---------------------------------------------------------------------------------------








----------------------------------- 질문 -----------------------------------------------
Ok on a Java Server Page I can use a function in two ways:
Either I declare
<jsp:useBean id="myBean" scope="session" class="mypack.mysubpack.MyClass" />
or I declare
<%@ page import = "mypack.mysubpack.MyClass" %>

In each case I can call a function later by coding

MyClass.myFunction();

So what is the difference between the two variantes?

Why are Beans invented if we don't need them because we have the import statement ?

Ulf



---------------------------------------------------------------------------답변-------------------------------------------------------------


(Ulf Meinhardt) writes:

> Ok on a Java Server Page I can use a function in two ways:
> Either I declare
> <jsp:useBean id="myBean" scope="session" class="mypack.mysubpack.MyClass" />


This checks if an object called "myBean" exists in the session
attributes, and if necessary creates and puts it there. It also adds a
variable called "myBean" that can be referenced later, in e.g.
<%=myBean.getFoo()%>.

> or I declare
> <%@ page import = "mypack.mysubpack.MyClass" %>


This does nothing more than add an import statement in the beginning
of the generated servlet.

> In each case I can call a function later by coding
>
> MyClass.myFunction();
>
> So what is the difference between the two variantes?


See above.

> Why are Beans invented if we don't need them because we have the
> import statement ?


Because beans aren't limited to what you artificially limit them to.
You can do much more with beans than just call static methods in their
classes.

댓글 없음:

댓글 쓰기