The method getDispatcherType() is undefined for the type HttpServletRequest

When I run my applicaiton from Eclipse it runs without any errors for servlet api 3.1.0 and 3.0.1.

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

I use tomcat 8.0.21 for eclipse. I have set up tomcat8 on ubuntu machine which runs on tomcat 8.0.14 stable version.

Unfortunately, I get the following error message if I use servlet api 3.1.0. But it works for the older version 3.0.1.

root cause

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [50] in the generated java file: [/var/lib/tomcat8/work/Catalina/localhost/ROOT/org/apache/jsp/WEB_002dINF/view/templates/login_002dtemplate_jsp.java]
The method getDispatcherType() is undefined for the type HttpServletRequest

Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)

Why do I get this error? How to fix this?


Solution 1:

You're not supposed to provide Servlet API along with the web application archive if the target runtime already provides the API out the box. Tomcat as being a JSP/Servletcontainer already provides JSP, Servlet and EL APIs out the box. When you provide them along with your webapp anyway, then you may run into classloading conflicts caused by duplicate different versioned classes in the runtime classpath coming from both the webapp and the server.

Add <scope>provided</scope> to those dependencies already provided by the target runtime.

See also:

  • How do I import the javax.servlet API in my Eclipse project?

Solution 2:

For Maven users there are several good answers here that you might want to check out.

I'm still in the dark ages, and am not using a dependency manager for my Tomcat project. If you're like me and have this issue, here's how I solved it: It seems tomcat provides the javax.servlet classes, so these don't need to be in your project's lib file. (I originally had the servlet-api-2.5.jar in my /WEB-INF/lib directory.) However you'll probably still need it to compile (I did), so you should move it to a location included in your java classpath. You may also need to tell your IDE where to look.

Hope that helps.