简体   繁体   中英

Why can't I find method setContentLengthLong(long len) in HttpServletResponse?

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?

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.

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.

<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.....

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