• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can We extend a java class or interface in a JSP page  RSS feed

 
swastik padhy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can We extend a java class or interface in a JSP page
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know:

(a) you cannot do that to a jsp page.
(b) your servlet can implement a custom interface
(c) your servlet cannot extend a custom java class (as it already extends HttpServlet class)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can, as long as you put it in a declaration script.
I would not recommend it though.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lynette, why can't you ?
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i misunderstood the question. yes you can do that.
 
Pavn Igor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use page directive to extend any java class.

<%@ page extends="your.java.class">
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pavan Igoor:
Use page directive to extend any java class.


Do so at your own risk. Even the JSP specification says to avoid doing this in favor of other means to achieve extending JSP functionality.
 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavn Igor wrote:Use page directive to extend any java class.

<%@ page extends="your.java.class">


From the above ...
JSP will automatically extend to HttpServlet class ..if we extend another class ,java does not support multiple inheritance ,how its achieved .
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karimkhan pathan wrote:
Pavn Igor wrote:Use page directive to extend any java class.

<%@ page extends="your.java.class">


From the above ...
JSP will automatically extend to HttpServlet class ..if we extend another class ,java does not support multiple inheritance ,how its achieved .


Hi everybody, can anybody please explain why there is no error if I extend a java class in a jsp page, java does not support multiple inheritance and I am not getting any error.

Thank you all in advance
 
Tomasz Szymanski
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you expect na error? The servlet produced from your JSP will extend only one class: either your custom class if you specify it in the page directive, or some container-provided class if you don't.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This whole topic makes me cringe, both if it's about the servlet class being generated from a JSP, or about Java classes defined inside of a JSP. Either is an indication of bad design choices.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf, is right on the money. The days of dealing with JSPs as if they were Java classes, or even putting Java code in a JSP are long long passed. Let's all catch up to 2010 and forget about this type of outdated nonsense.
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Ulf, is right on the money. The days of dealing with JSPs as if they were Java classes, or even putting Java code in a JSP are long long passed. Let's all catch up to 2010 and forget about this type of outdated nonsense.


The code written inside the declaration tags been treated as members of the class..
So the classes defined in above JSP are treated as inner classes.

For extending a class in your Servlet... You have to implement Servlet and ServletConfig interfaces where you are not required to extend HTTPServlet, you can extend another class.

However can you give a requirement where you need to extend someother class in your servlet or jsp?
Is it not possible to do the things without extending a class?

Cheers!!!
Prabhakar
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!