Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between <% ... %> and <%! ... %> in jsp

Tags:

java

jsp

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

like image 670
Yanki Twizzy Avatar asked Jul 26 '10 08:07

Yanki Twizzy


People also ask

What does <% %> mean in JSP?

<%@ is the directive attribute. You may include a page or may declare a tag-library using <%@ More on it here.

What is the difference between JSP include page and %@ include file?

What is the difference between <jsp:include page = ? > and <%@ include file = ? > <%@ include file=”filename” %> is the JSP include directive. At JSP page translation time, the content of the file given in the include directive is 'pasted' as it is, in the place where the JSP include directive is used.

What is the main difference between servlet and JSP?

JSP is slower than Servlets, as the first step in the JSP lifecycle is the conversion of JSP to Java code and then the compilation of the code. Servlets are Java-based codes. JSP are HTML-based codes. Servlets are harder to code, as here, the HTML codes are written in Java.

What is the difference between JSP and tag file?

A tag file is a source file that contains a fragment of JSP code that is reusable as a custom tag. Tag files allow you to create custom tags using JSP syntax. Just as a JSP page gets translated into a servlet class and then compiled, a tag file gets translated into a tag handler and then compiled.


1 Answers

<% ... %> 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.

like image 132
skaffman Avatar answered Oct 08 '22 04:10

skaffman