• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question Bank ID 41-- JSP to Servlet Conversion Rule

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question asks the scope (local or class) and whether it is in the service method or in the servlet after
<% int i =0; %>
<% int i; %>
<%! int i; %>
are converted. Where do I find "the rules of conversion" from JSP to servlet? Thanks in advance.
 
Tim Duncan
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pages 56-58 of the JSP 1.2 spec discuss declarations and scriptlets.
My understanding (which may be faulty) is that declarations (eg: <%! int i; %>) create a variable at the point where the JSP is translated into servlet code (so it exists in the servlet), and that this variable is accessible, globally, at any point in the page.
The other two examples are scriptlets, and these are executed when the request is processed, so presumably any variables created will be local to the service method.
Note that scriptlets are combined in the order they appear, and since any variables declared are local they are not automatically initialized, so attempts to use a variable before it is given a value (eg: in the second example <% int i; %>) would lead to an error.

[This message has been edited by Tim Duncan (edited October 02, 2001).]
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Tim.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Declarations are declared outside the _jspService method. So, "<%! int i; %>" declares an instance variable. "<%! static int i; %>" declares a class variable. And "<%! public int doSomething() {return 1;} %>" declares a method.
  • Declarations within scriptlets are executed withing the _jspService method, and are therefore local variables, and as Tim said, they "are combined in the order they appear."
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again, Miftah.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic