Servlet Config Interface

Click to rate this post!
[Total: 0 Average: 0]

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();

}

}

JAVA Tutorial Video

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>

 

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification 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.


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

Call Now Button