❮ Previous Next ❯

Login & Logout Application using HttpSession

Hi friends, let’s see a login and logout application using HttpSession

Required Files:

  1. index.html
  2. link.html
  3. LoginServlet.java
  4. LogoutServlet.java
  5. ProfileServlet.java
  6. web.xml
First look the following project structure:
servlet-login-1
index.html

<html>
<body>
<h1>Login Application using HttpSession</h1>
<a href="login.html">Login</a>
<a href="LogoutServlet">Logout</a>
<a href="ProfileServlet">Profile</a>
</body>
</html>


link.html

<html>
<body>
<a href="login.html">Login</a>
<a href="LogoutServlet">Logout</a>
<a href="ProfileServlet">Profile</a>
<hr>
</body>
</html>


login.html

<html>
<body>
<form action="LoginServlet">
<table>
<tr>
<td>UserName</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td><input type="submit" value="login"></td>
</tr>
</table>
</form>
</body>
</html>

LoginServlet.java

package java8s;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LoginServlet extends HttpServlet {
    
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
          response.setContentType("text/html");
          PrintWriter out=response.getWriter();
         
          request.getRequestDispatcher("link.html").include(request, response);
         
          String s1=request.getParameter("uname");
          String s2=request.getParameter("pass");
         
          if(s2.equals("silan"))
          {
              out.print("login is successful");
              HttpSession session=request.getSession();
              session.setAttribute("user", s1);
          }
          else
          {
              out.print("sry!! invalid password");
               request.getRequestDispatcher("login.html").include(request, response);
          }
     }
}



LogoutServlet.java

package java8s;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LogoutServlet extends HttpServlet {
    
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
          response.setContentType("text/html");
          PrintWriter out=response.getWriter();
         
          HttpSession session=request.getSession();
          session.invalidate();
         
          out.print("U have successfully logged out");
     }
}

ProfileServlet.java

package java8s;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ProfileServlet extends HttpServlet {
    
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
          response.setContentType("text/html");
          PrintWriter out=response.getWriter();
         
          HttpSession session=request.getSession(false);
         
          if(session!=null)
          {
              String s=(String)session.getAttribute("user");
              out.print("Hii"+s+""+"Welcome 2 SILAN TECHNOLOGY");
          }
          else
          {
              out.print("sry!!U have to login first");
               request.getRequestDispatcher("login.html").include(request, response);
          }
         
         
         
     }
}


web.xml

<web-app>
<display-name>LoginApp</display-name>
  <servlet>
  <servlet-name>abc</servlet-name>
  <servlet-class>java8s.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>abc</servlet-name>
  <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
 
  <servlet>
  <servlet-name>def</servlet-name>
  <servlet-class>java8s.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>def</servlet-name>
  <url-pattern>/LogoutServlet</url-pattern>
  </servlet-mapping>
 
  <servlet>
  <servlet-name>demo</servlet-name>
  <servlet-class>java8s.ProfileServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>demo</servlet-name>
  <url-pattern>/ProfileServlet</url-pattern>
  </servlet-mapping>
 
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>


Output
servlet-login-2

Now if u click on profile menu, then u will get


servlet-login-3

Without login if u will click on profile menu, then u will get


servlet-login-4

When u will click on logout menu, then u will get


servlet-login-5
❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved