Pin It

How to use Servlets Initialization Parameters through ServletConfig object in Java EE Application ?.

A simple Servlet code to demonstrate how to use Servlets Initialization Parameters through ServletConfig object in Java EE Application.

1. Implementing Initialization Parameters in web.xml as name/value pair.



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
    <servlet>
        <servlet-name>InitParameterDemo</servlet-name>
        <servlet-class>com.hubberspot.init.parameter.InitParameterDemo</servlet-class>
        <init-param>
            <param-name>Username</param-name>
            <param-value>Jontymagicman</param-value>
        </init-param>
        <init-param>
            <param-name>Website</param-name>
            <param-value>Hubberspot</param-value>
        </init-param>
        <init-param>
            <param-name>Email</param-name>
            <param-value>jonty@hubberspot.com</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>InitParameterDemo</servlet-name>
        <url-pattern>/InitParameterDemo</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>



2. Servlet Code using Initialization Parameters implemented above in web.xml

package com.hubberspot.init.parameter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
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 InitParameterDemo extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html");
        
        PrintWriter out = response.getWriter();
        
         try {
            
            out.println("");
            out.println("");
            out.println("Init Parameter Demo");            
            out.println("");
            out.println("");
            out.println("

Init-Parameters Demo (ServletConfig)

"); ServletConfig config = this.getServletConfig(); Enumeration params = config.getInitParameterNames(); int counter = 1; while(params.hasMoreElements()){ String paramName = (String) params.nextElement(); out.print(counter + ". param name = " + paramName + ""); out.println(" "+ counter + ". param value = " + config.getInitParameter(paramName) + " "); counter++; } out.println("Username is : " + config.getInitParameter("Username") + "" ); out.println("Email is : " + config.getInitParameter("Email") + "" ); out.println("Website is : " + config.getInitParameter("Website") + "" ); out.println(""); out.println(""); } finally { out.close(); } } }


Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot