Servlets - SPLessons

Servlet Config

Chapter 15

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Config

Servlet Config

shape Description

Servlet Config, The Servlet is a Java program which can run in Web server or Application server and act as a mediator from a web browser or client to the database or Server. For this Servlet, one can take input from the users through HTML or Web forms and can also take present records from databases and create web forms dynamically.

Servlet Config object is created by servlet container for every servlet. It is used to get the information from the web.xml as a param-value. That means when param-name is called in the web.xml, it gives the pram-value. ServletConfig can manage without disturbing servlet program. Go to web.xml. Change the param-value and call param-name. So it is easy to modify the content. Following is the syntax.

First create: ServletConfig config = getServletConfig();
Next call: String param_value = config.getCofig();

shape Methods

Method Description
getInitParameter(String name) It returns the param-value when param-name is called and the return type is string .
getServletContext() It returns the ServletContext object and the return type is ServletContext.
getInitParameterNames() It returns the names of servlet initialization param-values as a enumeration and return type is Enumeration.
getServletName() It returns name of the servlet instance like server administer and the return type is string.

shape Example


<a href="servlet1">click here</a>

Here just created a button click here, when click on the button parameters will be passed from web.xml file.

package config2;

import javax.servlet.*;
import javax.servlet.http.*;

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

		PrintWriter out = response.getWriter();
		ServletConfig config=getServletConfig();
		String splesson=config.getInitParameter("splesson");
		out.print("splessons: "+splesson);


The getServletConfig() method of Servlet interface returns the object of ServletConfig. Here the purpose of setContentType(“text/html”) is that, It basically tells the client what content type it is so that it knows what to do with it. The doGet() method is utilized to send parameters to an URL along with the header information. The PrintWriter is utilized print content information to a character stream and getWriter Returns a PrintWriter object that can send character content to the client.


		<param-value>Hey man stop thinking start coding</param-value>

Make sure that servlet name should be same, here parameters passing from the web.xml file.


By compiling the code the following output will be generated.

By clicking on the link following message will be displayed. This output message was placed in parameters.

Difference between ServletConfig and ServletContext


  • It is available in javax.servlet.*; package
  • It’s object is one per servlet class
  • The tag will be appear under tag


  • It is available in javax.servlet.*; package
  • It’s object is global to all.
  • The tag will be appear under tag


shape Points

  • Servlet Config – The parameters will be called from web.xml file
  • If data is changed in the web.xml then no need to disturb servelt file.
  • In Servlet Config object will be created by web container to retrieve the data from web.xml.