can not find addListener method from javax.servlet.ServletContext
Asked Answered
P

4

6

I am trying to change spring xml settings to pure code based setting.

So I read official documents and some posts from blogs.

e.g. http://docs.spring.io/spring-framework/docs/4.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html

An I made a code like ...

public class TestInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container)
            throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("on Startup method has called.");
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(RootConfig.class);
        container.


        //container.addListener(new ContextLoaderListener(ctx));
    }
};

A problem here. In those pages, they use addListener(new ContextLoaderListener(ctx)) method to set context. However my eclipse can not find that method from container variable.

I do not know any clue why my container variable(javax.servlet.ServletContext instance) can not read this method.

Thanks for your answer:D

P.S.

My spring version is 4.1.6.RELEASE and I include servlet3.0, spring-context, spring-webmvc on pom.xml.

========================

Maybe I got some communication problem, So I summarize this :D

=================================

Edit. This is not error on console. However it is the only message I got. It is from eclipse toolkit.

The method addListener(ContextLoaderListener) is undefined for the type ServletContext

than recommendation is Add cast to 'container'

Pharmaceutics answered 14/5, 2015 at 2:38 Comment(8)
That is not valid Java. Please edit and fix the code.Officeholder
Also cut and paste the complete, actual error message, do not paraphrase.Officeholder
@JimGarrison you mean container.? I put that into code since that line is where my problem occurs. Except that, it is pretty valid java. If you do not mind visit the link I provided on original post. And RootConfig is just another java file which has @Configuration on class definition. thanks.Pharmaceutics
@JimGarrison It is not about error message. The document says parameter variable can use addListener method. However in code, it is impossible. It just error so can not even compilePharmaceutics
@JuneyoungOh Hi, I understand that you can't compile but can you show the message that compiler show? or might as well show the screenshot of the error.Tit
@Tit OK. I will edit the post :)Pharmaceutics
@JuneyoungOh hmm strange indeed. Can you show me your pom.xml? it should be able to accept the parameter if you're using servlet api ver 3. and to make sure, can you try to find the ServletContext and ContextLoaderListener classes to make sure there is no conflicting dependency?Tit
@Tit Oh! thanks for clue. I just put dependency servlet2.5;;; Please, write this as an answer so I can close this issue. Thanks! - I found 2 ways to fix this. #1. make version 3.0.1 and artifactId 'javax.servlet-api'. #2.add tomcat(in my case 7.0) to project build path and remove servlet dependency.Pharmaceutics
T
10

To follow up on what @JuneyoungOh has commented, turns out that the problem is because of conflicting dependency. And these are the ways to solve this problem :

* make version 3.0.1 and artifactId 'javax.servlet-api' or
* add tomcat(in my case 7.0) to project build path and remove servlet dependency.
Tit answered 14/5, 2015 at 7:0 Comment(1)
Appreciate it. Nice insightVevay
D
0

In my case the problem was because of Spring-Support which is depended on "javax.servlet" and I just excluded it:

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-support</artifactId>
        <version>${spring-support.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>servlet-api</artifactId>
                <groupId>javax.servlet</groupId>
            </exclusion>
        </exclusions>
    </dependency>
Diplomatics answered 24/8, 2015 at 10:25 Comment(1)
Thanks for additional information. I hope it will quite a help :-)Pharmaceutics
R
0

In my case there was:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

notice, that artifactId is servlet-api, not javax.servlet-api.

I have created a legacy MVC project, that's why I had this package. When I tried to convert .xml configuration to Java, I came across this problem.

Certainly it's not the same as in the question, but it shows up as the first result in google search.

Rectal answered 24/1, 2018 at 13:3 Comment(0)
D
0

In my case I just had to comment out the javax.servlet:servlet-api dependency as depicted here:

    <!-- dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>7.0.47</version>
    </dependency>

This looks like the same idea presented here: https://mcmap.net/q/1678928/-can-not-find-addlistener-method-from-javax-servlet-servletcontext

Dialyze answered 30/9, 2022 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.