• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp code compilation error

 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I met a question like this:

Given the code fragment
<% x=10; %>
<% int x=5; %>
<%! int x; %>
x= <%=page.x %>
What will the result of accessing this jsp page?

The answer explains it will result in compiler error as page.x is invliad. But isn't the order of using variable x is wrong as well? Wouldn't compiler first complaint the use of x first?

Please help, thanks.
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not reall because the first two statements come under the _jspService() methods while the other one which is a delaration is outside the service() method and is an instance varilable. so it basically assigns a value to the instance variable inside the _jspService().
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should compile just fine.

But the result may differ from one JSP engine to other. Because it depends on how translator (JSP --> Servlet class) works. It may differ from one JSP engine to other.

For Example,

In generated servlet class x=10; may come before int x = 5; ==> result will be 10.

But what if, x=10; comes after int x=5; in generated servlet class ==> here shadowing happens and result will be 0.

HTH,
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the thing is the implicit object page in JSP's refer to object. but since there is no object of called x it will give an error at translation time. the order of the elements is just fine and nothing wrong there. also there wont be any shadowing happening in between.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!