Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enthuware question: Jsp expression  RSS feed

 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I came across question: which of the following is valid JSP code:

1) <%!
Hashtable ht = new Hashtable();
ht.put("max", "10");
%>

2)<%!
Hashtable ht = new Hashtable();
{
ht.put("max", "10");
}
%>


Ans: 1-wrong , 2-right .

I tried testing too , got error for 1 (and 2 was okay).

[Explaination says: Instance initializer is okay to use .]

But howcome 1 giving problem? Please guide me.



Thanks
Vishal Chugh
 
cesar valencia
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
declarations are meant for instance variables. what you're doing in 1) is kinda like:



whereas 2) goes kinda like



thus, 2 is correct and 1 will fail to compile
 
vipin jain
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one explaine more?
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi cesar,

Sorry i dont get it , Could you please explain more?

I know <%! %> is used for declaring class variables and methods , I dint get that how using block {} makes it working (that also from inside JSP declarration) ? what was the problem without using block?

Thanks
Vishal Chugh
 
Vijitha Kumara
Bartender
Posts: 3974
24
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishal Chugh:
Hi cesar,

Sorry i dont get it , Could you please explain more?

I know <%! %> is used for declaring class variables and methods , I dint get that how using block {} makes it working (that also from inside JSP declarration) ? what was the problem without using block?

Thanks
Vishal Chugh


When using inside a block it become a instance block in the generated servlet. So you can put the code like in any other method. But without a block, those code ended up as they are,



in the instance/class member declaration level. They can only go inside a method or a block.., so it won't compile. It's pure java once you identify the type of JSP code (declaration in this case).
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for guiding but still .

Did you mean : " whatever code statements (except declarations of variables and methods ) will be there in JSP declaration cant come under JSP declararion itself ,so we getting the error in 1 option , so SOLUTION is : we have to enclose them explicitly using {} (if we are putting code statements under JSP decalration ) then only it will end up as something like initialization block code in core Java (OR other way is to bring the code in scriplet , dont keep it under JSP declaration )"

Is my understanding correct now? Please respond.

Thanks
Vishal Chugh
[ December 18, 2008: Message edited by: Vishal Chugh ]
 
Vijitha Kumara
Bartender
Posts: 3974
24
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your statement is unclear to me. But think it as a plain java class. What you are allowed to declare in the class?(instance/class variables,methods etc..) right? That's where the code inside the "<%! %>" ended up in the generated servlet.

OR other way is to bring the code in scriplet , dont keep it under JSP declaration


Yes that's fine since scriptlets ended up inside the service method of the generated servlet.
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry

I meant that code statements if they are present in the JSP declaration wont work ( And we are allowed to have only instance/class variables and methods only in JSP declaration , no code statements at all in JSP decaration ) ,

If we want to put the code statements in the JSP decaration <%! %>, we must enclose them under {} to make it working.

I got it, Thanks for your time..

Thanks
Vishal Chugh
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we want to put the code statements in the JSP decaration <%! %>, we must enclose them under {} to make it working.
And the reason was given earlier: initialization blocks. I'm not sure you quite understood that, and if that's the case see Initializing Fields in the Language Tutorials. That's really an SCJP question then though.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!