<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>ServletContext</title>
</head>
<body>
<form action="MyServlet1">
<input type="submit" value="Go to Servlet1">
</form>
</body>
</html>
MyServlet1.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
ServletContext context=getServletContext();
context.setAttribute("name", "Abhi");
pw.println("We are in Servlet1 <br>");
pw.println("<a href='MyServlet2'>Click Me</a>");
pw.close();
}
}