这是一个困扰我很久的问题了,一直不明白<jsp:useBean>在jsp文件转为servlet时是怎样编译的,今天找到答案了,高兴,呵呵,请看:

JSP中正确应用类:

应该把类当成JAVA BEAN来用,不要在<% %> 中直接使用. 如下的代码(1)经过JSP引擎转化后会变为代码(2):
从中可看出如果把一个类在JSP当成JAVA BEAN 使用,JSP会根据它的作用范围把它保存到相应的内部对象中.
如作用范围为request,则把它保存到request对象中.并且只在第一次调用(对象的值为null)它时进行实例化. 而如果在<% %>中直接创建该类的一个对象,则每次调用JSP时,都要重新创建该对象,会影响性能.

代码(1)
<jsp:useBean id="test" scope="request" class="demo.com.testdemo">
</jsp:useBean>

<%
test.print("this is use java bean");

testdemo td= new testdemo();
td.print("this is use new");
%>

代码 (2)
demo.com.testdemo test = (demo.com.testdemo)request.getAttribute("test");
if (test == null)
{
try
{
test = (demo.com.testdemo) java.beans.Beans.instantiate(getClass().getClassLoader(),"demo.com.testdemo");
}
catch (Exception _beanException)
{
throw new weblogic.utils.NestedRuntimeException("cannot instantiate 'demo.com.testdemo'",_beanException);
}
request.setAttribute("test", test);
out.print("\r\n");
}
out.print("\r\n\r\n\r\n");
test.print("this is use java bean");

testdemo td= new testdemo();
td.print("this is use new");

原来指定作用范围是这么一回事