【问题标题】:Servlet does not have a default constructor.Servlet 没有默认构造函数。
【发布时间】:2014-09-16 12:36:17
【问题描述】:

我有这个 Servlet,它也包含它的构造函数,但是当我尝试在 Weblogic 服务器上运行我的应用程序时,它给了我一个错误,即“SocialMediaSessionHandler”没有默认构造函数。该应用程序在其他平台上运行良好,但是当我在服务器之间切换时出现错误:实例化 servlet 时发生错误:“SocialMediaSessionHandler”。

   public class SocialMediaSessionHandler extends HttpServlet {
    private static final long serialVersionUID = 1L;
    HttpSession session = null;
    private static final CDLoggerInterface log = CDLogger
            .getLogger(SocialMediaSessionHandler.class);
    Resource resource = new ClassPathResource("/fp.properties");
    private boolean debugEnabled;
    String serverUrl = "";
    IWebServiceManager webServiceManager;
    Utility util = null;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SocialMediaSessionHandler() {
        util = new Utility();

        // TODO Auto-generated constructor stub
        try {

            ApplicationContext context = LoadSpringManageService
                    .LoadApplicationContext();
            webServiceManager = (IWebServiceManager) context
                    .getBean("webserviceManager");

            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            if (props.getProperty("debug.enable") != null
                    && props.getProperty("debug.enable") != "")
                debugEnabled = Boolean.parseBoolean(props
                        .getProperty("debug.enable"));
            if (props.getProperty("server.url") != null
                    && props.getProperty("server.url") != "")
                serverUrl = props.getProperty("server.url");

        } catch (MalformedURLException e) {
            log.error("MalformedURLException occured.....", e);

        } catch (Exception e) {
            log.error("Problem in loading CD Logger properties file", e);
        }
    }

【问题讨论】:

  • 在 servlet 中你不应该创建任何构造函数。如果您想要任何默认功能,请覆盖 servlet 中的 inIt() 方法。因此,根据您的代码,您可以将 SocialMediaSessionHandler() 更改为 init()。让我知道它是否不起作用。 :)

标签: java facebook-graph-api jakarta-ee servlets java-ws


【解决方案1】:

当我们查看 Servlet 的生命周期时,它最初会加载类,然后通过调用默认构造函数来创建 Servlet 实例。并且步骤继续。

但是现在在您的情况下,您正在重载构造函数,通过禁止容器创建默认构造函数。默认构造函数(没有任何参数的构造函数)。仅当您没有创建构造函数时才会创建默认构造函数。

此外,在 Servlet 中定义构造函数并不是一个好习惯。

让我们做一些研发尝试在你的类中编写一个默认构造函数,我猜它应该可以工作。

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2015-06-02
    • 2017-10-04
    • 2022-01-01
    相关资源
    最近更新 更多