Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Struts
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Login Form using Ajax
This section provides you an easy and complete implementation of login form using the Ajax (DOJO).
 
 

Login Form using Ajax

                         

This section provides you an easy and complete implementation of login form using the Ajax (DOJO). 

Lets develop a login application using Ajax. This login application first authenticates a user, it asks for a login name and password (Both the login name and password is "Admin"). It displays a welcome page, when both fields are correctly filled by the user. Otherwise it shows an error and DEBUG message (Invalid user name and password! Please try again! and DEBUG: widget ID collision on ID: ajaxLogin_0).

Create an action mapping in the struts.xml file. Here is the code to be added in the struts.xml:

<action name="showAjaxLoginForm">
       <result>/pages/ajaxlogin.jsp</result>
</action>

<action name="ajaxLogin" class="net.roseindia.Login">
       <result name="input">/pages/ajaxlogin.jsp</result>
       <result name="error">/pages/ajaxlogin.jsp</result>
       <result>/pages/ajaxloginsuccess.jsp</result>
</action>

Develop a Login Form Using Ajax : The GUI of the application consists of a login form (ajaxlogin.jsp). The "ajaxlogin.jsp" displays the login page to the user. This jsp page uses the <s:div> tag, this tag creates a content area that can load its content using Ajax tags, optionally refreshing. Here we also use the <s:submit> tag that is used to update element (s) or submit a form with the help of Ajax.

Whenever an error occurs  then the <s:actionerror> and <s:fielderror> tags execute and display an error message the login form is submitted.

Here is the code of ajaxlogin.jsp file:

<%taglib prefix="s" uri="/struts-tags"%>
<html>
  <head>
    <s:head theme="ajax" debug="true"/>
    </script>
  </head>
  <body>
    <s:div id="loginDiv" theme="ajax">
    <div style="width: 300px;border-style: solid">
      <s:form action="ajaxLogin"  validate="true">
        <tr>
          <td colspan="2">
            Login
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <s:actionerror />
            <s:fielderror />
          </td>
        </tr>  
          <s:textfield name="username" label="Login name"/>
          <s:password name="password" label="Password"/>
          <s:submit theme="ajax" targets="loginDiv" notifyTopics="/ajaxLogin"/>    
      </s:form>
    </div>
    </s:div>
  </body>
</html>

Develop an action class that handles the login request and checks for the user authentication. If the user name and password is "Admin" then it returns SUCCESS otherwise ERROR object. 

Here is the code of "Login.java" action class:

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;


/**
 <p> Validate a user login. </p>
 */
public  class Login  extends ActionSupport {


    public String execute() throws Exception {
        System.out.println("Validating login");
    if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){
            addActionError("Invalid user name or password! Please try again!");
            return ERROR;
    }else{
      return SUCCESS;
    }
  }


    // ---- Username property ----

    /**
     <p>Field to store User username.</p>
     <p/>
     */
    private String username = null;


    /**
     <p>Provide User username.</p>
     *
     @return Returns the User username.
     */
    public String getUsername() {
        return username;
    }

    /**
     <p>Store new User username</p>
     *
     @param value The username to set.
     */
    public void setUsername(String value) {
        username = value;
    }

    // ---- Username property ----

    /**
     <p>Field to store User password.</p>
     <p/>
     */
    private String password = null;


    /**
     <p>Provide User password.</p>
     *
     @return Returns the User password.
     */
    public String getPassword() {
        return password;
    }

    /**
     <p>Store new User password</p>
     *
     @param value The password to set.
     */
    public void setPassword(String value) {
        password = value;
    }

}

The "ajaxloginsuccess.jsp" page displays the Login Success message (Welcome to Admin) when the user is successfully authenticated. 

Here is the code of "ajaxloginsuccess.jsp" file:

<html>
  <head>
    <title>Login Success</title>
  </head>
  <body>
    <p align="center"><font color="#000080" size="5">Login Successful !</font></p>
    <h1> Welcome to <%=request.getParameter("username")%>  </h1>
  </body>
</html>

Output of the Ajax login application:

When this application executes you get a login page:

If you leave the "Login name" field empty and just fills the "Password" field in the login page then a message is displayed as shown below:

If you fills the "Login name" field empty and just leaves the "Password" field empty in the login page then a message is displayed as shown below:

If you enter the wrong login name and password in the login page then it displays a  message as shown below::

When you enter the correct login name and password to the login page, it shows a success message as shown :

                           

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

9 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi,
This example works in Firefox, but doesn't work in IE7.
adela

Posted by adela on Friday, 05.9.08 @ 10:43am | #58997

Hi
The above example is not working it is validating username and password after entering the username and password fields Warning message is displayed in server like this 'Invalid chunk ignored.' and a page with User Name is Required and Password is Required is displayed
How to solve this
Regards
Febin

Posted by Febin on Wednesday, 05.7.08 @ 15:06pm | #58793

Hi. When I run this example, I m getting this warning. Invalid parameters ignored...

and when I submit, I see a page with th msg Undefined..
Plz help me out...

Posted by Vishal on Monday, 03.24.08 @ 22:16pm | #54189

Hey Buddy [Mark]

simple add this in css
.errorMessage {font-weight:bold; text-align: center; color:blue; }

Posted by Vinod on Tuesday, 02.26.08 @ 12:03pm | #50108

Does any one know how I can customize the errors I get from the validation?
For example, if I would like to change the color from red to blue.

Posted by Mark on Monday, 01.21.08 @ 17:21pm | #45775

I cannot seem to get this working. For some reason in my example I cannot get the form to submit into my action class and run the 'execute()' function. Maybe my configuration is incorrect? Is there some special configuration I need to do?

Posted by Henry on Thursday, 09.20.07 @ 03:38am | #28170

Good Example I Like this example

Posted by Mahesha p on Thursday, 08.16.07 @ 11:49am | #23493

Hi, can anyone tell me where can i find the strtus tag libraray for ajax used in the above sample. Is there any more ajax tlds?

Thanks.
tfr.

Posted by tfr on Wednesday, 08.15.07 @ 11:26am | #23443

javax.servlet.ServletException: Cannot find ActionMappings or ActionFormBeans collection
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.login_jsp._jspService(login_jsp.java:73)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

Posted by javax.servlet.ServletException: Cannot find ActionMappings or ActionFormBeans collection on Saturday, 08.11.07 @ 10:47am | #23154

Latest Tutorials:
Styles in HTML
Style Tags Used in HTM
Check Box in HTML
Action Submit Html
Cascading Style Sheet(
Horizontal Rule Attrib
Horizontal Rule in HTM
Show Hyperlink in HTML
Unordered Lists
Paragraph in HTML
Password Field in HTML
Radio Buttons in HTML
Set Background Colors
Table & the Border att
Text Area in HTML
Text Field in HTML
Use of Text Field
Table Caption in HTM
J2ME Servlet Example
J2ME Cookies Example
J2ME Frame Animation2
J2ME Frame Animation
J2ME RMS Read Write
J2ME Record Data Base
J2ME Audio Record
J2ME Record Listener
J2ME Text Box Example
J2ME Timer Animation
J2ME Vector Example
J2ME Video Control Exa
J2ME Event Handling Ex
J2ME HashTable Example
J2ME Icon MIDlet Examp
J2ME Image Item Exampl
J2ME Image Example
J2ME Item State Listen
J2ME Key Codes Example
J2ME KeyEvent Example
J2ME Label Example
J2ME Random Number
J2ME Read File
J2ME RMS Sorting Examp
J2ME Timer MIDlet Exam
Custom Item in J2ME
Creational Design Patt
Design Patterns
Throwing Run time exce
Grid in Echo3
Creating Table in Echo
JPA Introduction
Java bigdecimal toBigI
Java bigdecimal shortV
Java bigdecimal shortV
Java bigdecimal signum
Java bigdecimal stripT
Java bigdecimal subtra
Java bigdecimal subtra
Java bigdecimal toBigI
Java bigdecimal toEngi
Java bigdecimal toPlai
Java bigdecimal toStri
Java bigdecimal ulp ex
Java bigdecimal unscal
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal setSca
Java bigdecimal setSca
Java bigdecimal scaleB
Java bigdecimal scale
Java bigdecimal round
Java bigdecimal remain
Java bigdecimal remain
Java bigdecimal precis
Java bigdecimal pow me
Java bigdecimal pow ex
Java bigdecimal plus m
Java bigdecimal plus e
Java bigdecimal negate
Java bigdecimal negate
Java bigdecimal multip
Java bigdecimal multip
Java BigDecimal movePo
Java bigdecimal movePo
Java bigdecimal min ex
Java bigdecimal max ex
CheckBox component in
Visibility of Componen
Loading delay componen
Simple input applicati
Opening a new window i
Hello World in Echo3 f
Use of Local Inner cla
JSP bean set property
Java Method Synchroniz
Java Method Return Val
JAVA Method Wait
JDBC vs ORM
Java BigDecimal divide
Java BigDecimal divide
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.