The example below illustrates the scenario.
The code given below explains the same scenario.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% session= request.getSession(); session.setAttribute("username", "login"); %> <form action="login.jsp" method="post"> Username <input type="text" name="uname"><br/> <input type="submit"/> </form> </body> </html>
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <% response.setHeader("Cache-Control","no-cache"); response.setHeader("Cache-Control","no-store"); response.setHeader("Pragma","no-cache"); response.setDateHeader ("Expires", 0); if(session.getAttribute("username")==null) response.sendRedirect("index.jsp"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String uname = request.getParameter("uname") ; session = request.getSession(); session.setAttribute("username", uname); %> <h3>Hi <%out.println(uname); %></h3> <form action="page1.jsp" method="post"> Email <input type="text" name="email"/> <input type="submit"/> </form> </body> </html>
page1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <% response.setHeader("Cache-Control","no-cache"); response.setHeader("Cache-Control","no-store"); response.setHeader("Pragma","no-cache"); response.setDateHeader ("Expires", 0); if(session.getAttribute("username")==null) response.sendRedirect("index.jsp"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>PAGE 1</title> </head> <body> <% String email = request.getParameter("email") ; %> <h3>Hi <%out.println(email); %></h3> <form action="logout.jsp" method="post"> <input type="submit" value="LOGOUT"/> </form> </body> </html>
logout.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <% response.setHeader("Cache-Control","no-cache"); response.setHeader("Cache-Control","no-store"); response.setHeader("Pragma","no-cache"); response.setDateHeader ("Expires", 0); session.invalidate(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>LOGOUT</title> </head> <body> You have successfully logged out </body> </html>
No comments:
Post a Comment