Total members 11312 |It is currently Sat Apr 19, 2014 8:36 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

All times are UTC [ DST ]



Go to page 1, 2  Next
  login using jsp
Sat Jan 24, 2009 1:12 am 

login using jsp , the login is based on session object, here there are three files: login(Sign-in) form, check sign-in ,and logout , You can change the check of login to your program needs .here the files i made .


1.The Main Page . contain login form


Contains the html login form written within the jsp tags. A check condition is done on the session attribute "username" to check you if you are already logged in. If so a sign-out link will be presented otherwise, the html login form will be printed. The login form contains only two input fields for the username and password. The default form method is post and its action is sent to "checkLogin.jsp" page.
html code
<HTML>
    <HEAD>
        <TITLE>Login using jsp</TITLE>
    </HEAD>

    <BODY>
        <H1>LOGIN FORM</H1>
        <%
        String myname =  (String)session.getAttribute("username");
       
        if(myname!=null)
            {
             out.println("Welcome  "+myname+"  , <a href=\"logout.jsp\" >Logout</a>");
            }
        else 
            {
            %>
            <form action="checkLogin.jsp">
                <table>
                    <tr>
                        <td> Username  : </td><td> <input name="username" size=15 type="text" /> </td> 
                    </tr>
                    <tr>
                        <td> Password  : </td><td> <input name="password" size=15 type="text" /> </td> 
                    </tr>
                </table>
                <input type="submit" value="login" />
            </form>
            <% 
            }
        
            
            %>
        
    </BODY>
</HTML> 



2. Check Login file used to check the username and password :


We get the sent parameter from the main page to the checkLogin.jsp. The method getParameter of reference request is used to get parameter value using its name as reference. If the username or the password is not sent a static message will be printed "Invalid paramters" .

java code
<%
            String username = request.getParameter("username");
            String password = request.getParameter("password");
           out.println("Checking login<br>");
            if (username == null || password == null) {

                out.print("Invalid paramters ");
            }

            // Here you put the check on the username and password
            if (username.toLowerCase().trim().equals("admin") && password.toLowerCase().trim().equals("admin")) {
                out.println("Welcome " + username + " <a href=\"index.jsp\">Back to main</a>");
                session.setAttribute("username", username);
            }
           else 
               {
                out.println("Invalid username and password");
           }




%> 

If the parameters are found a new stage begins where some validations of authentication are applied. Here we are using hard coded value "admin" and "admin", but you can change this code easily to start reading from database and comparing a specific users data in database. The trim() function is used here to remove the white-spaces that may be found at the starts and ends of strings. We the validation is successfully passed. A new attributes are added to the current session with the value of the username.

3. Logout file :


If you are logged in and you want to go out. You can use the logout page where the session value for your username attribute is removed.
java code
<%

     String username=(String)session.getAttribute("username");
    if(username!=null)
        {
           out.println(username+" loged out, <a href=\"index.jsp\">Back</a>");
            session.removeAttribute("username");
            
        }
     else 
         {
         out.println("You are already not login <a href=\"index.jsp\">Back</a>");
     }



%>  

At the end a back link is printed to the user, it is better to use redirect after some time using javascript or meta tags such as this :
Code:
<META http-equiv="refresh" content="3;URL=index.jsp">

The meta tag above redirects you to the index.jsp page automatically after 3 seconds.
You can also use JSP redirect function as follows :
Code:
<%
response.sendRedirect("index.jsp");%>


_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 time
  Re: login using jsp
Wed Mar 25, 2009 5:33 am 
I love this~~~~~~~
Thank you.


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
  Re: login using jsp
Tue Jan 25, 2011 2:34 pm 
Hi,
In your program,there is a bug.That is if you click the logout link,it'll take you to the index page but after that if you click the back button,it'll again take you the welcome page .Please check it....


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
  Re: login using jsp
Thu Jun 30, 2011 1:29 pm 
love it
thanks


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
  Re: login using jsp
Tue Jul 26, 2011 7:21 am 
Really good one love it.................... :gOOd: :gOOd: :yahoo: :sohappy:


Author:
  Re: login using jsp
Fri Oct 07, 2011 7:01 am 
hi i want to check it from the database please any one help me


Author:
  Re: login using jsp
Sat Nov 05, 2011 9:50 am 
krrish wrote:
Hi,
In your program,there is a bug.That is if you click the logout link,it'll take you to the index page but after that if you click the back button,it'll again take you the welcome page .Please check it....

hi every one this is the best solution to the logout problem which logs out user when logout link is clicked,it also solves the problem of url, copying and pasting the url does not open the page after logout

this example consists of a sample.html page which takes name and password as input.
Please enter name as kiran and password as kiran
it then forwards to a login.jsp and if name and password are correct it sends it to a welcome.jsp page which has some links like x.jsp,y.jsp,z.jsp and p.jsp and a logout link.
the codes are shown below and are as follows:

**sample.html**
Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">
    <title>SOLVING LOGOUT PROBLEM EFFICIENTLY USING JSP</title>
    </head>
   
    <body bgcolor="#99FF66">
    <center><h1>login</h1></center>
    <center>
    <form nme="login" method="post" action="login.jsp">
    <table>
     <tr>
      <td>name:</td>
      <td><input type="text" name="name" align="right" /></td>
     </tr>
     <tr>
      <td>password:</td>
      <td><input type="password" name="password" align="right" /></td>
      </tr>
      <tr>
       <td>
       </td>
       <td><center><input type="submit" name="submit" /></center></td>
      </tr>
     </table>
     </form>
    </center>
    </body>
    </html>
   


**login.jsp**
Code:
   
    <%
    response.setHeader("Cache-Control","no-store");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0);
    %>
   
    Read more: http://wiki.answers.com/Q/How_do_you_clear_the_browser_cache_in_jsp#ixzz1c6g2F9jR
    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
   
    <body>
   
    <% String fname=null,fpassword=null;
    try
    {
    session.setAttribute("name",null);
    session.setAttribute("password",null);
    fname=request.getParameter("name");
    fpassword=request.getParameter("password");
    if(fname.equals("kiran")&&fpassword.equals("kiran"))
    {
     session.setAttribute("name",fname);
     session.setAttribute("password",fpassword);
    %>
    <jsp:forward page="welcome.jsp"/>
    <%
    }
    else
    {
     %>
     <jsp:forward page="sample.html"/>
     <%
    }
    }
    catch(Exception e)
    {
     System.out.println("errror"+e);
    }
    %>
    </body>
    </html>
   
   
    **welcome.jsp**
   
   
    <%
    response.setHeader("Cache-Control","no-store");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0); 
    %>
    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
   
   
    <%
    if((session.getAttribute("name"))!=null&&(session.getAttribute("password")!=null))
    {
    %>
      <body bgcolor="#99FF66"><center>
      <p>welcome<%=session.getAttribute("name")%> </p><h1 align="right"><a href="logout.jsp">logout</a></h1>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p><table width="100"><tr><td align="center"><a href="x.jsp">x</a></td><td align="center"><a href="y.jsp">y</a></td></tr>
      <tr><td align="center"><a href="z.jsp">z</a></td><td align="center"><a href="p.jsp">p</a></table>
    </center>
    </body>
    <%
    }
    else
    {
    %>
    <jsp:forward page="sample.html"/>
    <%
     }
    %>
   
    </html>

then the links x.jsp,y.jsp,z.jsp,p.jsp all look same.

**x.jsp**


<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0); 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<%
if((session.getAttribute("name"))!=null&&(session.getAttribute("password")!=null))
{
%>
<body bgcolor="#99FF66"><h1 align="center">x</h1>
<h1 align="right"><a href="logout.jsp">logout</a></h1>
<center>
<table width="100"><tr><td align="center"><a href="#">x</a></td><td align="center"><a href="y.jsp">y</a></td></tr>
  <tr><td align="center"><a href="z.jsp">z</a></td><td align="center"><a href="p.jsp">p</a></table>
</center>
</body>
<%
}
else
{
%>
<jsp:forward page="sample.html"/>
<%
}
%>
</html>

**y.jsp**

<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0); 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<%
if((session.getAttribute("name"))!=null&&(session.getAttribute("password")!=null))
{
%>
<body bgcolor="#99FF66"><h1 align="center">y</h1>
<h1 align="right"><a href="logout.jsp">logout</a></h1>
<center>
<table width="100"><tr><td align="center"><a href="x.jsp">x</a></td><td align="center"><a href="#">y</a></td></tr>
  <tr><td align="center"><a href="z.jsp">z</a></td><td align="center"><a href="p.jsp">p</a></table>
</center>
</body>
<%
}
else
{
%>
<jsp:forward page="sample.html"/>
<%
}
%>
</html>

**z.jsp**

<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0); 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<%
if((session.getAttribute("name"))!=null&&(session.getAttribute("password")!=null))
{
%>
<body bgcolor="#99FF66"><h1 align="center">z</h1>
<h1 align="right"><a href="logout.jsp">logout</a></h1>
<center>
<table width="100"><tr><td align="center"><a href="x.jsp">x</a></td><td align="center"><a href="y.jsp">y</a></td></tr>
  <tr><td align="center"><a href="#">z</a></td><td align="center"><a href="p.jsp">p</a></table>
</center>
</body>
<%
}
else
{
%>
<jsp:forward page="sample.html"/>
<%
}
%>
</html>

**p.jsp**

<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0); 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<%
if((session.getAttribute("name"))!=null&&(session.getAttribute("password")!=null))
{
%>
<body bgcolor="#99FF66"><h1 align="center">p</h1>
<h1 align="right"><a href="logout.jsp">logout</a></h1>
<center>
<table width="100"><tr><td align="center"><a href="x.jsp">x</a></td><td align="center"><a href="y.jsp">y</a></td></tr>
  <tr><td align="center"><a href="z.jsp">z</a></td><td align="center"><a href="#">p</a></table>
</center>
</body>
<%
}
else
{
%>
<jsp:forward page="sample.html"/>
<%
}
%>
</html>

and finally on logout the code is as follows :

**logout.jsp**

<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0); 
%>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<%
session.removeAttribute("name");
session.removeAttribute("password");
session.invalidate();
%>
<jsp:forward page="sample.html"/>
</body>
</html>

Thats it hope it solves the logout problem

for any queries contact me at jaikiran.anits@gmail.com or jaikiran_cool@yahoo.com


Author:
  Re: login using jsp
Mon Nov 07, 2011 7:21 am 
Guest wrote:
Thats it hope it solves the logout problem

for any queries contact me at jaikiran.anits@gmail.com or jaikiran_cool@yahoo.com


There is still a problem, it did not work for me.


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
  Re: login using jsp
Mon Jan 02, 2012 2:16 pm 
mahes wrote:
hi i want to check it from the database please any one help me

make class for connection then
write the name of the next page in form tag
example <form action="pagename.jsp">
in the second page we import the class file which make the connection and retrieving data from the database and check the username/password
example <%@page import="Exam.Validation" %>
<jsp:useBean class="Exam.Validation" id="vl" scope="session"></jsp:useBean>
then we take the user name and password form the varriable name of textbox form fist page and pass to the validation class
example <% String s=request.getParameter("Ut"); %>
<% String p=request.getParameter("Pt"); %>
and wirte the syntax if u/p wrong to forward the page
example <% if (!vl.validation(s, p ))response.sendRedirect("index.jsp?msg= invalid user Name/password"); %>


Author:
  Re: login using jsp
Sun Jan 15, 2012 7:41 am 
thanks..it worked for me. Very appreciated of all your help. Great forum.


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

  Related Posts  to : login using jsp
 develop a login page- login servlet - ServletContext     -  
 ASP.NET 2.0 Login Controls     -  
 Ajax Login     -  
 login using Ajax     -  
 php login with session     -  
 Login using jsp,Servlets and Database following MVC     -  
 How to Show a message before the login ?     -  
 Send login information     -  
 Login using session with php and mysql     -  
 How to replace login in mile200     -  

All times are UTC [ DST ]



Topic Tags

Servlets/JSP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team