Saturday, September 19, 2015

Difference between ServletConfig and ServletContext

In this post, we are going to see the difference between ServletConfig and ServletContext.

Difference between ServletConfig and ServletContext:

ServletConfigServletContext(It should have been ApplicationContext)
One ServletConfig object is created per servlet One ServletContext object is created per Application
Can be used to pass deployment time information to the Servlet. Information is stored as parameters in the deployment descriptor(web.xml) Can be used to share Application level information across servlets and is this information is also stored in the web.xml as well as parameters (So it kind of acts as GlobalContext or Application Context)
Provides a set of methods to set and get the initialization configuration information Provides a set of methods to the servlet so that servlet can communicate with the Container
------------------------Servlet can access a ServletContext object only through ServletConfig object

Working with ServletConfig:

web.xml: 
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.ranga.LoginServlet</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>ranga</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>ranga</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
LoginServlet.java    
package com.ranga;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Ranga Reddy
* @version 1.0
*/

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = -2226127949360374211L;
protected String username = null, password = null;

public void init(ServletConfig servletConfig) throws ServletException {
this.username = servletConfig.getInitParameter("username");
this.password = servletConfig.getInitParameter("password");
}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("<html><body> Username:" + this.username + ", Password: "+this.password+" </body></html>");
}
}
Getting init parameters inside the servlet
servletConfig.getInitParameter("username");

Working with ServletContext:

web.xml:
 <context-param>
<param-name>username</param-name>
<param-value>ranga</param-value>
</context-param>

<context-param>
<param-name>password</param-name>
<param-value>ranga</param-value>
</context-param>

<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.ranga.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
LoginServlet.java
package com.ranga;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Ranga Reddy
* @version 1.0
*/

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = -2226127949360374211L;
protected String username = null, password = null;

public void init(ServletConfig servletConfig) throws ServletException {

ServletContext servletContext = servletConfig.getServletContext();
this.username = servletContext.getInitParameter("username");
this.password = servletContext.getInitParameter("password");
}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("<html><body> Username:" + this.username + ", Password: "+this.password+" </body></html>");
}
}
Accessing context params in Servlet
servletConfig.getServletContext().getInitParameter("username");
Accessing context params in JSP
<% getServletContext().getInitParameter("username"); %>

0 comments: