> lol.. u sound like u have had a bad experience with declaration blocks :)
Absolutely. I've seen too many people use <%! %> tags to declare variables
e.g.
It was absolutely a stupid move of the people who wrote the JSP specs to
call <%! %> a 'declaration'. I know what they mean and in some way they are
correct, but it is easil misunderstood.
e.g. I read an article in java developers journal (which is supposed to be
one of the biggest about Java isn't it?) about JSP-JDBC. For 1 it's a bad
thing to do your JDBC code inside a JSP itself, but ok, I can live with
that.
The trouble was that in this article he used code like:
<%! Connection conn = null;
Statement stmt =null;
ResultSet rs = null;
%>
bla bla
<% conn = getConnection ... and all JDBC stuff
%>
Now listen everybody: DON'T EVER DO THIS !!!!!!!!!!
Don't declare variables in a 'declaration' (I know, there are exceptions,
but for now, simply DON'T ;-)!
The big problem is that your code will run fine. (now how's that ;-)
But when you move it to a production server, with multiple concurrent
requests, everything is messed up bad. You only have 1 statement and 1
ResultSet for the whole servlet, which means that all concurrent requests
will use the same ResultSet instance!!!
As long as you don't do transactional stuff, sharing the Connection might
not be the best idea, but isn't really a terrible thing either. But a Shared
ResultSet means trouble!
It means that your results might show up in my browser. Get the idea?
> "A declaration has translation unit scope, so it is valid in the JSP page
> and any of its static include files. A static include file becomes part of
> the source of the JSP page and is any file included with an include
> directive or a static resouce included with a <jsp:include> element. The
> scope of a declaration does not include dynamic resources included with
> <jsp:include>." - jsp reference guide (link that i included earlier)
Why using static includes?
why not dynamic?
What are you using your 'declarations' for.
- Declaring variables? Well, don't. see my example above.
- functions? Well, don't ;-) create a separate java class for that code. You
might simply use static functions.
> You can declare the same thing in a scriplet but it wont be available to
> anything but that immediate page. What a pain especially if ur using
> something like tiles or a tiles like structure.
Like I already said: use dynamic includes.
Check out custom tags.
> Everyone should know what the difference between
> scriplets/declarations/and
> expressions is.
> If your not a newbie and you dont know exactly what a declaration block is
> then you shouldnt be coding JSP pages.
> (sorry if that sounded rude. it's early and i cant think of a more tactful
> way to say that)
No, you're wrong ;-)
No matter what you say, JSP will always be used by newbies (now I think
about it, every technology, especially new technology, will always be used
by newbies, won't it?).
It's much more important that newbies get clear and correct information like
(sorry if I repeat this quite often ;-), I think it's necessary) DON'T USE
DECLARATIONS AT ALL !!!!!
It's only natural that JSP will be used by the more intelligent webdesigner.
people that knwo the basics about making dynamic pages, they know what if()
for and while() is about, but don't completely get OOP or the servlet model.
Geert Van Damme
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com