• Post Reply Bookmark Topic Watch Topic
  • New Topic

scriplet VS declaration

 
lavnish lalchandani
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in jsp
<%
int i;
system.out.println(i):
// this goes to service emthod of interface right ? so this i will be loal var
%>
Its compile time error. "Variable i may not have been initialized."

IF
<%!
int i;
system.out.println(i):
// this goes to init() method of servlet right ? so this i will be local var
%>
then it will compile and run fine.

I am not sure why ? as per my understanding both will be loacl metho of servlet class
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lavnish lalchandani wrote:
// this goes to init() method of servlet right ? so this i will be local var

No. It gets placed at class level.

Neither of scriptlets or scriptlet declarations should be used in new JSPs.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First one goes to the service method, while second one goes directly as class variable. I guess you can't use System.out.println() in declarations.

P.S. As previous post says, try not to use scriptlets in JSPs. Everything i've done till now that concerns View (from mVc) could be done with JPS + JSTL + EL. The code will be much more cleaner and correct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!