CodesJava

Easy learning with example program codes

Servlet Init parameters and ServletConfig interface


Init parameters:

Init parameters refers to the initialization parameters of a servlet or filter. <init-param> attribute is used to define a init parameter. <init-param> attribute has two main sub attributes <param-name> and <param-value>. The <param-name> contains the name of the parameter and <param-value> contains the value of the parameter.

ServletConfig interface:

ServletConfig interface is used to access the init parameters.

Methods of ServletConfig interface:

1. getInitParameter(String name): Returns the value of the specified parameter if parameter exist otherwise return null.

Syntax: public String getInitParameter(String name)

2. getInitParameterNames(): Returns the names of init parameters as Enumeration if servlet has init parameters otherwise returns an empty Enumeration.

Syntax: public Enumeration getInitParameterNames()

3. getServletContext():Returns an instance of ServletContext.

Syntax: public ServletContext getServletContext()

4. getServletName():Returns the name of the servlet.

Syntax: public String getServletName()

Example:

InitParamExample.java

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;
 
/**
 * This class is used to show the use of init parameters.
 * @author codesjava
 */
public class InitParamExample extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
        //no-argument constructor
        public InitParamExample() {
 
        }
 
      protected void doGet(HttpServletRequest request, 
		HttpServletResponse response)
	          throws ServletException, IOException {
	response.setContentType("text/html"); 
    	PrintWriter out = response.getWriter();
    	//get ServletConfig object.
    	ServletConfig config=getServletConfig(); 
    	//get init parameter from ServletConfig object.
    	String appUser = config.getInitParameter("appUser");
 
    	out.print("<h1>Application User: " + appUser + "</h1>");
 
    	out.close();
     }
}

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">
 
<servlet>
    <servlet-name>InitParamExample</servlet-name>
    <servlet-class>
    	com.codesjava.business.InitParamExample
    </servlet-class>
    <init-param>
    	<param-name>appUser</param-name>
    	<param-value>jai</param-value>
    </init-param>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>InitParamExample</servlet-name>
    <url-pattern>/InitParamExample</url-pattern>
  </servlet-mapping>
 
</web-app>

Output:

servlet example 7
 
Download this example.
 
Next Topic: Servlet context parameters and ServletContext interface with example.
Previous Topic: sendRedirect in servlet with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki