Here you will find a variety of videos on technology, Tutorials, News, Tip And Tricks, Reviews

Breaking

Post Top Ad

Your Ad Spot

Tuesday, June 30, 2015

Create Login Page using Servlet

Login.html









<!DOCTYPE html><!--[if lt IE 7 ]> <html lang="en" class="ie6 ielt8"> <![endif]--><!--[if IE 7 ]>    <html lang="en" class="ie7 ielt8"> <![endif]--><!--[if IE 8 ]>    <html lang="en" class="ie8"> <![endif]--><!--[if (gte IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]--><head><meta charset="utf-8"><title>Login</title><link rel="stylesheet" type="text/css" href="style.css" /></head>
<body>
<div class="container"> <section id="content"> 
<form action="/samplepro/login"> <h1>Login Form</h1>  
<div>
       <input type="text" placeholder="Email_ID" required="" id="username" name="username" /> </div>  
<div>  
<input type="password" placeholder="Password" required="" id="password" name="password"/>  
</div> 
<div>
<input type="submit" value="Log in" />  
<a href="#">Lost your password?</a>  
<a href="#">Register</a>  
</div>
</form>
<!-- form --> </section><!-- content --></div><!-- container -->
</body>
</html>
<style type="text/css">/* Reset CSS */html, body, div, span, applet, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,a, abbr, acronym, address, big, cite, code,del, dfn, em, font, img, ins, kbd, q, s, samp,small, strike, strong, sub, sup, tt, var,b, u, i, center,dl, dt, dd, ol, ul, li,fieldset, form, label, legend,table, caption, tbody, tfoot, thead, tr, th, td { margin: 0; padding: 0; border: 0; outline: 0; font-size: 100%; vertical-align: baseline; background: transparent;}body { background: #DCDDDF url(/samplepro/images/image.png); color: #000; font: 14px Arial; margin: 0 auto; padding: 0; position: relative;}h1{ font-size:28px;}h2{ font-size:26px;}h3{ font-size:18px;}h4{ font-size:16px;}h5{ font-size:14px;}h6{ font-size:12px;}h1,h2,h3,h4,h5,h6{ color:#563D64;}small{ font-size:10px;}b, strong{ font-weight:bold;}a{ text-decoration: none; }a:hover{ text-decoration: underline; }.left { float:left; }.right { float:right; }.alignleft { float: left; margin-right: 15px; }.alignright { float: right; margin-left: 15px; }.clearfix:after,form:after { content: "."; display: block; height: 0; clear: both; visibility: hidden;}.container { margin: 25px auto; position: relative; width: 900px; }#content { background: #f9f9f9; background: -moz-linear-gradient(top,  rgba(248,248,248,1) 0%, rgba(249,249,249,1) 100%); background: -webkit-linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); background: -o-linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); background: -ms-linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); background: linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f8f8f8', endColorstr='#f9f9f9',GradientType=0 ); -webkit-box-shadow: 0 1px 0 #fff inset; -moz-box-shadow: 0 1px 0 #fff inset; -ms-box-shadow: 0 1px 0 #fff inset; -o-box-shadow: 0 1px 0 #fff inset; box-shadow: 0 1px 0 #fff inset; border: 1px solid #c4c6ca; margin: 0 auto; padding: 25px 0 0; position: relative; text-align: center; text-shadow: 0 1px 0 #fff; width: 400px;}#content h1 { color: #7E7E7E; font: bold 25px Helvetica, Arial, sans-serif; letter-spacing: -0.05em; line-height: 20px; margin: 10px 0 30px;}#content h1:before,#content h1:after { content: ""; height: 1px; position: absolute; top: 10px; width: 27%;}#content h1:after { background: rgb(126,126,126); background: -moz-linear-gradient(left,  rgba(126,126,126,1) 0%, rgba(255,255,255,1) 100%); background: -webkit-linear-gradient(left,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%); background: -o-linear-gradient(left,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%); background: -ms-linear-gradient(left,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%); background: linear-gradient(left,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%);    right: 0;}#content h1:before { background: rgb(126,126,126); background: -moz-linear-gradient(right,  rgba(126,126,126,1) 0%, rgba(255,255,255,1) 100%); background: -webkit-linear-gradient(right,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%); background: -o-linear-gradient(right,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%); background: -ms-linear-gradient(right,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%); background: linear-gradient(right,  rgba(126,126,126,1) 0%,rgba(255,255,255,1) 100%);    left: 0;}#content:after,#content:before { background: #f9f9f9; background: -moz-linear-gradient(top,  rgba(248,248,248,1) 0%, rgba(249,249,249,1) 100%); background: -webkit-linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); background: -o-linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); background: -ms-linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); background: linear-gradient(top,  rgba(248,248,248,1) 0%,rgba(249,249,249,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f8f8f8', endColorstr='#f9f9f9',GradientType=0 ); border: 1px solid #c4c6ca; content: ""; display: block; height: 100%; left: -1px; position: absolute; width: 100%;}#content:after { -webkit-transform: rotate(2deg); -moz-transform: rotate(2deg); -ms-transform: rotate(2deg); -o-transform: rotate(2deg); transform: rotate(2deg); top: 0; z-index: -1;}#content:before { -webkit-transform: rotate(-3deg); -moz-transform: rotate(-3deg); -ms-transform: rotate(-3deg); -o-transform: rotate(-3deg); transform: rotate(-3deg); top: 0; z-index: -2;}#content form { margin: 0 20px; position: relative }#content form input[type="text"],#content form input[type="password"] { -webkit-border-radius: 3px; -moz-border-radius: 3px; -ms-border-radius: 3px; -o-border-radius: 3px; border-radius: 3px; -webkit-box-shadow: 0 1px 0 #fff, 0 -2px 5px rgba(0,0,0,0.08) inset; -moz-box-shadow: 0 1px 0 #fff, 0 -2px 5px rgba(0,0,0,0.08) inset; -ms-box-shadow: 0 1px 0 #fff, 0 -2px 5px rgba(0,0,0,0.08) inset; -o-box-shadow: 0 1px 0 #fff, 0 -2px 5px rgba(0,0,0,0.08) inset; box-shadow: 0 1px 0 #fff, 0 -2px 5px rgba(0,0,0,0.08) inset; -webkit-transition: all 0.5s ease; -moz-transition: all 0.5s ease; -ms-transition: all 0.5s ease; -o-transition: all 0.5s ease; transition: all 0.5s ease; background: #eae7e7 url(/samplepro/images/loginimage.png) no-repeat; border: 1px solid #c8c8c8; color: #777; font: 13px Helvetica, Arial, sans-serif; margin: 0 0 10px; padding: 15px 10px 15px 40px; width: 80%;}#content form input[type="text"]:focus,#content form input[type="password"]:focus { -webkit-box-shadow: 0 0 2px #ed1c24 inset; -moz-box-shadow: 0 0 2px #ed1c24 inset; -ms-box-shadow: 0 0 2px #ed1c24 inset; -o-box-shadow: 0 0 2px #ed1c24 inset; box-shadow: 0 0 2px #ed1c24 inset; background-color: #fff; border: 1px solid #ed1c24; outline: none;}#username { background-position: 10px 10px !important }#password { background-position: 10px -53px !important }#content form input[type="submit"] { background: rgb(254,231,154); background: -moz-linear-gradient(top,  rgba(254,231,154,1) 0%, rgba(254,193,81,1) 100%); background: -webkit-linear-gradient(top,  rgba(254,231,154,1) 0%,rgba(254,193,81,1) 100%); background: -o-linear-gradient(top,  rgba(254,231,154,1) 0%,rgba(254,193,81,1) 100%); background: -ms-linear-gradient(top,  rgba(254,231,154,1) 0%,rgba(254,193,81,1) 100%); background: linear-gradient(top,  rgba(254,231,154,1) 0%,rgba(254,193,81,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fee79a', endColorstr='#fec151',GradientType=0 ); -webkit-border-radius: 30px; -moz-border-radius: 30px; -ms-border-radius: 30px; -o-border-radius: 30px; border-radius: 30px; -webkit-box-shadow: 0 1px 0 rgba(255,255,255,0.8) inset; -moz-box-shadow: 0 1px 0 rgba(255,255,255,0.8) inset; -ms-box-shadow: 0 1px 0 rgba(255,255,255,0.8) inset; -o-box-shadow: 0 1px 0 rgba(255,255,255,0.8) inset; box-shadow: 0 1px 0 rgba(255,255,255,0.8) inset; border: 1px solid #D69E31; color: #85592e; cursor: pointer; float: left; font: bold 15px Helvetica, Arial, sans-serif; height: 35px; margin: 20px 0 35px 15px; position: relative; text-shadow: 0 1px 0 rgba(255,255,255,0.5); width: 120px;}#content form input[type="submit"]:hover { background: rgb(254,193,81); background: -moz-linear-gradient(top,  rgba(254,193,81,1) 0%, rgba(254,231,154,1) 100%); background: -webkit-linear-gradient(top,  rgba(254,193,81,1) 0%,rgba(254,231,154,1) 100%); background: -o-linear-gradient(top,  rgba(254,193,81,1) 0%,rgba(254,231,154,1) 100%); background: -ms-linear-gradient(top,  rgba(254,193,81,1) 0%,rgba(254,231,154,1) 100%); background: linear-gradient(top,  rgba(254,193,81,1) 0%,rgba(254,231,154,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fec151', endColorstr='#fee79a',GradientType=0 );}#content form div a { color: #004a80;    float: right;    font-size: 12px;    margin: 30px 15px 0 0;    text-decoration: underline;}.button { background: rgb(247,249,250); background: -moz-linear-gradient(top,  rgba(247,249,250,1) 0%, rgba(240,240,240,1) 100%); background: -webkit-linear-gradient(top,  rgba(247,249,250,1) 0%,rgba(240,240,240,1) 100%); background: -o-linear-gradient(top,  rgba(247,249,250,1) 0%,rgba(240,240,240,1) 100%); background: -ms-linear-gradient(top,  rgba(247,249,250,1) 0%,rgba(240,240,240,1) 100%); background: linear-gradient(top,  rgba(247,249,250,1) 0%,rgba(240,240,240,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f7f9fa', endColorstr='#f0f0f0',GradientType=0 ); -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.1) inset; -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.1) inset; -ms-box-shadow: 0 1px 2px rgba(0,0,0,0.1) inset; -o-box-shadow: 0 1px 2px rgba(0,0,0,0.1) inset; box-shadow: 0 1px 2px rgba(0,0,0,0.1) inset; -webkit-border-radius: 0 0 5px 5px; -moz-border-radius: 0 0 5px 5px; -o-border-radius: 0 0 5px 5px; -ms-border-radius: 0 0 5px 5px; border-radius: 0 0 5px 5px; border-top: 1px solid #CFD5D9; padding: 15px 0;}.button a { background: url(/samplepro/images/loginimage.png) 0 -112px no-repeat; color: #7E7E7E; font-size: 17px; padding: 2px 0 2px 40px; text-decoration: none; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -ms-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease;}.button a:hover { background-position: 0 -135px; color: #00aeef;}</style>





Servlet login: Login.java



package com.pack;
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 java.sql.Statement;

import javax.servlet.ServletConfig;
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;


/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class login extends HttpServlet
{
private static final long serialVersionUID = 1L;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
int flag=0;
public void init(ServletConfig config)throws ServletException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gateschema","root","root");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void destroy()
{
try
{
con.close();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter pw=response.getWriter();
String uid=request.getParameter("username");
String Pwd=request.getParameter("password");
HttpSession session=request.getSession();

if(con!=null)
{
try
{
Statement stmt=con.createStatement();
rs=stmt.executeQuery("Select * from user;");
while(rs.next())
{
if(uid.equals(rs.getString(4)) && Pwd.equals(rs.getString(6)))
{
String tempFname=rs.getString(2);
String tempLname=rs.getString(3);
session.setAttribute("fname",tempFname);
session.setAttribute("lname",tempLname);
flag=0;
break;
}
else
{
flag=1;

}
}
if(flag==1)
{
response.sendRedirect("/samplepro/login.html");
}
else
{
pw.print("welcome jaya");
response.sendRedirect("/samplepro/User");
}
}
catch (SQLException e)
{

e.printStackTrace();
}
}
}

}





Servlet: User.java


package com.pack;

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

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;

/**
 * Servlet implementation class User
 */
@WebServlet("/User")
public class User extends HttpServlet {
private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
response.setContentType("text/html");
       PrintWriter out = response.getWriter();
       HttpSession session=request.getSession(false);
       String n=(String)session.getAttribute("fname");
       String l=(String)session.getAttribute("lname");
       out.print("Hello "+n + " " +l);
       out.close();

               }
catch(Exception e){System.out.println(e);}  
} // TODO Auto-generated method stub

}



Database: User



User_ID First_Name Lasr_Name Email_ID User_Mobile Password
1 annie jambhulkar annie.jambhulkar@gmail.com 9999999999 annie123
2 Anaya gadge Anaya.gadge@gmail.com 8888888888 anaya123

When we run above application, we get response like below images 


Screenshots:



Post Top Ad

Your Ad Spot

Pages