JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Servlet Context

January 30, 2014 by Krishna Srinivasan Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

Servlet Context is used to communicate with the servlet container to get the details of web application. It is created at the time of deploying the project. There is only one Servlet Context for entire web application. Servlet Context parameter uses <context-param> tag in web.xml file.

Methods of ServletContext interface:

Name Description
String getInitParameter(String) It returns the Servlet initialization parameter of the given name and if the requested parameter is not available then it returns null.
Enumeration getInitParameterNames() It returns names of all Servlet initialization parameters defined in web.xml file.
void setAttribute(String, Object) Sets an attribute with the current request.
Object getAttribute(string) Returns the value of an attribute located with the given name or returns null value if an attribute with the given name is not found.
Enumeration getAttributeNames() Returns all the attribute names in the current request.
void removeAttribute(String) Removes an attribute identified by the given name from the request.
getRequestDispatcher( ) This method takes a string argument describing the path to located the resource to which the request is to be dispatched..

Servlet Context Example

Following example shows use of ServletContext:

[code lang=”java”]
package javabeat;

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

public class ServletExample extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException
{

PrintWriter pw = response.getWriter();
pw.println(getServletContext().getInitParameter(&amp;quot;Welcome&amp;quot;));

}
}
[/code]

Here getInitParameter() is used to initialize the parameter from the web.xml file.

web.xml file:

[code lang=”xml”]
<web-app>
<servlet>
<servlet-name>ServletExample</servlet-name>
<servlet-class>javabeat. ServletExample</servlet-class>
</servlet>

<context-param>
<param-name>Welcome</param-name>
<param-value>Welcome to javabeat!!!!!!!!!</param-value>
</context-param>

<servlet-mapping>
<servlet-name>ServletExample</servlet-name>
<url-pattern>/ServletExample</url-pattern>
</servlet-mapping>
</ web-app>
[/code]

Here <context-param > tag contains two tags namely <param-name> and <param-value> which is used to initialize the attributes of the servlet.

The output of the following program is:
Servlet Context output

Previous Tutorial : Web.xml  || Next Tutorial :  Request Dispatcher

Filed Under: Java EE Tagged With: Servlets Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved