Total members 10262 | Gratitudes |It is currently Wed May 23, 2012 9:31 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 16 posts ]  Go to page 1, 2  Next
Author Code Snippet
 Code subject: login using jsp
PostPosted: Sat Jan 24, 2009 1:12 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

login using jsp , the login is based on session object ,here there are three files ,login form ,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
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 :


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");
           }




%>
 


3. Logout file :
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>");
     }



%>
  

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: Wed Mar 25, 2009 5:33 am 
Offline
Newbie
User avatar

Joined: Tue Mar 24, 2009 3:49 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
I love this~~~~~~~
Thank you.


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: Tue Jan 25, 2011 2:34 pm 
Offline
Newbie
User avatar

Joined: Tue Jan 25, 2011 2:21 pm
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
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....


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: Thu Jun 30, 2011 1:29 pm 
Offline
Newbie
User avatar

Joined: Thu Jun 30, 2011 1:23 pm
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
love it
thanks


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: Tue Jul 26, 2011 7:21 am 
Really good one love it.................... :gOOd: :gOOd: :yahoo: :sohappy:


TOP
  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: Fri Oct 07, 2011 7:01 am 
hi i want to check it from the database please any one help me


TOP
  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: 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


TOP
  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: Mon Nov 07, 2011 7:21 am 
Offline
Newbie
User avatar

Joined: Mon Nov 07, 2011 7:07 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
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.


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: 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"); %>


TOP
  
Reply with quote  
 Code subject: Re: login using jsp
PostPosted: Sun Jan 15, 2012 7:41 am 
Offline
Newbie
User avatar

Joined: Sun Jan 15, 2012 7:38 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
thanks..it worked for me. Very appreciated of all your help. Great forum.


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 16 posts ]  Go to page 1, 2  Next
Quick reply


  

 Similar topics
 php login form (sign-in form)
 login using sessionid or time
 Send login information
 Mile200: Removing Username, Password & Login from Header
 How to Show a message before the login ?
 php login with session
 code for login by jsp and ajax
 login using Ajax
 Login using session with php and mysql
 Ajax Login

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






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