Searching...
Sunday 8 June 2014

CREATE A LOGIN APPLICATION USING SESSION USING SERVLET

6/08/2014 09:26:00 am
Login.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="Login",urlPatterns="/Login")
public class Login extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException    {
        HttpSession snsobj=request.getSession(true);       
        response.setContentType("text/html");
        PrintWriter printwriter=response.getWriter();
        printwriter.println("<html>");
        printwriter.println("<head>");
        printwriter.println("<title>Login</title>");
        printwriter.println("</head>");
        printwriter.println("<body>");
        printwriter.println("<form action=\"logindemo\" method=\"post\">");
        printwriter.println("<p> <h2>Login</h2></p>");
        printwriter.println("<p> User Name : " +  "<input type=\"text\" name=\"uname\"> </p>");
        printwriter.println("<p> Password : "  +  "<input type=\"password\" name=\"pass\"> </p>");
        printwriter.println("<p><input type=\"submit\" name=\"submit\" value=\"submit\">" + "<input type=\"submit\" name=\"cancel\" value=\"cancel\">  </p>");
        printwriter.println("</form>");
        printwriter.println("</body>");
        printwriter.println("</html>");           
    }  
}

Logindemo.java

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="logindemo",urlPatterns="/logindemo")
public class logindemo extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException    {
        HttpSession snsobj=request.getSession(true);
        response.setContentType("text/html");
        PrintWriter printwriter=response.getWriter();
        printwriter.println("<html>");
        printwriter.println("<head>");
        printwriter.println("<title>Login</title>");
        printwriter.println("</head>");
        printwriter.println("<body>");
        Enumeration enm=request.getParameterNames();
        String unam="";
        String pas="";
        int i;
        String[] pval=request.getParameterValues((String)enm.nextElement());
        for(i=0;i<pval.length;i++) {
            unam=unam+pval[i];
        }
        pval=request.getParameterValues((String)enm.nextElement());
        for(i=0;i<pval.length;i++){
            pas=pas+pval[i];
        }
        if(unam.equals("admin") && pas.equals("admin")){
            snsobj.setAttribute("uidnm", unam);
            response.sendRedirect("/simpeservlet/logindemo1");     }
        else {
             printwriter.print("User Name or Passwors is Wrong ");
             String url="/simpeservlet/Login";
             printwriter.print("<a href=" + url  + ">Back </a>");    }
        printwriter.println("</body>");
        printwriter.println("</html>");     }
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
        doGet(request,response);    }   
}

Logindemo1.java

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="logindemo1",urlPatterns="/logindemo1")
public class logindemo1 extends HttpServlet{
     public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException  {
        HttpSession snsobj=request.getSession(true);
        PrintWriter printwriter=response.getWriter();
        response.setContentType("text/html");
        try{
        String UN= (String) snsobj.getAttribute("uidnm");   
        printwriter.println("<html>");
        printwriter.println("<head>");
        printwriter.println("<title>Login</title>");
        printwriter.println("</head>");
        printwriter.println("<body>");
        printwriter.println("<form action=\"logout\" method=\"post\">");
        if(UN.length()>0) {
        printwriter.println("<h4>" + "Welcome :  " + UN  + "</h4>");
        printwriter.println("<input type=\"submit\" name=\"submit\" value=\"LogOut\">");      
        } else{
            printwriter.println("Error..  please login...");  }       
        } catch(Exception e){
            printwriter.println("Error..  please login...");
        }
        printwriter.println("</form>");
        printwriter.println("</body>");
        printwriter.println("</html>");
    }
}

Logout.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="logout",urlPatterns="/logout")
public class logout extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException    {
     HttpSession snsobj=request.getSession(true);
     snsobj.setAttribute("uidnm","");
     response.sendRedirect("/simpeservlet/Login");    }
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
    {
        doGet(request,response);
    }

}

0 comments:

Post a Comment