Saturday, May 22, 2010

Struts 2 - Login Application


Struts2 Application-Login page Application
Aim:
          To develop login application using struts2 framework.

Software Requirements:
           A). NetBean 6.8
                 www.netbeans.org/

                B). Download plugin for struts2  
                http://plugins.netbeans.org/PluginPortal/

Requirement:
              1). HelloWorld.jsp
             2). Success.jsp
             3). Failure.jsp
             4). Made some changes in example.xml
             5). Include example.xml into struts.xml
             6). Create Helloworld.java


Procedure:

        Step_1: Open NetBean 6.8



Step_2: Goto Fileà New Project. The New Project window will open. Where you select  under categories Java Web and under Projects web Application . And Click Next. The New Web Application window will open. Where you give project name ( I have given project name as LoginproA). Click Next and you select Apache server and click Next and the New web Application window will open. Where you select checkbox for struts 2. Click Finish. Now you have successfully created the struts project.

            Step_3: Structure of the project after creating 






Step_4: Creating jsp file

Web pages
|
|
example
|
|
*.jsp file



Step_4.1:Create HelloWorld.jsp

    Right click on example under web pages.
    Newàotherà under categories web – under File types jsp
    Give jsp file name as HelloWorld.jsp
    Include following coding into HelloWorld.jsp

< %@ page contentType="text/html; charset=UTF-8" % >

< %@ taglib prefix="s" uri="/struts-tags" % >

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< s:form action="login" >
< s:textfield name="username" label="UserName"/ > < br >
< s:password name="password" label="PassWord"/ > < br >
< s:submit value="Login" label="Login"/ >
< /s:form >
< /body >
< /html >

Step_4.2: Create success.jsp

< %@ taglib prefix="s" uri="/struts-tags" % >

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< h2 > Success < /h2 >
< /body >
< /html >


Step_4.3: Create failure.jsp



< %@ taglib prefix="s" uri="/struts-tags" % >

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< h2 > Failure < /h2 >
< /body >
< /html >


Step_5: Creating action file

                                Source Package
                                                |
                                                |
                                         example
                                                |
                                                |
                                        *.java
Step_5.1:Create HelloWorld.java under source package
Include following coding
        
package example;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport
{
    private String username;
    private String password;
     public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String execute() throws Exception
    {
        if(getUsername().equals("suresh")&&(getPassword().equals("kumar")))
        {
            return "success";
        }
        else
        {
            return "failure";
        }
    }
 }

Step_6: Configuration file

           There are two configuration file
           One is example.xml
           Another one is struts.xml

Source Package
|
|
< default-package >
|
|
example.xml
struts.xml

example.xml

include following code into example.xml


…….
…….

< struts >
< package name="example" namespace="/example" extends="struts-default" >
< action name="login" class="example.HelloWorld" >
< result > /example/HelloWorld.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< result name="failure" > /example/failure.jsp < /result >
< /action >
< /package >
< /struts >


struts.xml

include following coding into struts.xml

……
……

< struts >
< include file="example.xml"/ >
< !-- Configuration for the default package. -- >
< package name="default" extends="struts-default" >
< /package >
</struts >


Step_7:

Run HelloWorld.jsp à right click on HelloWorld.jspà run file

Conclusion:

From this post, how to create structure and how to create jsp file and class file .

How to run the struts2 application.You have any doubt give comments.


No comments:

Post a Comment