Pin It

How to use Context Parameters in Servlets through ServletContext object in Java EE Application ?.

A simple Servlet code to demonstrate how to use Context Parameters through ServletContext object in Java EE Application.


1. Implementing Context 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">

    <context-param>
        <param-name>Username</param-name>
        <param-value>Jontymagicman</param-value>
    </context-param>
    <context-param>
        <param-name>Website</param-name>
        <param-value>Hubberspot</param-value>
    </context-param>
    <context-param>
        <param-name>Email</param-name>
        <param-value>jonty@hubberspot.com</param-value>
    </context-param>
    <servlet>
        <servlet-name>ContextParameterDemo</servlet-name>
        <servlet-class>com.hubberspot.context.parameter.ContextParameterDemo</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ContextParameterDemo</servlet-name>
        <url-pattern>/ContextParameterDemo</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>


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

package com.hubberspot.context.parameter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContextParameterDemo extends HttpServlet {

    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("Context Parameter Demo");            
            out.println("");
            out.println("");
            out.println("

Context-Parameters Demo (ServletContext)

"); ServletContext context = this.getServletContext(); Enumeration params = context.getInitParameterNames(); int counter = 1; while(params.hasMoreElements()){ String paramName = (String) params.nextElement(); out.print(counter + ". param name = " + paramName + ""); out.println(" "+ counter + ". param value = " + context.getInitParameter(paramName) + " "); counter++; } out.println("Username is : " + context.getInitParameter("Username") + "" ); out.println("Email is : " + context.getInitParameter("Email") + "" ); out.println("Website is : " + context.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