aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes enthuware question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "enthuware question" Watch "enthuware question" New topic
Author

enthuware question

trivikram Kumar
Ranch Hand

Joined: Feb 21, 2005
Posts: 172
Which of these two is a valid JSP code fragment?

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

OR

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


Javainn<br /> <br />SCJP,SCWCD,SCBCD,<br />ICED (IBM-287,IBM-484,IBM-486)
IBM certified in DB2 V 8.1 Family Fundamentals (IBM 700)
Wei-ju Wu
Ranch Hand

Joined: Feb 16, 2005
Posts: 147
The first one is correct, that one would have the effect:



The code within the curly braces is an instance initializer which will be called when the servlet is created.

In the second case the code is not placed in curly braces so it stands where the compiler would expect a declaration - so it is illegal.


"The UrlyBird catches the certificate. And he's gonna FlyByNight"<br /> <br />SCJP 1.2/5.0, SCJD, SCBCD, SCWCD, SCEA
trivikram Kumar
Ranch Hand

Joined: Feb 21, 2005
Posts: 172
Thankyou Wei-ju Wu for your reply...
Can't we write the code (which you said illegal)in that way??...and can't it be called when MyServlet is called??
Should it definetely be defined in an initializer??
But your answer is perfectly correct.

Thanks,
Javainn
Wei-ju Wu
Ranch Hand

Joined: Feb 16, 2005
Posts: 147
One needs to imagine how the container will transform and handle your JSP: it will first try to transform it into a servlet class, placing scriptlets and expressions within _jsp_service() and jsp declarations outside of the service method, but as members of the servlet class.

Then you regard it as a regular Java class and just try to figure out if it is legal Java syntax and therefore, compilable.

You can try that by writing a simple class which does the creation and setting the value, in one case putting the code in curly braces, in the other without and try to compile it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enthuware question
 
Similar Threads
is it a valid jsp code
Hashtable allowing null values
Valid JSP code fragments
Enthuware mock doubt!!
Enthuware question: Jsp expression