request.getServletContext() not found, even with new JAR

情到浓时终转凉″ 提交于 2019-11-30 08:28:28
BalusC

According to the Javadoc the ServletRequest#getServletContext() method is introduced in Servlet 3.0, not 2.3. You need to install and integrate a Servlet 3.0 compatible container such as Tomcat 7, Glassfish 3, etc in Eclipse and set the Target Runtime of your Dynamic Web Project to that container. When you do that properly, then you do not need to manually fiddle with build paths or build.xml at all, Eclipse will handle it for you automatically. You also do not need to download loose JAR files of an arbitrary servletcontainer of a different make/version and put it in your buildpath. It would only lead to future classpath and portability troubles.

See also:

Neha Velhal

The getServletContext() method is introduced in Servlet 3.0, not 2.3. But if you want to get the ServletContext then an alternative method to get it is:

ServletContext context = request.getSession().getServletContext();

if (username != "" & username != null ) {
    context.setAttribute("savedUserName", username);
}
writer.println("Context Parameter : " + (String)context.getAttribute("savedUserName"));

This way you can get the stored Request Parameter Value in different browser....

I've had the same trouble recently. In fact it started happening after adding some new jars. Ant found HttpServletRequest class in selenium-server.jar which alphabetically comes first before servlet-api.jar (which was supposed to be used). So i just renamed selenium-server.jar to x-selenium-server.jar and everything started building OK, as it used to.

This is not a problem with your java compiler. javax is provided by servlet container itself and you must include servlet container jar files to your project setup.

javax.servlet.http and all classes related servlet context and servlet programming is related to your Servlet Container only. So stop worrient about anything else and check if Tomcat libraries are being included in your WEB-APP class path.

If not add them and everything will be fine.

Right Click on your project > Properties > Add Libraries > Server Runtime

and choose your server that is associated with your application.

You are done, this will include Servlet Container libraries to your project and HttpServletRequest & HttpServletResponse classes will be resolved.

Hope it helps, more information about Servlet Architecture and context can be found Here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!