Tuesday 15 March 2016

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.

Advantage of ServletConfig

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

  1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name.
  2. public Enumeration getInitParameterNames():Returns an enumeration of all the initialization parameter names.
  3. public String getServletName():Returns the name of the servlet.
  4. public ServletContext getServletContext():Returns an object of ServletContext.


How to get the object of ServletConfig

  1. getServletConfig() method of Servlet interface returns the object of ServletConfig.

Syntax of getServletConfig() method


  1. public ServletConfig getServletConfig();  

Example of getServletConfig() method


  1. ServletConfig config=getServletConfig();  
  2. //Now we can call the methods of ServletConfig interface  


Syntax to provide the initialization parameter for a servlet

The init-param sub-element of servlet is used to specify the initialization parameter for a servlet.

  1. <web-app>  
  2.   <servlet>  
  3.     ......  
  4.       
  5.     <init-param>  
  6.       <param-name>parametername</param-name>  
  7.       <param-value>parametervalue</param-value>  
  8.     </init-param>  
  9.     ......  
  10.   </servlet>  
  11. </web-app>  


Example of ServletConfig to get initialization parameter

In this example, we are getting the one initialization parameter from the web.xml file and printing this information in the servlet.

DemoServlet.java

  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5. public class DemoServlet extends HttpServlet {  
  6. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  7.     throws ServletException, IOException {  
  8.   
  9.     response.setContentType("text/html");  
  10.     PrintWriter out = response.getWriter();  
  11.       
  12.     ServletConfig config=getServletConfig();  
  13.     String driver=config.getInitParameter("driver");  
  14.     out.print("Driver is: "+driver);  
  15.           
  16.     out.close();  
  17.     }  
  18.   
  19. }  

web.xml

  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>DemoServlet</servlet-name>  
  5. <servlet-class>DemoServlet</servlet-class>  
  6.   
  7. <init-param>  
  8. <param-name>driver</param-name>  
  9. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  10. </init-param>  
  11.   
  12. </servlet>  
  13.   
  14. <servlet-mapping>  
  15. <servlet-name>DemoServlet</servlet-name>  
  16. <url-pattern>/servlet1</url-pattern>  
  17. </servlet-mapping>  
  18.   
  19. </web-app>  

ServletContext Interface


n object of ServletContext is created by the web container at time of deploying the project. This object can be used to get configuration information from web.xml file. There is only one ServletContext object per web application.
If any information is shared to many servlet, it is better to provide it from the web.xml file using the <context-param> element.

Advantage of ServletContext

Easy to maintain if any information is shared to all the servlet, it is better to make it available for all the servlet. We provide this information from the web.xml file, so if the information is changed, we don't need to modify the servlet. Thus it removes maintenance problem.

Usage of ServletContext Interface

There can be a lot of usage of ServletContext object. Some of them are as follows:
  1. The object of ServletContext provides an interface between the container and servlet.
  2. The ServletContext object can be used to get configuration information from the web.xml file.
  3. The ServletContext object can be used to set, get or remove attribute from the web.xml file.
  4. The ServletContext object can be used to provide inter-application communication.
ServletContext interface

Commonly used methods of ServletContext interface

There is given some commonly used methods of ServletContext interface.
  1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name.
  2. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters.
  3. public void setAttribute(String name,Object object):sets the given object in the application scope.
  4. public Object getAttribute(String name):Returns the attribute for the specified name.
  5. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.
  6. public void removeAttribute(String name):Removes the attribute with the given name from the servlet context.


How to get the object of ServletContext interface

  1. getServletContext() method of ServletConfig interface returns the object of ServletContext.
  2. getServletContext() method of GenericServlet class returns the object of ServletContext.

Syntax of getServletContext() method


  1. public ServletContext getServletContext()  

Example of getServletContext() method


  1. //We can get the ServletContext object from ServletConfig object  
  2. ServletContext application=getServletConfig().getServletContext();  
  3.   
  4. //Another convenient way to get the ServletContext object  
  5. ServletContext application=getServletContext();  


Syntax to provide the initialization parameter in Context scope

The context-param element, subelement of web-app, is used to define the initialization parameter in the application scope. The param-name and param-value are the sub-elements of the context-param. The param-name element defines parameter name and and param-value defines its value.

  1. <web-app>  
  2.  ......  
  3.       
  4.   <context-param>  
  5.     <param-name>parametername</param-name>  
  6.     <param-value>parametervalue</param-value>  
  7.   </context-param>  
  8.  ......  
  9. </web-app>  


Example of ServletContext to get the initialization parameter

In this example, we are getting the initialization parameter from the web.xml file and printing the value of the initialization parameter. Notice that the object of ServletContext represents the application scope. So if we change the value of the parameter from the web.xml file, all the servlet classes will get the changed value. So we don't need to modify the servlet. So it is better to have the common information for most of the servlets in the web.xml file by context-param element. Let's see the simple example:

DemoServlet.java

  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5.   
  6. public class DemoServlet extends HttpServlet{  
  7. public void doGet(HttpServletRequest req,HttpServletResponse res)  
  8. throws ServletException,IOException  
  9. {  
  10. res.setContentType("text/html");  
  11. PrintWriter pw=res.getWriter();  
  12.   
  13. //creating ServletContext object  
  14. ServletContext context=getServletContext();  
  15.   
  16. //Getting the value of the initialization parameter and printing it  
  17. String driverName=context.getInitParameter("dname");  
  18. pw.println("driver name is="+driverName);  
  19.   
  20. pw.close();  
  21.   
  22. }}  
web.xml

  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>sonoojaiswal</servlet-name>  
  5. <servlet-class>DemoServlet</servlet-class>  
  6. </servlet>  
  7.   
  8. <context-param>  
  9. <param-name>dname</param-name>  
  10. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  11. </context-param>  
  12.   
  13. <servlet-mapping>  
  14. <servlet-name>sonoojaiswal</servlet-name>  
  15. <url-pattern>/context</url-pattern>  
  16. </servlet-mapping>  
  17.   
  18. </web-app>  


Example of ServletContext to get all the initialization parameters

In this example, we are getting all the initialization parameter from the web.xml file. For getting all the parameters, we have used the getInitParameterNames() method in the servlet class.

DemoServlet.java

  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5.   
  6. public class DemoServlet extends HttpServlet{  
  7. public void doGet(HttpServletRequest req,HttpServletResponse res)  
  8. throws ServletException,IOException  
  9. {  
  10. res.setContentType("text/html");  
  11. PrintWriter out=res.getWriter();  
  12.   
  13. ServletContext context=getServletContext();  
  14. Enumeration<String> e=context.getInitParameterNames();  
  15.       
  16. String str="";  
  17. while(e.hasMoreElements()){  
  18.     str=e.nextElement();  
  19.     out.print("<br> "+context.getInitParameter(str));  
  20. }  
  21. }}  
web.xml

  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>sonoojaiswal</servlet-name>  
  5. <servlet-class>DemoServlet</servlet-class>  
  6. </servlet>  
  7.   
  8. <context-param>  
  9. <param-name>dname</param-name>  
  10. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  11. </context-param>  
  12.   
  13. <context-param>  
  14. <param-name>username</param-name>  
  15. <param-value>system</param-value>  
  16. </context-param>  
  17.   
  18. <context-param>  
  19. <param-name>password</param-name>  
  20. <param-value>oracle</param-value>  
  21. </context-param>  
  22.   
  23. <servlet-mapping>  
  24. <servlet-name>sonoojaiswal</servlet-name>  
  25. <url-pattern>/context</url-pattern>  
  26. </servlet-mapping>  
  27.   
  28. </web-app>  

No comments:

Post a Comment