getServletContext

This section will explain you about the getServletContext() method in Servlet using a simple example. This example will demonstrate you the use of getServletContext() method.

getServletContext

getServletContext

In this section we will discuss about the getServletContext() method in Java.

This section will explain you about the getServletContext() method in Servlet using a simple example. This example will demonstrate you the use of getServletContext() method.

getServletContext() method is declared in the javax.servlet.ServletConfig interface and it is used to get the reference of ServletContext. Web container creates the object of ServletContext when a project is deployed. For each web application, object of ServletContext is created only once and we can get the configuration information from the deployment descriptor (web.xml) through this object.

Syntax of getServletContext()

ServletContext getServletContext()

Example

Here I am giving a simple example which will demonstrate you that how getServletContext() method can be used. In this example we will use the RequestDispatcher class to navigate to the another page for this we will get the object of ServletContext and then forwarded to the index.jsp page.

FirstServlet.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
//rd.include(request, response);
rd.forward(request, response);
}
}

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index.jsp</title>
</head>
<body>
<h2>
<%
out.print("getServletContext() Example");
%>
</h2>
</body>
</html>

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_3_0.xsd" id="WebApp_ID" version="3.0">

<servlet>
<servlet-name>getContext</servlet-name>
<servlet-class>roseindia.net.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getContext</servlet-name>
<url-pattern>/getContext</url-pattern>
</servlet-mapping>
</web-app>

Output

When you will execute the Servlet FirstServlet.java then the output will be as follows :

Download Source Code