First.java:
SecondApp.java:
Web.xml:
MyHtml.html:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class First extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); String name=request.getParameter("uname"); String pwd=request.getParameter("pwd"); HttpSession session=request.getSession(); session.setMaxInactiveInterval(10); if(name.equals("atharva")&&(pwd.equals("agrawal"))) { out.println("<h1> Login Success "); out.println("Session id is :"+session.getId()); out.println("Session CT is :"+session.getCreationTime()); out.println("Session Interval time is :"+session.getMaxInactiveInterval()); session.setAttribute("userattr",name); out.println("<a href='Second'> Go for Next servlet...</a>"); } else { out.println("<h1> Login Fails "); RequestDispatcher rd=request.getRequestDispatcher("/MyHtml.html"); rd.include(request, response); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
SecondApp.java:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SecondApp extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); HttpSession session=request.getSession(); out.println("<h3> Session ID is "+session.getId()); String fetchname=(String) session.getAttribute("userattr"); if(fetchname!=null) { out.println(" <h1><b> Name is :"+fetchname); } else { out.println(" <h2> Session Expires , please login again"); out.println("<a href='/MyHtml.html'> Go for Home page...</a>"); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
Web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>First</servlet-name> <servlet-class>First</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>SecondApp</servlet-name> <servlet-class>SecondApp</servlet-class> </servlet> <servlet-mapping> <servlet-name>First</servlet-name> <url-pattern>/First</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SecondApp</servlet-name> <url-pattern>/Second</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>MyHtml.html</welcome-file> </welcome-file-list> </web-app>
MyHtml.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>MyHtml.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="./First" method="post"> <center><h1><u>Session Programming example</u><br/></h1> <h2> Username: <input type="text" name="uname"/><br/> password : <input type="password" name="pwd"><br/> <input type="submit" name="Click" value="click here"> </h2> </center> </form> </body> </html>