Topic: A jsp usebean question?

  Print this page

1.A jsp usebean question? Copy to clipboard
Posted by: didongusa
Posted on: 2004-09-05 20:35

Regarding following two pieces of code, what is the difference?

piece code 1:

<jsp:usebean id="myid" class="java.lang.StringBuffer" />
<% myid.append("abc"); %>
Myid is <%= myid %>

piece code 2:

<%{%>
<jsp:usebean id="myid" class="java.lang.StringBuffer" />
<% myid.append("abc"); %>
<%}%>
Myid is <%= myid %>

I am asking the difference how JSP engine translate the two pieces of code.

Thanks first...

2.Re:A jsp usebean question? [Re: didongusa] Copy to clipboard
Posted by: didongusa
Posted on: 2004-09-08 04:54

Is this so easy or you are lazy to answer?

I really need the answer, is there someone be kind to offer answer for this very basic but for me a confusing q.

Thanks...

3.Re:A jsp usebean question? [Re: didongusa] Copy to clipboard
Posted by: floater
Posted on: 2004-09-08 09:21

run both on the server and check the compiled java code to see the difference, the answer depends on what server and jdk you are using.

4.Re:A jsp usebean question? [Re: didongusa] Copy to clipboard
Posted by: didongusa
Posted on: 2004-09-09 05:32

To upstairs:

I don't think the JDK or JSP engine matters. I try two compiler, one is jasper-runtime and jasper-compiler like Tomcat's, and the other one ojsp-runtime and ojspc that's for Oracle JSP Compiler. The piece code 2 return the same error: they both say: the "myid" is unknown.

But if I modify the piece code 2 as follows:

<%{%>
<jsp:useBean id="myid" class="java.lang.StringBuffer" scope="page"/>
<% myid.append("abc"); %>
<%}%>
Myid is <%= (StringBuffer) pageContext.findAttribute("myid") %>

Then it passed and run as piece code 1.

When I compare both translated code, I couldn't find a reason why the piece code 2 is refused to compile. The "myid" is in the scope. What does the pair
<%{ %> <%}%> function in the translation.

Sorry for my stupidity, but I need the JSP engine's logic.

Thanks for all reply.

5.Re:A jsp usebean question? [Re: didongusa] Copy to clipboard
Posted by: floater
Posted on: 2004-09-09 12:02

That's a scope problem, either move <jsp:use...> outside the {, or move Myid... inside the }. I used the book Prof JSP, haven't done this kind of coding for two years. Should move the java code to a class, rather than here.

There could be a diff between jsp compilers, if my memory serves right, one of the ibm's is diff from one of tomcat's.

6.Re:A jsp usebean question? [Re: didongusa] Copy to clipboard
Posted by: lnsylt
Posted on: 2004-09-09 17:29

<%! java.lang.StringBuffer buffer=new java.lang.StringBuffer();%>
Buffer:<%=buffer.append("abc")%>

7.Re:A jsp usebean question? [Re: didongusa] Copy to clipboard
Posted by: didongusa
Posted on: 2004-09-10 05:57

With respect to above two floors.

However, I am not asking how to modify the code to pass the compilation.

I am asking how jsp engine think when they encounter the { } and the the reason it refuse "myid" in the last line.

The scope is the issue, and it confuse me because no books on my hand talks with it. I only refer:

The code is as follows:

<%{%>
<jsp:usebean id="myid" class="java.lang.StringBuffer" />
<% myid.append("abc"); %>
<%}%>
Myid is <%= myid %>

According to the theory, any scriptlet will go to _jspService(?,?).
In the above code, inside { }, so the "myid" is created within the _jspService() scope. "myid" is defined in the _jspService() scope in the translation time. The last line will require myid in the page scope in the translation, because "myid" is not defined yet, so the jsp engine rejected.

If I correct the last line as follows:

Myid is <%= (StringBuffer) pageContext.findAttribute("myid") %>

The jsp engine don't care about if the page has an attribute "myid" or not during the translation, so it let you pass.

Actually in the runtime, when "myid" was initialized, it definitely register with page scope, so we have the result as piece code 1.

In the piece code 1, I just directly declare "myid" as in the page scope, so the jsp engine has no reason to reject.

I just give my idea, and really not very sure the jsp engine's logic.
I need your confirmation about the process or give a different reason.

8.Re:A jsp usebean question? [Re: didongusa] Copy to clipboard
Posted by: sunzy19810705
Posted on: 2004-09-16 09:23

大哥啊,myid是个javaben啊,怎么能输出一个类啊?没听过吧?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923