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
















Sunday 22 May 2011

What is Struts ?

Framework : A framework is made up of the set of classes 
                     which allow us to use a library in a best possible way for a specific requirement. 

Struts : Struts is free open source implementation of MVC
            (Model-View-Controller) pattern & the most popular framework for developing Java based web applications. Struts is part of the Jakarta Project, sponsored by the Apache Software Foundation.
Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java. The framework includes a set of custom tag libaries and their associated Java classes, along with various utility classes.

History : Apache Struts framework as released in May 2000 
              by Craig R. McClanahan. 
In July 2001, version 1.0 of Struts framework was released.

versions :    Struts 1.0.2  Details
                    Struts 1.1     Details
                    Struts 1.2.4  Details
                    Struts 2.0.6  Details
                    Struts 2.1.6  Details
                    Struts 2.2.1  Details
Struts 2.2.3 is the "best available" version of Struts in the 2.2 series.Download

MVC MVC (Model-View-Controller) is a way to build
           applications that promotes complete separation between business logic and presentation. 
Struts encourages application architectures based on the Model 2 approach, a variation of the classic Model-View-Controller (MVC) design paradigm. 

Model : The model contains the core of the application's
             functionality. The model encapsulates the state of  
             the application.It knows nothing about the view or 
             controller.Components like business logic /business
             processes and data are the part of model.
View : The view provides the presentation of the model. 
           It is the look of the application.
           HTML, JSP are the view components.
Controller :The controller reacts to the user input. 
                   It creates and sets the model.
ActionServlet and RequestProcessor class in struts are the Part of Controller.








 


Sunday 15 May 2011

About java

Java is Object-oriented programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform.Originally called Oak by James Gosling.
Editions: 
J2SE  : Java 2 Standard Edition
Java SE was known as Java 2 Platform, Standard Edition or J2SE from version 1.2. The "2" was originally intended to emphasize the major changes introduced in version 1.2
J2EE  : Java 2 Enterprise Edition
Java Platform, Enterprise Edition or Java EE is a widely used platform for server programming in the Java programming language.
J2ME : Java 2 Micro Edition
Java 2 Micro Edition (J2ME) is version of Java aimed at machines with limited hardware resources such as PDAs, cell phones, and other consumer electronic and embedded devices.