|
-
Importing interfaces into JSPs?
Is it possible to import interfaces into JSPs, such as can be done with classes
using the import attribute in the page directive? We have an application
in which we would like our JSPs to be able to use public static final String
constants defined elsewhere, in an interface. This does not seem to discussed
in at least two books which deal extensively with JSPs.
-
Re: Importing interfaces into JSPs?
In article <39995e77$1@news.devx.com>, Bob <robert.byrnes@transcore.com> wrote:
>Is it possible to import interfaces into JSPs, such as can be done
>with classes using the import attribute in the page directive?
Yes! Here's a sample interface:
package com.awl.jspbook.ch04;
public interface MyInterface {
public static String TITLE = "Paradox Addendum";
}
and a sample jsp that uses it:
<%@page import="com.awl.jspbook.ch04.MyInterface" %>
The title is <%= MyInterface.TITLE %>
This works as expected.
The import attribute turns into a simple import in the generated
servlet, so there should be no surprises as long as the interface is
somewhere in your classpath.
- Larne
-
Re: Importing interfaces into JSPs?
I think you can even make the generated servlet to implement your interface
but this would require some extra coding.
Is that right?
Fernando Ribeiro
fribeiro@bol.com.br
larnep@canetoad.canetoad.com (Larne Pekowsky) wrote:
>In article <39995e77$1@news.devx.com>, Bob <robert.byrnes@transcore.com>
wrote:
>>Is it possible to import interfaces into JSPs, such as can be done
>>with classes using the import attribute in the page directive?
>
>Yes! Here's a sample interface:
>
>
>package com.awl.jspbook.ch04;
>
>public interface MyInterface {
> public static String TITLE = "Paradox Addendum";
>}
>
>
>and a sample jsp that uses it:
>
><%@page import="com.awl.jspbook.ch04.MyInterface" %>
>
>The title is <%= MyInterface.TITLE %>
>
>
>This works as expected.
>
>The import attribute turns into a simple import in the generated
>servlet, so there should be no surprises as long as the interface is
>somewhere in your classpath.
>
>
> - Larne
>
-
Re: Importing interfaces into JSPs?
Fernando Ribeiro <fribeiro@bol.com.br> wrote:
>
>I think you can even make the generated servlet to implement your interface
>but this would require some extra coding.
>
>Is that right?
You're probably thinking of the extends attribute of the page
directive, which forces the generated servlet to extend a class you
specify. In Tomcat by default all JSP-based servlets extend
org.apache.jasper.runtime.HttpJspBase. If you wanted to add some
string constant to all your JSPs you could define a new base class:
public class MyJspBase extends org.apache.jasper.runtime.HttpJspBase {
public final static String TargetURL = "http://www.devx.com";
}
Now if your JSPs extend MyJspBase they'll be able to use TargetURL
without explicitly importing anything:
<%@page extends="MyJspBase">
Click <a href="<%= TargetURL %>">here!</a>
You can also add methods to base classes.
Note that "HttpJspBase" is not part of the JSP specification, and
different JSP engines will use different base classes. Therefore this
feature can't really be used in a platform-independent way, at least
not yet. There are usually safer ways to get the same functionality,
such as importing classes with static fields (which brings this
conversation full circle...)
There is no 'implements' attribute that is to interfaces what
'extends' is to classes. Maybe in a future version.
- Larne
Similar Threads
-
By Kevin in forum VB Classic
Replies: 3
Last Post: 12-05-2005, 06:25 PM
-
By Larry Hunter in forum .NET
Replies: 1
Last Post: 01-26-2003, 11:45 PM
-
Replies: 11
Last Post: 10-25-2002, 08:20 PM
-
By Robert Cullen in forum Java
Replies: 0
Last Post: 10-23-2001, 07:34 AM
-
By Carl Crosswhite in forum authorevents.pekowsky
Replies: 1
Last Post: 08-16-2000, 10:48 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks