Differentiation between servletConfig and servletContext
Hello Guys,
I am totally confused between servletConfig and servletContext objects of servlet. Both of these objects seems to be have similar functionality in servlet. I know these objects of servlet is basically used to hold the references. But I am unable to find the differences between servletConfig and servletContext. Does anyone knows about the differentiation between servletConfig and servletContext? if yes then please let me know that.
Re: Differentiation between servletConfig and servletContext
The purpose ServletConfig is an interface. This interface contains the following four methods:
- getServletName():This returns a String containing the name of the instance of the servlet.
- getServletContext():This returns a reference to the interface ServletContext.
- getInitParameterNames() : This returns an object Enumeration containing the parameters initialization of the servlet.
- getInitParameter() : This returns a String which includes the initialization of the servlet parameter .
Re: Differentiation between servletConfig and servletContext
Hi friend,
The interface ServletContext includes methods getMajorVersion () and getMinorVersion () to indicate which version of the Servlet API is used. To access a resource from a servlet, it must pass through the ServletContext interface. For the ServletContext in a servlet (that is to say in a class inherits from HttpServlet), must be this.getServletContext (), which we must returns a ServletContext object reference.
Re: Differentiation between servletConfig and servletContext
The ServletContext object has two methods that : The method setAttribute (String attrName, Object attribValue), and the method getAttribute (String attrName). When you want a servlet, set a attribute of the application, that is to say, a global variable to the application, must be this.getServletContext (). To retrieve 'setAttribute ( "myAttribute", myObject)' object in a JSP page, do: myObject = application. setAttribute ( "myAttribute").
Re: Differentiation between servletConfig and servletContext
Javax.Servlet.ServletConfig is an interface. The ServletConfig object is an object which is passed to the method Init () to allow the servlet container (i.e the application server, e.g Tomcat) to pass configuration information to the servlet. The Init method of the Servlet interface is a method used to Initialization of the servlet. This method is called once, and just after instantiating the servlet. Initialization is the last step of loading a servlet. A Servlet must be initialized before responding to requests from the client.
Re: Differentiation between servletConfig and servletContext
The object type ServletConfig passed as a parameter to method and which enables access to an object of type ServletContext (because it implements the ServletContext interface) that describes the execution environment where the servlet is running. The servers generally have a log file for that, and servlet engines are no exception to the rule. To use these log files, it is recommended to use the method log (String message) or log (Exception e, String message) of the ServletContext interface. The first written message in the log message from the server, the second adds to the message stack trace (the result of e.printStackTrace ()).