#
Servlet Context
This tutorial explains to you what a ServletContext object is in Java.
The ServletContext is an object that contains meta information about your web application. This information is shared between all the session (for all users) that receive information passing through that web-container. For distributed servers (clusters) each container has its own context.
Here is an example of using ServletContext object.
For this example I will create a Dynamic Web Application using Eclipse. This Web Application will have 2 servlets:
MyJavaServlet : will set a parameter on the application (container) level
MyJavaServlet2 : will read that parameter (the same result is if this page is from another user/ session)
package servlets;
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 MyJavaServlet
*/
@WebServlet("/MyJavaServlet")
public class MyJavaServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyJavaServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = request.getSession().getServletContext();
context.setAttribute("user_name", "David");
PrintWriter myResponse = response.getWriter();
String message = "This servlet set a context variable using ServletContext object. ";
myResponse.println( "\n" +"<title>This servlet set a context variable using ServletContext object.</title>\n" +
"\n" +
"<h2 align="\"center\"">" + message + "</h2>\n" +
"<p> </p> <p>Link to MyJavaServlet2 page : "+
" <a href="http://localhost:8080/JAVA_servlet_example/MyJavaServlet2">link</a></p>"+
"");
}
}
package servlets;
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 MyJavaServlet
*/
@WebServlet("/MyJavaServlet2")
public class MyJavaServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyJavaServlet2() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = request.getSession().getServletContext();
String uname = (String) context.getAttribute("user_name");
PrintWriter myResponse = response.getWriter();
String message = "This servlet read a context variable using ServletContext object. ";
myResponse.println( "\n" +"<title>This servlet read a context variable using ServletContext object.</title>\n" +
"\n" +
"<h2 align="\"center\"">" + message + "</h2>\n" +
"<p> </p> <p> The <b>User Name </b> read from the ServletContext object is <b>"+ uname+ "</b>.</p>"+
"");
}
}
The following screen is the result of MyJavaServlet servlet:
The following screen is the result of MyJavaServlet2 servlet: