global Methods inside JSPs

hi everyone,

is it possible to create Global methods inside JSPs so that all my JSPs can access them. notice that I am not looking at helper classes or simialr solutions. I want to be able to do the following inside my JSP

<%= getname("test") =%>

on solution I thought of is to make the JSP class extends MyClass instade of the HttpJspBase. MyClass will extends the HttpJspBase class and add the additional methods. but I don't know how to tell the JSPcontainer to do it my way.

any idea? is there an easy way to create globel methods in JSPs?

thanks in advance

[606 byte] By [Adelxa] at [2007-11-26 12:20:02]
# 1

Easiest way without hacking into the JSP class is just to provide a utility class that your JSPs can use, and include a whole bunch of static "helper" methods in it.

<%= MyUtilities.getName("test") %>

However, scriptlet code is discouraged in JSPs. I would suggest you write this sort of thing into a servlet/action layer where you can more easily do this overriding.

Another alternative is to use the <%@ include %> directive to always include some basic functions in the JSP.

However any functions that you want to define will NOT be able to access the implicit variables in a JSP (request, response, application...) They will be seperate methods. If you want to use those variables, you have to pass them to the method. At that case, a seperate bean class implementing static methods becomes equivalent, and even preferable for testing purposes.

evnafetsa at 2007-7-7 15:09:31 > top of Java-index,Archived Forums,Socket Programming...
# 2

thank you for your reply,

I am well aware of the idea of helper classes. and this is not what I want.

as you may know, the JSP is converted to a Java Class. this java Class extends the HttpJspBase class. if I am able to tell the container to extend my Class, then I am able to provide more functions in my JSPs.

I like my code to look like this <%= getName("test") %> rather then <%= helperClass.getName(request, "test") %>

I am very sure that there is away. I just need to dig more to find it. I will be greatful of any help in this area.

Adelxa at 2007-7-7 15:09:31 > top of Java-index,Archived Forums,Socket Programming...
# 3

There is no way I am aware of to do exactly what it is you are asking.

But there are two alternatives you might consider

1: JSTL/EL

I'm not exactly sure what you want <%= getName("test") %> to do , but it may be possible to write as an EL expression. eg ${param.test} or ${requestScope.test}

2,write a custom tag to do this lookup.

<myTags:name value="test" />

Hope this helps some,

evnafets

evnafetsa at 2007-7-7 15:09:31 > top of Java-index,Archived Forums,Socket Programming...
# 4
http://java.sun.com/products/jsp/syntax/2.0/syntaxref2010.html#15653You could use the extends attribute of the page directive tag.
tolmanka at 2007-7-7 15:09:31 > top of Java-index,Archived Forums,Socket Programming...