Context variable can access thorough the entire application. Shared across servlets and users .But session object can't be shared across the user . so it the is an advantage of the context variable over session. Now lets consider how context can be created and use the same across the entire application: package servlets.first; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class SessionTest */ @WebServlet(description = "Write data to session and read data from session and from context as well", urlPatterns = { "/SessionTest" }) public class SessionTest extends HttpServlet { private static final long se
Master Core Java, JSP, Servlet, Hibernate, Maven, Spring, and more with our comprehensive tutorials and guides. Start learning today!