AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-servlet: What is the difference between ServletConfig and ServletContext?

Please explain the key difference. I am very much confused.

java x 211
servlet x 20
Posted On : 2014-04-05 23:30:16.0
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
up-rate
5
down-rate

Answers


The object of ServletConfig is provided by web container for each servlet. It is used to get the configuration data from web.xml file. It makes easier to manage the web applications. As we can change the configuration information in web.xml file easily without making any changes in servlets. It is the main advantage of using ServletConfig.
Note: getServletConfig() method of Servlet interface is used to get the object of ServletConfig.

Let´s see the small example of ServletConfig. First we define some configuration information in web.xml file as following.

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>


Following is the Servlet code i.e. DemoServlet
DomoServlet.java:
--------------------------
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("driver");
out.print("Driver is: "+driver);

out.close();
}

}


--------------------------------------------------------------------------------------------------------------------------------

The object of ServletContext is also provided by web container but at the time of deploying the application. It can be used to get the information from web.xml file. There is only one ServletContext object per web application.

If any information is needed to share to many servlet, it is better to provide it in the web.xml file using the <context-param> element.

Note:
1. getServletContext() method of ServletConfig interface is used to get the object of ServletContext.
2. getServletContext() method of GenericServlet class is used to get the object of ServletContext.

Let´s see the example of ServeltContext:

web.xml:
--------------

<web-app>

<servlet>
<servlet-name>anyforum</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>

<context-param>
<param-name>driver</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>

<servlet-mapping>
<servlet-name>anyforum</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping>

</web-app>

DemoServlet.java:
--------------------------
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("driver");
pw.println("driver name is="+driverName);

pw.close();

}}


Note: ServletConfig parameters are put under servlet tag in web.xml file. While Context parameters are put in <context-param> under <web-app>. Context parameters are remains same for all servlet and Config parameters may change servlet to servlet.

Posted On : 2014-04-06 00:20:26
Satisfied : 2 Yes  0 No
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939909
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in