Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Question Bank ID 41-- JSP to Servlet Conversion Rule

 
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.
 
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.
 
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.
 
Bring out your dead! Or a tiny ad:
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic