I am adding some more functionality to a page that has the two tags mentioned in the title. I noticed that the variable I declare in <% ... %>
cannot be used in <%! ... %>
and vice versa. What is the difference between the two and how can I declare variables that could be used in the two tags
views:
87answers:
1
+8
A:
<% ... %>
is used to embed some java code within the main service()
method of the JSP. It is executed during the rendering of the page.
<%! ... %>
is used to define code outside of the flow of the page, and therefore outside the main service()
method. Typically, this was used to define utility methods that would be called from within a <% ... %>
block.
Both approaches are now obsolete, however. JSP EL, JSTL and tag classes are the preferred way of doing the same thing.
skaffman
2010-07-26 08:05:17
i only can add that whole jsp is now deprecated since jee6. jsf - is a main view technology.
foret
2010-07-26 08:13:37
hm, is it really deprecated? I doubt that
Bozho
2010-07-26 08:32:03
@bozho: I guess you are referring to the comment, not the answer? @skaffman I second the notion of scriptlets being deprecated very much, whereas @foret JSPs will probably always remain appropriate for cases where action frameworks are needed because component frameworks are too resource-hungry.
seanizer
2010-07-26 08:43:28
yes, I referred to the comment
Bozho
2010-07-26 10:23:50
@foret: JSF isn't a view technology. JSP and Facelets are. Facelets is indeed the successor of JSP when talking in JSF perspective. @foret and @Bozho: JSP is only deprecated in perspective of JSF 2.0, not of Java EE. This is literally cited in [Facelets chapter of Java EE 6 tutorial](http://download.oracle.com/docs/cd/E17410_01/javaee/6/tutorial/doc/giepx.html). @skaffman: awesome, 8 votes in JSP tag for such a simple answer? How did you do that? :o
BalusC
2010-07-26 16:30:52
I'm not sure if scriplets are deprecated, but they aren't the best practice since embedding Java code in your JSP file goes against the separation of presentation and business logic.
Lotus Notes
2010-07-26 16:37:00
@Lotus: Scriptlets are indeed not officially deprecated when talking in JSP perspective. Its use is however officially discouraged and it should be used for quick prototyping only. Here's a cite from [Oracle JSP conventions](http://www.oracle.com/technetwork/articles/javase/code-convention-138726.html): *JSP Scriptlets - Where possible, avoid JSP scriptlets whenever tag libraries provide equivalent functionality.*
BalusC
2010-07-26 16:40:32
@BalusC: Mystifying, isn't it? Related to the "bikeshed effect", I reckon.
skaffman
2010-07-26 16:47:49