Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
85 views
in RTU B.Tech (CSE-V Sem) Advance Java Lab by Goeduhub's Expert (5.8k points)

ServletConfig Interface in Java

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
 
Best answer

ServletConfig Interface

An object of ServletConfig is created by the web container for each servlet. This object can be used to get configuration information from web.xml file. If the configuration information is modified from the web.xml file, we don't need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time.

Note: The core advantage of ServletConfig is that you don't need to edit the servlet file if information is modified from the web.xml file.

Methods of ServletConfig interface

Method Description
public String getInitParameter(String name) Returns the parameter value for the specified parameter name
public Enumeration getInitParameterNames() Returns an enumeration of all the initialization parameter names
public String getServletName() Returns the name of the servlet
public ServletContext getServletContext() Returns an object of ServletContext

Example Code

DemoServ.java

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 DemoServ extends HttpServlet {  

public void doGet(HttpServletRequest request, HttpServletResponse response)  

        throws ServletException, IOException { 

    response.setContentType("text/html");  

    PrintWriter out = response.getWriter();  

    ServletConfig config=getServletConfig();  

    Enumeration<String> e=config.getInitParameterNames();     

    String str="";  

    while(e.hasMoreElements()){  

    str=e.nextElement();  

    out.print("<br>Name: "+str);  

    out.print(" value: "+config.getInitParameter(str));  

    }         

    out.close();  

}  

}  

index.html
<a href="servlet1">Click here</a>

web.xml

<?xml version="1.0"?>

<web-app>

<servlet>

<servlet-name>DemoServ</servlet-name>

<servlet-class>DemoServ</servlet-class>

<init-param>

<param-name>username</param-name>

<param-value>system</param-value>

</init-param>

<init-param>

<param-name>password</param-name>

<param-value>oracle</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>DemoServ</servlet-name>

<url-pattern>/servlet1</url-pattern>

</servlet-mapping>

</web-app>

Output

Output 1

Output 2


For more RTU V Sem Advance Java Lab Experiments Click here


3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...