将super()或this()放在我的类中的位置

Web*_*net 6 java oop compiler-errors build java-ee

我有一个Servlet类来处理我不想在每个Servlet上重复的函数.我还在努力(即它仍然只加载index.jsp而不是其他文件).

public class MyServlet extends HttpServlet {
    public MyServlet () {
        super();
    }

    public void loadView (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");

        response.setContentType("text/html;charset=UTF-8");

        System.out.println("MyServlet::LoadView() success");

        dispatcher.forward(request, response);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Servlet如下

@WebServlet(name = "EditServlet", urlPatterns = {"/content/edit"})
public class EditServlet extends Library.MyServlet {
    public EditServlet () {
        super();
    }

    public void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("EditServlet loaded");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法编译我的代码:

SEVERE: Exception while deploying the app [ContentManagement] : (class: contentmanagement/content/EditServlet, method: <init> signature: ()V) Constructor must call super() or this()
Run Code Online (Sandbox Code Playgroud)

更新

好吧,删除void我的构造函数和调用super();得到的must call first部分消失,但它仍然说我必须打电话超级尽管已经被调用.

更新

我不明白下面的响应....他们一直说要放在super()构造函数中,当我的例子已经表明完成时,它是第一行代码.任何其他建议将不胜感激.

有什么想法吗?

现在这不是问题.我不知道是什么解决了这个问题,但是通过多次更改并从头开始重建我的应用程序,我不再遇到此问题

fal*_*lla 11

你的问题是:

method: <init> signature: ()V) Constructor must call super() or this()

这似乎是VerifyError症状.清理和重建项目应解决问题,另请参阅:


此外,您可以使用super()单行删除构造函数.JVM使用默认(隐式隐藏)构造函数为您完成.所以,清除这些不必要的行:

public MyServlet () {
    super();
}
Run Code Online (Sandbox Code Playgroud)

public EditServlet () {
    super();
}
Run Code Online (Sandbox Code Playgroud)

super()如果需要在构造函数中编写其他业务行,则必须显式调用.