index.html
<!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();
}
}
<!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();
}
}
MyServlet2.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 MyServlet2 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();
String name=(String)context.getAttribute("name");
pw.println("We are in Servlet2<br>");
pw.println("Welcome "+name);
pw.close();
}
}
web.xml
<servlet>
<description></description>
<display-name>MyServlet1</display-name>
<servlet-name>MyServlet1</servlet-name>
<servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet1</servlet-name>
<url-pattern>/MyServlet1</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>MyServlet2</display-name>
<servlet-name>MyServlet2</servlet-name>
<servlet-class>MyServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet2</servlet-name>
<url-pattern>/MyServlet2</url-pattern>
</servlet-mapping>
0 comments:
Post a Comment