Weekend -Special offer upto 50% off | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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.javaExample: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

[embed]https://www.youtube.com/watch?v=UbpbqqIw_3Q&t=1725s [/embed]web.xmlDemoServletclass>DemoServletclass>driversun.jdbc.odbc.JdbcOdbcDriverDemoServlet/servlet1
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.javaExample: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 objectServletContext context=getServletContext();//Getting the value of the initialization parameter and printing itString driverName=context.getInitParameter("dname");pw.println("driver name is="+driverName);pw.close();}} web.xml:sonoojaiswalclass>DemoServletclass>dnamesun.jdbc.odbc.JdbcOdbcDriversonoojaiswal/context