Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ServletConfig Interface in servlets

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 380
    Comment on it

    ServletConfig interface:

    For each servlet, an instance of this interface is created by the container. With the help of this instance, information can be fetched from the configuration file (web.xml).

    Advantage:

    You don't need need to do changes in the servlet if any modifications are required. The configuration file can be updated and the changes will be reflected in the particular servlet. e.g. Database information can be kept in the configuration file, and if there is a migration to a new database, then you need to do the changes only in the web.xml file and it will be automatically reflected in the required servlet.

    Get ServletConfig Object:

    getServletConfig() This method of the interface is used to obtain the instance of ServletConfig.

    To the value of the particular parameter we make use of the following methods:

    1) public String getInitParameter(String name):Fetch the value for the given name.
    2) public Enumeration getInitParameterNames():Fetch an enumeration of all the parameter names.

    Example:

    web.xml - In this file we specify the parameters.

      <servlet>  
        <servlet-name>Demo</servlet-name>  
        <servlet-class>Demp</servlet-class>  
    
        <init-param>  
        <param-name>name</param-name>  
        <param-value>Roth</param-value>  
        </init-param>  
       <init-param>  
        <param-name>password</param-name>  
        <param-value>Roth</param-value>  
        </init-param>  
    
        </servlet>  
    
        <servlet-mapping>  
        <servlet-name>Demo</servlet-name>  
        <url-pattern>/demo</url-pattern>  
        </servlet-mapping>  
    
        </web-app>  
    

    Demo.java - Servlet used to fetch init parameters

    import java.io.*;  
    import javax.servlet.*;  
    import javax.servlet.http.*;  
    
    public class Demo extends HttpServlet {  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
    
        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  
    
        ServletConfig config=getServletConfig();  // getting the interface object
        String name=config.getInitParameter("name"); //fetching the name from the configuration file i.e. Roth
        String password=config.getInitParameter("password");//fetching the password from the configuration file i.e Roth
        out.print("Name is: "+name);    // This will print Roth in the output
        out.close();  
        }  
    
    }  
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: