• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it not possible to use System class inside JSP declarations?  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Please see the code posted below



When i put System.out.println inside Declaration , the code isn't being compiled .

Please tell me , why is it not possible to use System class inside JSP Declarations .

Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you're just trying this to see how scriptlets work. You are using a declaration here :

This won't compile.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you're just trying this to see how scriptlets work.


No , my question is ,

inside scriptlet is working fine , but not in declarations .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:
inside scriptlet is working fine , but not in declarations .


Declarations are used to declare variables. int i = 7; String name = "Ravi";. Things like that. You're line of code is a scriptlet, not a declaration.
 
Pravin Shirke
Ranch Hand
Posts: 152
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi Kiran V,
when you write a jsp and put declaration and scriptlets and expression in your jsp page. try to find out where all this goes when the jsp is converted into servlet at translation time. you will get your answers there itself.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that the Scriplets will be placed under the _service method and declarations will be placed as instance variables .

correct me if i am wrong .

Here in this case as the code isn't being compiled , i cant see this code under servlet . so please share your knowledge .

Thanks in advance
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declarations are for declarations... Declarations are not scriptlets. It's not going to compile unless you remove that erroneous declaration. Here is a declaration :
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for this .

Declarations are for declarations...


But is there any thumb rule taht what values will be allowed inside the JSP Declarations .

Thanks in advance .
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "thumb rule" is that you can put declarations there. Perhaps you need to move on to the next question, which apparently is "What are declarations?" You might consult your beginning Java text for that.
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really shouldn't use System.out or System.err in webapps at all. These channels aren't designed for webapps. Or for that matter, for applets or anything other than Applications. There's no telling where the output will go. Assuming it doesn't just get eaten.

In actual practice on webapps, System.out and System.err output usually goes to a console. However, that can vary depending on not only what appserver product you're using, but on how the product has been customized for the OS platform it's running on.

For writing out to web pages, there's a separate output channel defined as a built-in variable to the JSP context. However, I should point out that putting executable code on JSPs is not something that scales well. For any decent-sized webapp, you should be considering some sort of Model/View/Controller infrastructure. JSPs are Views, but using scriptlets as Controller functions breaks the Separation of Concerns design strategy. And more to the point, swiftly becomes a maintenance nightmare full of JSP files.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:But is there any thumb rule taht what values will be allowed inside the JSP Declarations .Thanks in advance .

If you understand that what you put in a JSP declaration (<%! %>) will be translated into the servlet instance variables, you can figure out what is allowed in there.

In your case, the following :

will be translated into something looking like this :

You see, the JSP declaration becomes a servlet instance variable, which won't compile here. Something like <%! String s = "Hi Ravi"; %> would.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe , that was really nice explanation . Thanks for your time .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!