ServletContext in Servlet

Advertisements

Prev Tutorial Next Tutorial

ServletContext Interface in Servlet

ServletContext is one of pre-defined interface available in javax.servlet.*; Object of ServletContext interface is available one per web application. An object of ServletContext is automatically created by the container when the web application is deployed.

Assume there exist a web application with 2 servlet classes, and they need to get some technical values from web.xml, in this case ServletContext concept will works great, i mean all servlets in the current web application can access these context values from the web.xml but its not the case in ServletConfig, there only particular servelet can access the values from the web.xml which were written under <servlet> tag, hope you remember. Have doubt ? just check Example of ServletConfig.

How to Get ServletContext Object into Our Servlet Class

In servlet programming we have 3 approaches for obtaining an object of ServletContext interface

Way 1.

Syntax

ServletConfig conf = getServletConfig();
ServletContext context = conf.getServletContext();

First obtain an object of ServletConfig interface ServletConfig interface contain direct method to get Context object, getServletContext();.

Way 2.

Direct approach, just call getServletContext() method available in GenericServlet [pre-defined]. In general we are extending our class with HttpServlet, but we know HttpServlet is the sub class of GenericServlet.

Syntax

public class Java4s extends HttpServlet
{
public void doGet/doPost(-,-)
{
//….
}
ServletContext ctx = getServletContext();
}

Way 3.

We can get the object of ServletContext by making use of HttpServletRequest object, we have direct method in HttpServletRequest interface.

Syntax

public class Java4s extends HttpServlet
{
public void doGet/doPost(HttpServletRequest req,-)
{
ServletContext ctx = req.getServletContext();
}
}

How to Retrieve Data from ServletConfig Interface Object

ServletContext provide these 2 methods, In order to retrieve the data from the web..xml [ In web.xml we have write <context-param> tag to provide the values, and this <context-param> should write outside of <servlet> tag as context should be accessed by all servlet classes ].

In general database related properties will be written in this type of situation, where every servlet should access the same data.

Syntax

public String getInitParameter("param name");
public Enumeration getInitParameterNames();

I am not going to explain about these methods, these are similar to Retrieve Client Input Data in Servlet but here we are retrieving values from web.xml that's it.


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements