Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javaee6 @WebServlet Login and Logout Example

Related Tutorials


 
 

Share on Google+Share on Google+

@WebServlet Login and Logout Example

Advertisement
In this tutorial I am giving a very simple example which will demonstrate how you can create a login page, how can you authenticate them at the time of login, and finally logged out them in servlet.

@WebServlet Login and Logout Example

In this tutorial I am giving a very simple example which will demonstrate how you can create a login page, how can you authenticate them at the time of login, and finally logged out them in servlet.

Example :

login1.html

<!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>Login Page</title>
</head>
<body>
<form method="get" action="RequestForwardExample">
<p>Give the following details</p>
<table>
<tr><td>Name </td> <td><input type="text" name="name"/></td></tr>
<tr><td>Password </td> <td> <input type="password" name="psw"/></td></tr>
<tr><td></td> <td><input type="submit" value="submit"/></td></tr>
</table>
</form>
</body>
</html>

RequestForwardExample.java

package roseindia.requestDispatcherExample;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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= "RequestForwardExample",
urlPatterns= {"/RequestForwardExample"})
public class RequestForwardExample extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();

String name= request.getParameter("name");
String psw= request.getParameter("psw");

String className= "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://192.168.10.13/data";
String user= "root";
String password= "root";

Connection con;
PreparedStatement ps;
ResultSet rs;

try
{
Class.forName(className);
con= DriverManager.getConnection(url, user, password);

String sql= "SELECT name FROM login WHERE name = '"+name+"' AND password = '"+psw+"'";
ps= con.prepareStatement(sql);
rs= ps.executeQuery();
if(rs.next())
{
HttpSession session= request.getSession();
String n= rs.getString("name");
session.setAttribute("name", n);

ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/ForwardTesterServlet");
rd.forward(request, response);
}
else
{
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/login1.html");
out.println("<font color=red>invalid user name or password</font>");
rd.include(request, response);
}

con.close();
ps.close();
rs.close(); 
}
catch(ClassNotFoundException cx)
{
out.println();
}
catch(SQLException sx)
{
out.println();
}
}
}

ForwardTesterServlet.java

package roseindia.requestDispatcherExample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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("/ForwardTesterServlet")
public class ForwardTesterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
HttpSession session= request.getSession();
String name= (String)session.getAttribute("name");
out.println("Welcome "+name);
out.println("<h4>You are successfully logged in<h4>");
out.println("<html><body>");
out.println("<form method=get action= LogOutServlet>");
out.println("<input type=submit value=logout></input>");
out.println("</form></body></html>");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}

LogOutServlet.java

package roseindia.requestDispatcherExample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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("/LogOutServlet")
public class LogOutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
HttpSession session= request.getSession();
session.invalidate();
out.println("<font color=red><b>You have been successfully logged out</b></font>");
out.println("<font color=green><b>Login again</b></font>");
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/login1.html");
rd.include(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

}

Output :

When you will execute the above example you will get the output as :

When you will click on "submit" button you will get the output as :

But to logout when you will click on "logout" button you will get the output as :

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 16, 2011

Related Tutorials

Discuss: @WebServlet Login and Logout Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
P S Nayak
February 22, 2012
Computer

I have tried your example. It is showing the following exception after logout button is pressed. HTTP Status 500 - type Exception report message descriptionThe server encountered an internal error () that prevented it from fulfilling this request. exception java.lang.IllegalArgumentException: PWC1222: Path index.jsp does not start with '/' and is not empty note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.
yudha
December 12, 2012
thank'z

thank'z
musharrif
December 13, 2013
servlet

Sir u used this example using annotation can u please use web.xml
DMCA.com