Importing interfaces into JSPs?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Importing interfaces into JSPs?

  1. #1
    Bob Guest

    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.
    Share on Google+

  2. #2
    Larne Pekowsky Guest

    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

    Share on Google+

  3. #3
    Fernando Ribeiro Guest

    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
    >


    Share on Google+

  4. #4
    Larne Pekowsky Guest

    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

    Share on Google+

Similar Threads

  1. Importing text file using schema.ini
    By Kevin in forum VB Classic
    Replies: 3
    Last Post: 12-05-2005, 06:25 PM
  2. Interfaces Inheriting from Interfaces
    By Larry Hunter in forum .NET
    Replies: 1
    Last Post: 01-26-2003, 11:45 PM
  3. Re: I'm amazed(The answer)
    By Blob in forum .NET
    Replies: 11
    Last Post: 10-25-2002, 08:20 PM
  4. Re: Interfaces Question
    By Robert Cullen in forum Java
    Replies: 0
    Last Post: 10-23-2001, 07:34 AM
  5. Mixing JSPs and HTML (I)
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center