1

Can we implement interface in Jsp ? If Yes, Then how or why not?

If yes, then how can we override those unimplemented methods?

Can we extend any other class in jsp ? If no then why or how if yes.

If yes, then what will happen as jsp will be converted as servlet and servlet already extends something else.

If no, then <%@ page extend="package.className"> , what does this mean?

Thanks.

2 Answers2

0

All the code written in JSP will be used to generate the code in service method for a Servlet which URL pattern matches the URL to access to your JSP. Apart of that, it is considered a bad practice to write Java code in JSP, known as scriptlet, and you should not use it anymore.

The extend means that the generated servlet must extend from the class specified in the value. But again, you should avoid using scriptlets.

Instead thinking on using any Java code (through interface, class inheritance or whatever you may come up with later), use an MVC approach and move all your business logic into controller/model. The basic approach is using a Servlet, another approach is using a web framework like Spring MVC or JSF that helps you writing this. For the cases you have to add dynamic data to your view (JSP), you can use Expression Language and libraries like JSTL to solve your problem.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Yeap, I know that very bad practicing java code in jsp page, But still I need to know the answer. Cant say Its bad practice to an interview board :( – Khaja Md Sher E Alam Dec 03 '14 at 15:04
  • @Edamame well, if I receive this question in an interview, I would answer that the JSP code is written into a Servlet and I can make this servlet extends from a class, but the fact that I can doesn't mean I will do it because it's considered as a bad practice. – Luiggi Mendoza Dec 03 '14 at 15:10
  • @LuiggiMendoza Just wondering how can you modify the container generated JSP servlets? – Sas Dec 03 '14 at 15:33
  • @Edamame Of course you can. How you act is just as important as what you know. – Klas Lindbäck Dec 04 '14 at 09:46
0

The attribute extends (not extend) specifies a class that should implement the interface javax.servlet.jsp.JspPage.

As the generated class name is not necessarily normed or straight-forward, it is not nice to extend another JSP. So extending has little value.

Implementing interfaces does not offer much value, as the JSP class cannot be reused (extended).

In fact JSPs main use should be for the View functionality of the MVC paradigm, where a servlet serves as Controller, and dispatches to the Vew / JSP, with data/Model in the attributes.

Instead of inheritance use POJOs (plain old java objects) to implement some general functionality. Such a class can be developed outside of a web container, for instance by writing unit tests: TDD, test driven development.

Also consider that a JSP can, as a servlet, forward or include another servlet/JSP.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138