Friday, April 18, 2014

Resolving "HttpServletRequest cannot be resolved" error in eclipse

Eclipse errors like - "The import javax.servlet.http.HttpServletRequest cannot be resolved" are caused by not having the servletcontainer-specific libraries in your project's build path.

You should at least use the Eclipse IDE for Java EE Developers.

You should also have a servlet container (eg.; Tomcat, Glassfish) installed on your machine.

These are what you have to do in your project if you haven't done so:

1. Integrate a new servlet container in Eclipse.

Open the Servers view (if not yet opened in the bottom box section of Eclipse).

Right-click from the Servers pane  > New >  Server.

A new window will pop up. Pick the servlet container you want to use from the list and walk through the wizard.


2.  Set the project's  'Targeted Runtimes'  property.

Right-click on the project name > Properties > Targeted Runtimes.

Select a servlet container (eg.; Apache Tomcat) and click "OK".

Eclipse should then automatically take the servlet container's libraries into the build path. Build your project and the errors should go away. The errors are resolved.

If the error persists, then for some reason the servlet container's libraries was not automatically added into the build path. Just do the next step.


3. Manually add the servlet container's library into the build path.

Right-click on the project name > Properties > Java Build Path > Add Library > Server Runtime > select a servlet container (eg.; Apache Tomcat) > Finish.

Build your project and the errors should go away. The errors are resolved.


IMPORTANT NOTES:
Some examples of servlet container-specific libraries are servlet-api.jar, el-api.jar, javaee.jar, annotations-api.jar, etc.

Never manually copy or move servlet container-specific libraries into your webapp's /WEB-INF/lib and JRE/lib.

Servlet container libraries belong only in the servlet container. For example, in your webapp,  the Apache Tomcat jars should only be in  /Java Resources/Libraries/Apache Tomcat folder.

You should also never have a duplicate servlet container in the classpath.




No comments:

Post a Comment