Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why JSP can't implement interface?

 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can somone tell me why JSP can't implement interface?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to implement an interface with a JSP?
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4015
18
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Ravi Derek
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
Saurabh Maganlal Agrawal
Greenhorn
Posts: 1
Firefox Browser Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do both realise this thread is over 6 years old?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic