Home Answers Viewqa JSP-Servlet login example

 
 


mahender
login example
1 Answer(s)      a year and 4 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;
}
   }
View Answers

January 18, 2012 at 11:57 AM


Specify the error.









Related Pages:
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
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
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
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
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
@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
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 application - Struts
login application  Can anyone give me complete code of user login application using struts and database?  Hello, Here is good example of Login and User Registration Application using Struts Hibernate and Spring
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
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
login
login  login page display an error showing failure to login even when the correct information is entered
html login page with ajax
html login page with ajax  hi all... i want to create a login page...... thanks in advance   Here is a jsp example that accepts username...=="yes"){ alert("Welcome User"); }else{ alert("Invalid Login! Please try again
Regarding Login application program
example from: http://roseindia.net/struts/struts-login-form.shtml Thanks...Regarding Login application program  Hi this is shiva. iam writing a small login form using struts1.3.10 version. once iam submit the login button
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 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 PROBLEM - JDBC
LOGIN PROBLEM   sir iam harikrishna studying B.Tech Fourth year sir i'm designing one website in that one i have to give facility as login id... a connection and store the values in the database 2. Validating the login
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
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
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
Login form in jscript
Login form in jscript  For example the html script is <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http
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 Form
to accessing data. E-mail's login are very  good example... Login Form       Login form in struts: Whenever you goes to access data from
Struts 2.1.8 Login Form
to validate the login form using Struts 2 validator framework. About the example: This example will display the login form to the user. If user enters Login Name...:fielderror /> tags in JSP. Download the code of Login example. In this section we
RichFaces: Login & Registration Application:
& Registration example application provides: User Login User Registration... RichFaces: Login & Registration Application      Login and Registration is one of the most required
login page using jsp servlrt with mysql database?
login page using jsp servlrt with mysql database?  Description: example:total users are 3.each use have username and password save in mysql database table login. After successfully login user1 see only index page,if user2 login
PHP MySQL Login Form
Login Form using PHP and MySQL: In any website generally it is mandatory to have a login form to keep your data secure. In this regard we need to have... will study how to create a login form, connect with the database server and login
Creating Login Page In JSF using NetBeans
and click. 3. Enter JSP File Name (login, for this example) when New JSP File... as below:   Write your code for login. In this example, login.jsp is created... Creating Login Page In JSF using NetBeans  
PHP SQL Login Script
PHP SQL Login Script       This Application illustrates how to create a php validation script for login application. In this example, we have created a login application
how to login form through spring dao module
how to login form through spring dao module  here i want to chek user details in database through form by using spring dao module.please give me some reference example to me
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 Authentication in JSP
;    Example program using Select Box to show retrieved data from database This example will describe you the use of Select Box in a JSP.... In this example we are using JavaScript to show the content selected
Ajax Login Example
from database. Example of Ajax login Program : <html> <head>... Ajax Login Program      ... to authenticate the user.  When a user input username and password and press Login
Role based login ..Need the concepts.. - JavaMail
Role based login ..Need the concepts..  Role based login how can i do... dynamically... Example... Admin can delete option available...". Login Name Admin Employee Login Name
PHP SQL Login Script
PHP SQL Login Script This Application illustrates how to create a php validation script for login application. In this example we create a login application... is invalidate but if an authorized user login then a success message will be display
jsp/servlet login program - JSP-Servlet
jsp/servlet login program  hello sir, well i have a problem... a login program where a new user will be registered and his/her details stored... the example it is working properly. If it arises problem on your system just try
How to put the image in footer in login form ?
How to put the image in footer in login form ?  Hi, how to put the image in footer in login form ?   Here is an example that shows image on footer along with Login Form in Java Swing. import javax.swing.*; import
About Struts 2.2.1 Login application
.style1 { margin-left: 80px; } About Struts2.2.1 login application In the most common login application there is  login form which is takes... / browser. To create an login application At first create a login page in JSP
admin login control page - JSP-Servlet
admin login control page  how to encrypted password by using jsp... adminPage.jsp?  JSP Example code for encrypted passwordJSP Encrypted Password Example CodeadminPage.jsp<html> <title>Admin login
login page
login page  code for login page
How to use filter to redirect to login page after session timeout.
How to use filter to redirect to login page after session timeout.  ... then automatically it should redirect to login page from where user has to login again to access the resources. I know filters are used for this but do

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.