简体   繁体   中英

Can we implement an interface in jsp?

I am just preparing for interview and I just came across this question : can we implement an interface in jsp?

If yes then how can we do it?

I tried to find the answer on many of the site but not able to understand the exact answer for this.

Can you please help me.

Thanks.

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 ?

SEE HERE .Here you can find some points

您可以在实现接口的JSP中导入一个类,也可以使用Bean。

将Java程序逻辑放入JSP的坏主意-将其放在帮助器类(即JavaBean)或标记库中。.JSP不是.java,因此它不能实现接口,但是您可以在jsp和此javabean中使用javabean可以实现接口

As a comment indicated, in conversational context of JSPs "interface" often means user-interface, GUI. But that would be too trivial a question.

Implementation of an interface

<%
    List<String> roles = new LinkedList<String>();
%>

List is an interface, and LinkedList is an implementation.

<%@
    class ZeroMap<K, V> implements Map<K, V> {
        ...
    }
%>

As a JSP is a (to be generated) servlet class, of which the class name officially is not known, for internal use only.

Declaration of a local interface

<%@
    interface A extends Serializable, Runnable {
        public int calc(int x, int y);
    }

    private A alpha = ...;
    private A beta = ...;
%>

So for internal usage possible in the declaration section <%@ ... %> . To collect several interfaces, as marker interface, and so on. As an internal usage requires an implementation, it gives bulky code.

So not really suitable for JSPs which one would like to restrict to the represation of results (View in MVC).

try

<%@page extends="bla.bla.bla" %>

or even

<jsp:directive.page extends="bla.bla.bla" />

A simple answer is NO. But you can declare a class inside JSP page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM