• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • 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)
  • 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)

ServletConfig in Servlet

February 1, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

In this article we will see about ServletConfig Interface and how it works. The config object is created by the web container based on the initialization parameters specified in the deployment descriptor. It is used to send information to a servlet during initialization.

Now let see the methods of ServletContext interface, which are listed the following table:

Name Description
String getInitparameter(String Name) It returns the Servlet initialization parameter of the given name and if the requested parameter is not available then it returns null.
Enumeration getInitparameter Names( ) It returns names of all Servlet initialization parameters defined in web.xml file.
String getServletName() Returns the servlet name as defined in the deployment descriptor .
ServletContext getServletContext() Returns the ServletContext object reference.

ServletConfig Example

Let us try out simple example to understand the working of the ServletConfig. Create a class named ServletconfigExample. This class shall create an object of ServletConfig and fetch the init-param from the deployment descriptor and print on the screen.

Listing 1: ServletconfigExample

package javabeat.net.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		ServletConfig config = getServletConfig();
		String S1 = config.getInitParameter("websitename");
		out.print("I'm : " + S1 + "website!!!!");

		out.close();
	}
}

Listing 2: 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>ServletconfigExample</servlet-name>
		<servlet-class>javabeat.net.servlets.ServletconfigExample</servlet-class>
		<init-param>
			<param-name>websitename</param-name>
			<param-value>Javabeat</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>ServletconfigExample</servlet-name>
		<url-pattern>/ServletconfigExample</url-pattern>
	</servlet-mapping>

</web-app>

Here we have defined the <init-param> which defines a param-name as websitename and param-value as Javabeat.

Now execute the ServletconfigExample, right click select Run>Run As and the following output appears:
ServletConfig

Previous Tutorial : sendRedirect in Servlet  || Next Tutorial : Difference Between JSP and Servlet

Category: Java EETag: 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.

Previous Post: « sendRedirect in Servlet
Next Post: OCPJP 6 / SCJP 1.6 Syllabus »

Reader Interactions

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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact