Vinod sir struts 2 part 1

Post on 17-May-2015

2,760 views 1 download

Tags:

description

Basic Java Web Application using Eclipse, MVC Architecture & Struts Architecture.

transcript

Prof. Vinod Pillai.

vinodthebest@gmail.com

http://vinodthebest.wordpress.com

www.youtube.com/vinodthebest

Agenda Part – I : Understanding Basics Web concepts & Struts

Part – II : Setting Up Struts & Running basic App.

Part – III : Struts 2 UI Tags & Integrating Tiles.

Part – IV : Database + Session & Request Object.

2Prof. Vinod Pillai

About Me: Currently I am working with Chaudhari Technical Institute – MCA as Assistant

Professor since 2008.

Academic counselor in MCA & BCA Department of Indira Gandhi National OpenUniversity (IGNOU), Ahmedabad since 2009.

Visiting Faculty for MBA - Udaybhansinhji Regional Institute of CooperativeManagement (URICM), Gandhinagar since 2009.

Having 2.0 years of Industry experience + 4.0 teaching experience.

Done Master of Computer Science (Wollongong University - Australia) with 70%.

Done Bachelor of Computer Application (Gujarat University) with 87%.

Cleared SCJP (Java Standard Edition 6 Programmer Certified Professional) with95%.

Major areas: C, C++, Java, Advance Java, Android, NT-II, C Project & C++ Project.

Regular Jury Member of National Institute of Fashion Technology (NIFT-Gandhinagar) since 2009.

Guided more than 25 MCA Groups for their Final Year Project.

Guided 30 MCA Groups for C & C++ Project.

Guided one of the MCA students for her NASA project which is being appreciatedby a certificate. 3Prof. Vinod Pillai

Part – IUnderstanding

Basics Web Concepts &

Struts

4Prof. Vinod Pillai

Part – I : Understanding Struts Architecture.

Overview of Web Application.

Servlet.

JSP.

Java Beans.

MVC Architecture.

Simple MVC Base Application.

Understanding Struts 2 Architecture.

Prof. Vinod Pillai 5

Overview of Web Application Java Application = Desktop || Web || Mobile.

Web Application :

Generation data dynamically not just simple static htmlpages.

Having some forms or login page.

Example: Gmail, Youtube, Facebook and many more.

Web Application :

Simple Web Application.

Complex Web Application.

6Prof. Vinod Pillai

Java Web Application General flow of client request and response:

7Prof. Vinod Pillai

Java Web Application Key players for Java Web Application development:

Web Server.

Static HTML pages + CSS + JavaScript + Images + Videos.

Servlet.

JSP.

web.xml.

Library [If required].

8Prof. Vinod Pillai

Setting Up Web Applicaiton*Before we start we need the following tools:

JDK 1.7 (Download)

Eclipse Java EE IDE for Web Developers (Indigo)

Tomcat 7 or any other container (Glassfish, JBoss,

Websphere, Weblogic etc) (Download)

9Prof. Vinod Pillai

JDK 1.7

http://www.oracle.com/technetwork/java/javase/downloads/index.html 10Prof. Vinod Pillai

Eclipse Java EE IDE

http://eclipse.org/downloads/ 11Prof. Vinod Pillai

Apache Tomcat 7

http://tomcat.apache.org/download-70.cgi 12Prof. Vinod Pillai

Application Dev. [Eclipse] Starting Eclipse IDE

13Prof. Vinod Pillai

Getting Started

Selecting the workspace name:

14Prof. Vinod Pillai

Application Dev. [Eclipse] Eclipse Main Window

15Prof. Vinod Pillai

Hello World

HTML in

Eclipse16Prof. Vinod Pillai

Application Dev. [Eclipse] Step 2: [Creating Dynamic Web Project]

17Prof. Vinod Pillai

Open Eclipse and go to File -> New -> Project and selectDynamic Web Project in the New Project wizard screen.

18Prof. Vinod Pillai

Project Name: HelloWorld. Target runtime: <Select New Runtime> & show the path where you have unzipped the Tomcat Server. Dynamic web module version: 2.5.

19Prof. Vinod Pillai

HelloWorld Application Folder Structure

20Prof. Vinod Pillai

Creating HTML/JSP/Servlet:

21Prof. Vinod Pillai

Creating HTML/JSP/Servlet:

22Prof. Vinod Pillai

Running the application [Static / Dynamic Project]:

23Prof. Vinod Pillai

Running the application [Static / Dynamic Project]:

24Prof. Vinod Pillai

Final Output:

25Prof. Vinod Pillai

HTML Page

calling

Servlet26Prof. Vinod Pillai

Java Web Application - Servlet Servlet

A servlet is a small Java program that runs within a Webserver. Servlets receive and respond to requests from Webclients, usually across HTTP, the HyperText TransferProtocol.

Important methods: void doGet(HttpServletRequest request, HttpServletResponse

response)

void doPost(HttpServletRequest request, HttpServletResponse response)

27Prof. Vinod Pillai

Creating Servlet:

28Prof. Vinod Pillai

Servlet Code:

29Prof. Vinod Pillai

Telling HTML to call the Servlet:

30Prof. Vinod Pillai

JSP Page

calling

Servlet31Prof. Vinod Pillai

Java Web Application - JSP JSP [JavaServer Pages]

JavaServer Pages (JSP) technology allows you to easilycreate Web content that has both static and dynamiccomponents. JSP technology projects all the dynamiccapabilities of Java Servlet technology but provides amore natural approach to creating static content.

32Prof. Vinod Pillai

Creating JSP:

33Prof. Vinod Pillai

Creating JSP:

34Prof. Vinod Pillai

Java Web Application – Beans Javabean class is a type of java classes that is follow

some rules for methods and naming.

Major Rules of JavaBean class:

No-argument constructor.

The properties variables are private and accessed due theset and get methods.

The name of setter function is set followed by theproperty name but every first letter is upper case. (ex:setId() and getId()).

Setters and getters are public.

Getters have no parameter.

Setters have void return type. 35Prof. Vinod Pillai

Java Bean:

36Prof. Vinod Pillai

Java Web Application – web.xml According to the Servlet 2.4 specification, every Web

application should include a deployment descriptor(web.xml file). This file must be placed in the WEB-INF/ directory of the Web application.

Be very careful when making modifications to this file(such as any additions or changes) because they willaffect all Web applications running.

37Prof. Vinod Pillai

web.xml:

38Prof. Vinod Pillai

MVC

Architecture

39Prof. Vinod Pillai

MVC Architecture The MVC (Model-View-Controller) architecture is a

way of decomposing an application into three parts:

Model

View

Controller.

Model : A model represents an application’s data andcontains the logic for accessing and manipulating thatdata. Any data that is part of the persistent state of theapplication should reside in the model objects.

40Prof. Vinod Pillai

MVC Architecture View: View represents the presentation of the

application. The view object refers to the model. It usesthe query methods of the model to obtain the contentsand renders it. The view modifies itself when a changein the model is communicated to the view.

Controller : Whenever the user sends a request forsomething then it always go through the controller. Thecontroller is responsible for intercepting the requestsfrom view and passes it to the model for the appropriateaction. After the action has been taken on the data, thecontroller is responsible for directing the appropriateview to the user.

41Prof. Vinod Pillai

Model View Controller (MVC)

42Prof. Vinod Pillai

MVC Application [Add two integer values]

43Prof. Vinod Pillai

Controller [Servlet]:

44Prof. Vinod Pillai

Model [Java Class / Bean or Both]:

45Prof. Vinod Pillai

View [Result JSP]:

46Prof. Vinod Pillai

Output:

47Prof. Vinod Pillai

Struts

Framework

48Prof. Vinod Pillai

Introduction Java Application = Desktop || Web || Mobile.

Struts 2= Apache Struts Web Framework.

Framework:

Peace of Software that Automates all tedioustask.

Use Design patterns commonly agreed byIndustry.

In built features that commonly needed by mostof project.

49Prof. Vinod Pillai

Jakarta Struts The Apache Struts web framework is a free open-

source solution for creating Java web applications.

Apache Struts was launched in May 2000, withversion 1.0 officially released in July 2001.

Why we need it?

Web applications based on JSP sometimes combinesdatabase code, page design code, and control flow code.

In practice, we find that unless these concerns areseparated, larger applications become difficult tomaintain.

50Prof. Vinod Pillai

Jakarta Struts One way to separate concerns in a software

application => Model-View-Controller (MVC)architecture.

MVC

Model = Business or database code

View = Page design code

Controller = Navigational code.

Struts framework is designed to help developerscreate web applications that utilize a MVCarchitecture.

51Prof. Vinod Pillai

Struts 2 Apache Struts Project offers two major versions:

Struts 1 is recognized as the most popular web applicationframework for Java.

Struts 2 was originally known as WebWork 2.WebWorkand Struts communities joined forces to create Struts 2.

Struts 2 is a pull-MVC framework. i.e. the data that is tobe displayed to user has to be pulled from the Action.

Action class in Struts 2 act as the model in the webapplication.

Unlike Struts, Struts 2 Action class are plain POJOobjects thus simplifying the testing of the code.

52Prof. Vinod Pillai

Struts 2 Struts2 also comes with power APIs to configure

Interceptors that reduce greatly the coupling inapplication.

The view part of Struts 2 is highly configurable and itsupports different result-types such as Velocity,FreeMarker, JSP, etc.

53Prof. Vinod Pillai

Struts 2 Architecture

54Prof. Vinod Pillai

Struts 2 Architecture1. Request is generated by user and sent to Servlet

container [web.xml].

2. Servlet container invokes FilterDispatcher filter which inturn transfer the call to [struts.xml] and finds theappropriate action .

3. One by one Intercetors are applied before calling theAction. Interceptors performs tasks such as Logging,Validation, File Upload, Double-submit guard etc.

4. Action is executed and the Result is generated by Actionif the state of Model is to be Change then do so.

55Prof. Vinod Pillai

Struts 2 Architecture5. The output of Action is rendered in the view (JSP,

Velocity, etc) and the result is ready to returned to theuser.

6. The response passes through the interceptors in reverseorder to perform any clean-up or additional processing.

7. Now the Control is with [Servlet Engine] and the resultis rendered to the user.

56Prof. Vinod Pillai

Part – I : Understanding Struts Architecture.

Overview of Web Application.

Servlet.

JSP.

Java Beans.

MVC Architecture.

Simple MVC Base Application.

Understanding Struts 2 Architecture.

Prof. Vinod Pillai 57

Part – I

Completed

58Prof. Vinod Pillai

Thank You

vinodthebest@gmail.com

59Prof. Vinod Pillai