Sunday 29 May 2011

Login Example using Struts 1.3

To understand the struts framework,Here a simple Login Example.
To create a Web Application using Struts we need An IDE(Integrated Development Environment) Like NetBeans IDE or MyEclipse.
Here we are using Netbeans IDE.
NetBeans IDE : A free, open-source Integrated Development Environment for software developers. All the
                         tools needed to create professional desktop, enterprise, web, and mobile applications with the Java platform.Click here to Download
Download and Install it on your computer.
Steps to create a project using Netbeans :

1. Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application and click Next.

 2. In the Name and Location panel, enter StutsLoginExample for Project Name and click Next.


3. In the Server and Settings panel, select the server to which you want to deploy your application.
 Choose Apache Tomcat 6 and click Next

 4. In the Frameworks panel, select Struts 1.3.8



do not change any of the configuration values in the lower region of this panel. These are the following: 


  • Action Servlet Name: The name of the Struts action servlet used in the application. The web.xml deployment descriptor contains an entry for the action servlet. In web.xml mapping as follows:
<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
  • Action URL Pattern: Specifies the patterns of incoming requests which are mapped to the Struts action controller. This generates a mapping entry in the deployment descriptor. By default, only the *.do pattern is mapped.
  • Application Resource: will be used in the struts-config.xml file for localizing messages. By default, this is com.myapp.struts.ApplicationResource.
  • Add Struts TLDs: A tag library descriptor is an XML document which contains additional information about the entire tag library as well as each individual tag. In general this is not necessary.
5 .Click Finish. The IDE creates the project folder in your file system. As with any web application in the IDE, the project folder contains all of your sources and the IDE's project metadata.

The project opens in the IDE. The Projects window is the main entry point to your project sources. It shows a logical view of important project contents.It looks like:

Now We can develop project.Development of project contains following steps:

Step 1- Creating View : We will create three JSP pages for our project to view.
------.
Login.jsp & Success.jsp & Failure.jsp
Login.jsp contains two fields UserName and Password. And a Button Login
Success.jsp contains simple message for successful Login.
Failure.jsp contains message for Login Failed
Creating Login.jsp as follows :
Right-click Web pages>New>JSP 


File name as Login and click Finish 


In the Source Editor, change the content of both the <title> to Login and add following tag lib :

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

Struts provides custom tag libraries which facilitate interaction with HTML forms. These can be easily applied to a JSP file using the IDE's support for code completion.
Source code for Login.jsp :
---------------------------------------------------------------------------------------------------------------
 <%--
    Document   : Login
    Created on : May 29, 2011, 5:34:41 PM
    Author     : Amit
--%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>Login</title>
    </head>
    <body>
        <html:form action="/LoginAction">
        <table border="0" align="center">
            <tbody>
                <tr>
                    <td>User Name :</td>
                    <td><html:text property="username"/></td>
                </tr>
                <tr>
                    <td>Password :</td>
                    <td><html:password property="password"/></td>
                </tr>
                <tr>
                    <td colspan="2"><html:submit value="Login"/></td>
               </tr>
            </tbody></table>
      </html:form>
</body></html>
 -------------------------------------------------------------------------------------------------------------
Creating success.jsp as follows :
Right-click Web pages>New>JSP
File Name : success (In Name and Location panel)
click Finish

source code for success.jsp 
------------
<%--
    Document   : success
    Created on : May 29, 2011, 5:52:32 PM
    Author     : Amit
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>Success page</title>
    </head>
    <body>
        <h1>Successful Login</h1>
    </body>
</html>
-------------------------------------------------------------------------------------------------------------
Creating Failure.jsp as follows :
Right-click Web pages>New>JSP
File Name : Failure (In Name and Location panel)

click Finish

source code for Failure.jsp 
---------------------------
<%--
    Document   : Failure
    Created on : May 29, 2011, 5:52:49 PM
    Author     : Amit
--%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>Failure Page</title>
    </head>
    <body>
  <h1>Login Failed.</h1>
  <h2><html:link href="LoginAgainAction.do"> Login Again</html:link></h2>
    </body>
</html>
--------------------------------------------------------------------------------------------------------------
Step 2 - Creating Model :
------
In source package > Right click com.myapp.struts>New>java class





In Name and Location Panel
Class Name : LoginModel
Click Finish

Source code for LoginModal.java :


Now We will create ActionForm :

In source package > Right click com.myapp.struts>New>Other
















No comments:

Post a Comment