File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Why JSP can't implement interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Why JSP can Watch "Why JSP can New topic
Author

Why JSP can't implement interface?

Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Hi,
Can somone tell me why JSP can't implement interface?


Chop your own wood, and it will warm you twice. - Henry Ford
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Why would you want to implement an interface with a JSP?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
I want all my JSPs to implemet this interface so each and every JSP will have that methods.....

And anyways as JAVA is providing interface feature than it should be available to JSP... if Sun is providing facility to extend class than why not interface?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Kartik Patel:
I want all my JSPs to implemet this interface so each and every JSP will have that methods.....

And anyways as JAVA is providing interface feature than it should be available to JSP... if Sun is providing facility to extend class than why not interface?


JSP are meant to reduce Java code and put more presentation (HTML)code. JSP implementing an interface does not provide any value. Who is going to use the implemented interface ?


Groovy
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
I want all my JSPs to implemet this interface so each and every JSP will have that methods....

Bad idea to put java program logic into a JSP - put it in a helper class (ie JavaBean) or tag library.
Bill
Robert Baggio
Greenhorn

Joined: Dec 14, 2005
Posts: 1
.JSP is not a .java
so it cat not implements interface
but you can use a javabean in a jsp and this javabean can implements interface
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Hi,
Thank you all for the answer.

but I think everybody has either suggested me to go for another approach (using tags or beans) or justify that the writing java code in jsp is a bad design...

But can anyone please tell me that Why the hell <B>Sun Microsystem does not think of giving JSP a way to implement interface</B>.... if it is giving facility to extend other class.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Its not that they "didn't think of it" its that they "don't want you to do it".

Didn't someone on this forum once say something like "You can bake a cake by setting your house on fire, but there's probably a better way". The same goes with your question. If you want to put tons of application logic in JSPs, such as requiring they each implement a method, then you shouldn't be using JSPs in the first place.

I almost *never* put any functions in JSPs, I put them in shared libraries that multiple JSPs can use. I think the only I will even break this is if I am writing text processing code (to convert input/output) and I know for sure it will only be called by one page (oh, and I'm being really lazy)... but even then its still better to put it in the library.
[ December 15, 2005: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
If a JSP extends a class, it inherits methods and fields that can be used in the page (which, as william pointed out, is a bad idea for web app developers).

But if a JSP implements an interface, what do you gain by that? How could you now use that page in ways that you couldn't use it before?

JSPs don't exist as Java objects outside of the page (as far as the web app developer is concerned). Thus, 'extends' buys you something, 'implements' doesn't.


Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Kartik Patel:
I want all my JSPs to implemet this interface so each and every JSP will have that methods.....


You do realize that implementing an interface doesn't (by itself) give you access to methods from the interface you're implementing, don't you?
It's a contract, not an inheritance mechanism.
You would still need to code those methods yourself.

If you're not sure what this means, you might want to do some more reading on interfaces.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I hope Kartik also knows these all that implementing interface in JSP is not good idea....

Even scriptlet in JSP is not a good idea but Sun has provided this facility.... just like that why not interface implementation.....

What he wants is why...
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

What he wants is why...


JSP's are intended for presentation(VIEW). It's NOT a java program to implement a interface. You can put the functionality in a Bean (or) a tag handler and invoke it using standard and custom tags.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Hi all,

Actually Rathi is correct...
forget about the well know quotes in the forum:
You can bake a cake by setting your house on fire, but there's probably a better way..
and all other alternative.....

The basic question is Why the Hell interfaces are not introduced by SUN MICROSYSTEM in JSP
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

The basic question is Why the Hell interfaces are not introduced by SUN MICROSYSTEM in JSP


There is only one man who can put an end to this thread. Ya its Bear. I am sure you are going to enjoy his replies.
[ December 15, 2005: Message edited by: Vishnu Prakash ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Oh get a grip people!

Look at the JSP API - the interfaces related to JSP specify a small number of methods. Therefore the JSP compiler only has to cope with creating those methods.

If J Random Programmer was allowed to specify a new kind of JSP interface, the JSP compiler would have to cope with it - adding a whole new set of problems to an already complex environment.

Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

There really is no need for me to jump into this as the question has already been answered for anyone who is bothering to read the replies. An interface on a JSP would be absolutely useless since there is no way that the JSP class could subsequently be referenced using the interface. "Mammaries on a bull" comes to mind. You might as well ask why they didn't design your car with a shaving mirror in the engine compartment. If you can't use it, why design it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

The fundamental question is: how are you going to call those methods?

If you are calling them from within the JSP, then it doesn't matter if the JSP implements the interface or not, since you still have to write the methods So it doesn't matter if it's an interfact or not, because you are calling the methods from within the same class.

If you are trying to call the methods another way, then we'd need to see your architecture, as there's something amiss.

Theoretical arguments as to the fact that JSPs should be allowed to implement interfaces just becuase that keeps the language pure won't cut it, I'm afraid. There are other theoretical arguments that state that they shouldn't (MVC design pattern, for one)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ravi Derek
Greenhorn

Joined: May 31, 2006
Posts: 1
How about an interface with only constants. It would be usefull to implement such an interface.

I can then reference a value like just "CONST_VALUE" instead of Constants.CONST_VALUE.

This is not a big deal, but a little convenience
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

Read this article.
Saurabh Maganlal Agrawal
Greenhorn

Joined: Feb 14, 2012
Posts: 1

It's always advisable not to add Java code in JSPs.

1. When you extend any class, your JSP inherits the data members and methods from the base class. You can use those members in your JSP.

However,

2. When implement any interface, it is required to write method body for all the operations written in interface contract. And if you do so, then you will have to write/add Java code in your JSP to implement the method body (which is unexpected anyway).

Hence, it's not allowed to implement any interface in JSPs, to prove it's design.

Thanks,
Saurabh Agrawal
kien kun
Greenhorn

Joined: Feb 15, 2012
Posts: 27
Because Jsp isn't a class Which express a object in oriantate object.
JSP is a page HTML to combine with java.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

You do both realise this thread is over 6 years old?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why JSP can't implement interface?