Use and share a class in JSP pages Tag(s): Servlet/JSP
I have a class
public class Hello { public String say() { return "Hello"; } }
<%= say() %>
They are many to achieve that goal :
<% Hello h = new Hello(); out.print(h.say()); %>
<% Hello h = new Hello(); out.print(h.say()); %>
TAG source code
TAG source code : package test; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloTag extends TagSupport { public int doStartTag() throws JspException { try { pageContext.getOut().println("Hello"); } catch (IOException e) { } return SKIP_BODY; } }
TLD configuration (in the WEB-INF/tlds as hello.tld)
<taglib> <tag> <name>hello</name> <tagclass>test.HelloTag</tagclass> </tag> </taglib>
<%@ taglib prefix="ht" uri="WEB-INF/tlds/hello.tld" %> ... <ht:hello/>
<%! public static String say() { return "Hello"; } %>
<%@ include file="hello.inc" %>