login example

login example

1.Loginpage.jsp

<%@ page language="java" contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"%>

<!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=windows-1256">
<title>Login Page</title>
</head>

<body>
<form action="LoginServlet">Please enter your username <input
    type="text" name="un" /><br>

Please enter your password <input type="text" name="pw" /> <input
    type="submit" value="submit"></form>
</body>
</html>


2.invalidLogin.jsp

<%@ page language="java" 
      contentType="text/html; charset=windows-1256"
      pageEncoding="windows-1256"
   %>

   <!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=windows-1256">
         <title>Invalid Login</title>
      </head>

      <body>
         <center>
            Sorry, you are not a registered user! Please sign up first
         </center>
      </body>

   </html>

3.userlogged.jsp


<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256"
         import="ExamplePackage.UserBean"
   %>

   <!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=windows-1256">
         <title>   User Logged Successfully   </title>
      </head>

      <body>

         <center>
            <% UserBean currentUser = (UserBean) (session.getAttribute("currentSessionUser"));%>

            Welcome <%= currentUser.getFirstName() + " " + currentUser.getLastName() %>
         </center>

      </body>

   </html>

4.LoginServlet.java

package ExamplePackage;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) 
                       throws ServletException, java.io.IOException {

try
{       

     UserBean user = new UserBean();
     user.setUserName(request.getParameter("un"));
     user.setPassword(request.getParameter("pw"));

     user = UserDAO.login(user);

     if (user.isValid())
     {

          HttpSession session = request.getSession(true);       
          session.setAttribute("currentSessionUser",user); 
          response.sendRedirect("userLogged.jsp"); //logged-in page             
     }

     else 
          response.sendRedirect("invalidLogin.jsp"); //error page 
} 


catch (Throwable theException)      
{
     System.out.println(theException); 
}
       }
    }



5.userbean.java

package ExamplePackage;

public class UserBean {

      private String username;
      private String password;
      private String firstName;
      private String lastName;
      public boolean valid;


      public String getFirstName() {
         return firstName;
    }

      public void setFirstName(String newFirstName) {
         firstName = newFirstName;
    }


      public String getLastName() {
         return lastName;
            }

      public void setLastName(String newLastName) {
         lastName = newLastName;
            }


      public String getPassword() {
         return password;
    }

      public void setPassword(String newPassword) {
         password = newPassword;
    }


      public String getUsername() {
         return username;
            }

      public void setUserName(String newUsername) {
         username = newUsername;
            }


      public boolean isValid() {
         return valid;
    }

      public void setValid(boolean newValid) {
         valid = newValid;
    }   
}


6.userdao.java

package ExamplePackage;

import java.text.*;
   import java.util.*;
   import java.sql.*;

   public class UserDAO     
   {
      static Connection currentCon = null;
      static ResultSet rs = null;  



      public static UserBean login(UserBean bean) {

         //preparing some objects for connection 
         Statement stmt = null;    

         String username = bean.getUsername();    
         String password = bean.getPassword();   

         String searchQuery =
               "select * from users where username='"
                        + username
                        + "' AND password='"
                        + password
                        + "'";

      // "System.out.println" prints in the console; Normally used to trace the process
      System.out.println("Your user name is " + username);          
      System.out.println("Your password is " + password);
      System.out.println("Query: "+searchQuery);

      try 
      {
         //connect to DB 
         currentCon = ConnectionManager.getConnection();
         stmt=currentCon.createStatement();
         rs = stmt.executeQuery(searchQuery);           
         boolean more = rs.next();

         // if user does not exist set the isValid variable to false
         if (!more) 
         {
            System.out.println("Sorry, you are not a registered user! Please sign up first");
            bean.setValid(false);
         } 

         //if user exists set the isValid variable to true
         else if (more) 
         {
            String firstName = rs.getString("firstname");
            String lastName = rs.getString("lastname");

            System.out.println("Welcome " + firstName);
            bean.setFirstName(firstName);
            bean.setLastName(lastName);
            bean.setValid(true);
         }
      } 

      catch (Exception ex) 
      {
         System.out.println("Log In failed: An Exception has occurred! " + ex);
      } 

      //some exception handling
      finally 
      {
         if (rs != null)    {
            try {
               rs.close();
            } catch (Exception e) {}
               rs = null;
            }

         if (stmt != null) {
            try {
               stmt.close();
            } catch (Exception e) {}
               stmt = null;
            }

         if (currentCon != null) {
            try {
               currentCon.close();
            } catch (Exception e) {
            }

            currentCon = null;
         }
      }

return bean;

      } 
   }


7.connectionmanager.java


package ExamplePackage;

import java.sql.*;
   import java.util.*;


   public class ConnectionManager {

      static Connection con;
      static String url;

      public static Connection getConnection()
      {

         try
         {

            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println(" Defing the URL");

             String url= "jdbc:oracle:thin:@172.24.137.30:1521:ORA10G";
            // assuming "DataSource" is your DataSource name

           // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            try
            {   


                System.out.println(" Defing the username");
                String username= "e469573"; 
                System.out.println(" Defing the password");
                String password= "nkDquOhwj";
                System.out.println(" Defing the connection");
                con = DriverManager.getConnection(url,username,password); 
                System.out.println(" connection done" +con);





            }

            catch (SQLException ex)
            {
               ex.printStackTrace();
            }
         }

         catch(ClassNotFoundException e)
         {
            System.out.println(e);
         }

      return con;
}
   }
View Answers

January 18, 2012 at 11:57 AM

Specify the error.









Related Tutorials/Questions & Answers:
login example
login example  1.Loginpage.jsp <%@ page language="java...-Type" content="text/html; charset=windows-1256"> <title>Login...; public static UserBean login(UserBean bean) { //preparing
login example
login example  1.Loginpage.jsp <%@ page language="java...-Type" content="text/html; charset=windows-1256"> <title>Login...; public static UserBean login(UserBean bean) { //preparing
Advertisements
ajax login example
ajax login example  hi deepak, i am trying to execute ajax login example in eclipse but it show the form but when I enter admin admin it shows invalid credentials how to resolve
Spring MVC Login Example
Spring MVC Login example       Spring 2.5 MVC User Login Example This section explains you how you can make Login example using Spring MVC module. In this login example we are not connecting to any
login page php mysql example
login page php mysql example  How to create a login page for user in PHP Mysql? Please provide me a complete tutorial. Thanks in Advance
JSP Login Logout Example
JSP Login Logout Example In this section we will discuss how to create a simple login and logout example. This section will describe you all the steps for creating a simple login and logout example. To create a simple login logout
struts2.2.1 Login validation example.
struts2.2.1 Login validation example. In this example, We will discuss about the Login validation using struts2.2.1. Directory structure of example...;title>Login Validation Example</title> <s:head
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example  Spring 3 MVC Login Form Example index.jsp file have code.... Simple Form request will be handle by SimpleFormController @Controller public class @Controller public class
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example  Spring 3 MVC Login Form Example index.jsp file have code.... Simple Form request will be handle by SimpleFormController @Controller public class @Controller public class
@WebServlet Login and Logout Example
@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
Spring 3 MVC Login Form Example with Database MySql
Spring 3 MVC Login Form Example with Database MySql  Sir i have checked your project of Spring 3 MVC Login Form Example But Sir Not able to do It with database Mysql. Can you Provide code for login with database. Thanks
Struts 2 Login Form Example
Struts 2 Login Form Example tutorial - Learn how to develop Login form... the Struts 2 Login Form Example Step 1: Create a new dynamic project. Step 2: Add... you can create Login form in Struts 2 and validate the login action
Swing login form example
Swing login form example In this example we have described swing login form. We have created one text field "textField" and one jPasswordField and we have set text for username and password. We create one submit button and perform
Spring Login Example
Spring Login Example In this section , we have shown a simple Spring loging... Spring Login Example: Logger.java UserLogin.java dispatcher-servlet.xml... Example Spring 4 MVC Login form Example with source code
login
login  How to create login page in jsp
login
login  How to create login page in jsp
login
login  login page display an error showing failure to login even when the correct information is entered
Simplest Login and Logout example in JSP
Simplest Login and Logout example in JSP   This JSP example shows you how to login and logout the session between JSP... application takes an user login from that having user name and password
login
login  how to login admin and user with the same fields(name & password) in single login page while the table for admin and user is seprate in database(mysql) please provide me solution
login
login  i want to now how i can write code for form login incolude user and password in Jcreator 4.50   Hello Friend, Visit HereADS_TO_REPLACE_1 Thanks
login
java.awt.event.*; class Login { JButton SUBMIT; JLabel label1,label2; final JTextField text1,text2; Login() { final JFrame f=new JFrame("Login Form...(); ResultSet rs=st.executeQuery("select * from login where username='"+value1
login
login  how to create login page in jsp   Here is a jsp code that creates the login page and check whether the user is valid or not. 1...;tr><td></td><td><input type="submit" value="Login">
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name
Example of login form validation in struts2.2.1framework.
Example of login form validation in struts2.2.1 framework. In this example, we.... Our login form validation example does not validate the user against... structure of Login form validation example. Description of login validation
Creating Midlet Application For Login in J2ME
Creating MIDlet Application For Login in J2ME       This example show to create the MIDlet application for user login . All MIDlet applications for the MIDP ( Mobile Information
login application
login application  how to create login application ?   Hi, Please check the following tutorials: Video tutorial - JSP Login Logout Example Login Authentication using Bean and Servlet In JSP simple code to login user
Login form
Login Form with jsp       Now for your confidence with  JSP syntax, following example of login form will really help to understand jsp page. In this  example we
login page
login page  code for login page
HTML Login Page Code
HTML Login Page Code Here is an example of html login page code. In this example, we have displayed one text field, Password, Reset button and Login button... Page? Login and Logout Example in JSP with database backend
Creating Login Page In JSF using NetBeans
and click. 3. Enter JSP File Name (login, for this example) when New JSP File... as below: ADS_TO_REPLACE_1   Write your code for login. In this example... Creating Login Page In JSF using NetBeans  
Running and Testing Struts 2 Login application
Running and Testing Struts 2 Login application       Running Struts 2 Login Example In this section we will run the example on Tomcat 6.0 server and check how it works. Running Tomcat To run the Tomcat
Login page
Login page  how to create Login page in java
login page
login page  How to login as different user in php
login-logout
login-logout  how to do login and logout using session in php
LOGIN ERROR
LOGIN ERROR  ERROR SCRIPT: LOGIN FAILED", $_SESSION['login fail']='0',}?> Ty Team
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example In this tutorials we are showing you and example to create LoginForm in Spring 3.0. In this tutorial we are using annotation based Controller and other required configuration files. In the example
Login Action Class - Struts
Login Action Class  Hi Any one can you please give me example of Struts How Login Action Class Communicate with i-batis
Login authentication
Login authentication  i want d code for login authentication from mysql database on the same pc using swings
Login authentication
Login authentication  i want d code for login authentication from mysql database on the same pc using swings
login and register - Java Beginners
login and register  pls send me the code for login and register immediately  Hi friend, Please specify the technology you want code for login and register. For example : JSP,Servlet,Struts,JSF etc
Login Page
Login Page  Can anyone tell me the steps to create a login page in myeclipse 7.0 and validating it with login database in db2 9.7 ??? Please tell me
login page
<h1>Login Example!</h1> USER NAME <input name="uname...login page  hi i'm trying to create login page using jsp. i get...="text"/><br> <br> <input name="Login" type="submit" value
LOGIN PAGE
LOGIN PAGE  A java program with "login" as title,menus added, and a text field asking for firstname, lastname, and displaying current date and time
Spring 4: Login Form using Spring MVC and Hibernate Example
Spring 4 MVC Login Example: Database driven login form using Spring MVC... learned how to make Spring 4 MVC login form example and validate against... Download the source code of the Spring 4 MVC login form example with backend
Login modification
Login modification  How can I invalidate a user once he goes back to login page without signing out,I have already tried using session but it doesn't work please help
login box
login box  class file for password and conform password and how it connected to database
login authentication in jsp and servlet
login authentication in jsp and servlet  Hi, I am beginner in web programming with JSP and Servlets. I want to make jsp database example. How to make login authentication in jsp and servlet? Thanks   Hi, I to make

Ads