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
Joined: Feb 21, 2005
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.
Joined: Feb 16, 2005
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com