Java ServletContext Interface


 

Java ServletContext Interface

In this tutorial, we will discuss about ServletContext Interface.

In this tutorial, we will discuss about ServletContext Interface.

Java ServletContext Interface

In this tutorial, we will discuss about ServletContext Interface.

ServletContext Interface :

ServletContext defines set of methods that helps servlet to communicate with its servlet container. ServletContext object is used to communicate with the servlet container. There is only one ServletContext object per web application. It is initialized when your web application is started and destroyed when your application is being shutdown.

It provides many methods. We are defining some of them -

  • getAttribute(String name) : it returns attribute of servlet container of specified name. If specified name not present then return null.
  • getContext(String uripath) :  It returns a servletContext object corresponding to the given URL on the server.
  • getMimeType(String file) : It returns the MIME type of the given file.If MIME type is unknown then return null.
  • getServletContextName() : this method returns name of the web application belonging to the given ServletContext as defined in web.xml.

Other methods are - getAttributeNames(),getInitParameter(String name), getInitParameterNames(), getMajorVersion(), getMinorVersion(), getNamedDispatcher(String name), getRealPath(String path),getRequestDispatcher(String path), getResource(String path), getResourceAsStream(String path), getResourcePaths(String path), getServerInfo(), getServlet(String name), getServletContextName(), getServletNames(), getServlets(),
log(Exception exception, String msg), log(String msg), log(String message, Throwable throwable), removeAttribute(String name),
setAttribute(String name, Object object).

Example :

ServletContextExample.java -

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletContextExample extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{response.setContentType("text/html");
PrintWriter pw = response.getWriter();
ServletContext context = getServletContext();
pw.println("Email: " + context.getInitParameter("email"));
}
}

web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletContext</display-name>

<context-param>
<param-name>email</param-name>
<param-value>[email protected]</param-value>
</context-param>
<servlet>
<servlet-name>ServletContextExample</servlet-name>
<servlet-class>ServletContextExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletContextExample</servlet-name>
<url-pattern>/servletContextExample</url-pattern>
</servlet-mapping>


</web-app>

Output :

Ads