Servlet Config Interface

Servlet Config Interface:

A protest of ServletConfig is made by the web compartment for each servlet. This protest can be utilized to get arrangement data from web.xml document.

On the off chance that the arrangement data is adjusted from the web.xml document, we don’t have to change the servlet. So it is less demanding to deal with the web application if a particular substance is altered every once in a while.

 Advantage of ServletConfig :

The center favorable position of ServletConfig is that you don’t have to alter the servlet document if data is adjusted from the web.xml record.

 Methods for ServletConfig interface:

  1. public String getInitParameter(String name)
  2. public Enumeration getInitParameterNames()
  3. public String getServletName()
  4. public ServletContext getServletContext()

 object of ServletConfig :

Syntax :

public ServletConfig getServletConfig();

DemoServlet.java

Example:

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class DemoServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

ServletConfig config=getServletConfig()

String driver=config.getInitParameter(“river”);

out.print(“Driver is: “+driver);

out.close();

}

}
web.xml

<web-app>

<servlet>

<servlet-name>DemoServlet</servlet-name>

<servlet-class>DemoServlet</servlet-class>

<init-param>

<param-name>driver</param-name>

<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>DemoServlet</servlet-name>

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

</servlet-mapping>

</web-app>

Servlet Context:

A challenge of ServletRequest is used to give the client request information to a servlet, for instance, content sort, content length, parameter names and qualities, header informations, attributes.

Advantage :

Simple to keep up if any data is shared to all the servlet, it is ideal to make it accessible for all the servlet. We give this data from the web.xml record, so if the data is transformed, we don’t have to alter the servlet. In this manner it expels upkeep issue.

 

methods of ServletContext interface:

           public String getInitParameter(String name)

           public Enumeration getInitParameterNames()

           public void setAttribute(String name,Object object)

           public Object getAttribute(String name)

           public Enumeration getInitParameterNames()

          Syntax of getServletContext() method:

              public ServletContext getServletContext()  DemoServlet.java

Example:

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class DemoServlet extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException,IOException

{

res.setContentType(“text/html”);

PrintWriter pw=res.getWriter();

//creating ServletContext object

ServletContext context=getServletContext();

//Getting the value of the initialization parameter and printing it

String driverName=context.getInitParameter(“dname”);

pw.println(“driver name is=”+driverName);

pw.close();

}}

 web.xml:

<web-app>

<servlet>

<servlet-name>sonoojaiswal</servlet-name>

<servlet-class>DemoServlet</servlet-class>

</servlet>

<context-param>

<param-name>dname</param-name>

<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>

</context-param>

<servlet-mapping>

<servlet-name>sonoojaiswal</servlet-name>

<url-pattern>/context</url-pattern>

</servlet-mapping>

</web-app>

 

Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.