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



Home Answers Viewqa JSP-Servlet login example
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mahender
login example
2 Answer(s)      2 years and 11 months ago
Posted in : JSP-Servlet


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

Advertisement
View Answers

January 18, 2012 at 11:57 AM


Specify the error.



November 1, 2012 at 5:08 PM


please give web.xml and servletconfig.xml pages...



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
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
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
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...; charset=UTF-8"> <title>Login Validation Example<
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
@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.... Let's start developing the Struts 2 Login Form Example Step 1... you can create Login form in Struts 2 and validate the login action
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
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
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
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 Here 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
Spring Login Example
Spring Login Example In this section , we have shown a simple Spring loging Example. In this example, we have accepted two values Username and password... Spring Login Example: Logger.java UserLogin.java dispatcher-servlet.xml
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
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
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 page
login page  code for login page
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-logout
login-logout  how to do login and logout using session in php
login page
login page  How to login as different user in php
 

 

 

DMCA.com