Create a servlet demonstrating the use of session creation and destruction. Also check whether the user has visited this page first time or has visited earlier also using sessions.

index.html 

<html> 

    <head> 

        <title>Session Demo</title> 

    </head>  

    <form action="Page1" method="get" >    

        Enter UserName <input type="text" name="txtName"><br><br> 

        <input type="reset" value="Reset" > 

        <input type="submit" value="Submit"> 

    </form> 

</html> 

Page1.java

package mypack;  
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 Page1 extends HttpServlet { 
    public void doGet(HttpServletRequest request, HttpServletResponse response)                 throws ServletException, IOException {           
response.setContentType("text/html;charset=UTF-8");          
PrintWriter out = response.getWriter();  
         out.println("<html><head><title>Session Demo</title></head>");  
         String name = request.getParameter("txtName");           
         HttpSession hs = request.getSession(true);              
        if(hs.isNew()) 
         {          
            out.println("<body bgcolor=yellow>");                          
            hs.setAttribute("visit", "1");     
            out.println("<h1>Welcome First Time</h1>");    
         }                  
 else        
 {        
            out.println("<h1>Welcome Again</h1>"); 
        }   
        out.println("<body bgcolor=yellow>"); 
        out.println("<h1> UserName   : "+name+"</h1>"); 
        int visit = Integer.parseInt((String)hs.getAttribute("visit"))+1;              
        out.println("<h1>You Visited "+visit+"Times</h1>");             
        hs.setAttribute("visit", ""+visit);                  
        out.println("<h1>Your Session ID "+hs.getId()+"</h1>");     
        out.println("<h1>You Logged in at  "+new java.util.Date(hs.getCreationTime())+"</h1>");                    out.println("<h1><a href=LogoutServlet>Click to Terminate Session </a></h1>"); 
    } 

LogoutServlet.java
package mypack; 
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; 
public class LogoutServlet extends HttpServlet {  
    public void doGet(HttpServletRequest request, HttpServletResponse response)                throws                         ServletException, IOException {             
                 response.setContentType("text/html;charset=UTF-8");            
                 PrintWriter out = response.getWriter();        
                 out.println("<html><head><title>Servlet LogoutServlet</title></head>");             
                 out.println("<body bgcolor=red>");         
                 javax.servlet.http.HttpSession hs = request.getSession();             
                 if(hs != null)             
                 hs.invalidate();                
                 out.println("</body>");  
                 out.println("<h1>You are Logged out now........</h1>");                
                 out.println("</html>");   
    }    
}  



Post a Comment

0 Comments