1

I am in Eclipse and some time ago I was using this method: http://docs.oracle.com/javaee/7/api/javax/servlet/ServletResponse.html#setContentLengthLong(long) Now I can't manage to mek it work. I am using JDK 1.7 and I have inserted the following dependency in pom.xml:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

I have cleaned and updated the project with Maven. I have even closed and opened Eclipse. When I write:

response.setContentLengthLong(downloadFile.length());

I get:

The method setContentLengthLong(long) is undefined for the type HttpServletResponse

What am I doing wrong?

Ariel
  • 1,222
  • 2
  • 14
  • 25
  • Maybe eclipse maven cooperation is not working well. Have you tried building the project with the maven commandline tool? – Matthias Oct 05 '14 at 12:31
  • 3
    Some other version of the servlet api is also in your classpath. Open the HttpServletResponse class in Eclipse, and look in the explorer which jar file it's part of. – JB Nizet Oct 05 '14 at 12:32
  • @JBNizet You're right. It's conflicting with Apache Tomcat 7.0.35 `servlet-api.jar`. What can I do? – Ariel Oct 05 '14 at 12:37
  • 4
    If you're deploying your webapp in Tomcat 7, you shouldn't have servlet 3.1 in your classpath, since Tomcat 7 only supports servlet 3.0. If you want to use servlet 3.1, you need Tomcat 8. Also, a dependency on te servlet API, which is provided by the web container where you deploy the app and thus shouldn't be in the webapp itself, should be declared in the pom with `provided`. – JB Nizet Oct 05 '14 at 12:40
  • @JBNizet So you're telling me that I can't use Servlet 3.1 even though I have the necessary jar? – Ariel Oct 05 '14 at 12:42
  • 1
    Yes. Suppose you compile an application using libraries of Windows 8. Do you intend this app to work fine under Windows 3.1? Same here. You can't deploy an app using servlet 3.1-specific methods in a server that only supports servlet 3.0. – JB Nizet Oct 05 '14 at 12:44
  • That solves it. Put it as an answer if you want. – Ariel Oct 05 '14 at 12:52

3 Answers3

0

The most probably cause would be the second comment on your post. "Some other version of the servlet api is also in your classpath. Open the HttpServletResponse class in Eclipse, and look in the explorer which jar file it's part of. – JB Nizet"

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html

If you look at the docs for jdk 6 you will find that it does not contain that method.

Gerrit Brink
  • 977
  • 3
  • 15
  • 26
0

I met the same problem today. I am using IDEA, so I open the "Show Dependencies" from the maven tab, and search for the "servlet-api", there is one with version 2.4.

Exclude it from pom.xml, and build succeeded.

Robin Wang
  • 779
  • 1
  • 8
  • 16
-2
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

instead

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

use.. then ... solved.....

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135