Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Showing posts with label Servlets. Show all posts
Showing posts with label Servlets. Show all posts

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"); %>