본문 바로가기

자주 뜨는 에러 모음집 👿

getServletContext() 함수 사용 에러

by Meteora_ 2021. 6. 28.
728x90

오류->

The method getServletContext() is undefined for the type HttpServletRequest

 

 

원인은 HttpServletRequest 의 getServletContext() 함수는 Servlet 3.0 부터 지원되기때문이다

 

서블릿 3.0은 톰캣 7.0부터 지원된다는데 본인은 톰캣 8.5를 사용하는데도 에러가떴다. 이거 뭐지...?

 

 

해결책 두가지

 

1. <%= getServletContext().gerRealPath("/") %> 호출

 

or

 

2. pom.xml에 다음 dependency 추가

<!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

 

참고 : http://stackoverflow.com/questions/1979957/maven-dependency-for-servlet-3-0-api

 

Maven dependency for Servlet 3.0 API?

How can I tell Maven 2 to load the Servlet 3.0 API? I tried: javax.servlet servlet-api <versi...< p=""> </versi...<>

stackoverflow.com

 

댓글