0% found this document useful (0 votes)
14 views28 pages

Ajava Unit II

The document provides an overview of the Struts 2 framework, detailing its architecture, features, and advantages over Struts 1. It explains the MVC design pattern, the steps to create a Struts 2 application, and how to integrate it with Hibernate for database operations. Additionally, it outlines the use of Tiles for page layout management and code reusability in Struts applications.

Uploaded by

princesharma7199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

Ajava Unit II

The document provides an overview of the Struts 2 framework, detailing its architecture, features, and advantages over Struts 1. It explains the MVC design pattern, the steps to create a Struts 2 application, and how to integrate it with Hibernate for database operations. Additionally, it outlines the use of Tiles for page layout management and code reusability in Struts applications.

Uploaded by

princesharma7199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

1

ADVANCED JAVA
PCC-CSE-306G

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-
2

UNIT-II
STRUTS
Servlets are the Java programs that runs on the Java-enabled web server or The struts 2
framework is used to develop MVC-based web application.
The struts framework was initially created by Craig McClanahan and donated to Apache
Foundation in May, 2000 and Struts 1.0 was released in June 2001.

When you use Struts, the framework provides you with a controller
servlet, ActionServlet, which is defined in the Struts libraries that are included in the
IDE, and which is automatically registered in the web.xml deployment descriptor as
shown below. The controller servlet uses a struts-config.xml file to map incoming
requests to Struts Action objects, and instantiate any ActionForm objects associated
with the action to temporarily store form data. The Action object processes requests
using its execute method, while making use of any data stored in the form bean. Once
the Action object processes a request, it stores any new data (i.e., in the form bean, or
in a separate result bean), and forwards the results to the appropriate view.

Faculty : Dr. Ashima Mehta Date:


Date:
Branch
Branch &
& Semester
Semester :: CSIT_VI SEM
CSE_IT_CSIT_VI SEM Subject with CodeSubject
: CLOUD COMPUTING(PCC-IT-
with Code : AJAVA(PCC-
3

UNIT-II
STRUTS ARCHITECTURE

Faculty : Dr. Ashima Mehta Date:


4

Struts 2 provides many features that were


not in struts 1. The important features of
struts 2 framework are as follows:
1.Configurable MVC components
2.POJO based actions
3.AJAX support
4.Integration support
5.Various Result Types
6.Various Tag support
7.Theme and Template support

Faculty : Dr. Ashima Mehta Date:


5

1) Configurable MVC components


In struts 2 framework, we provide all the
components (view components and action)
information in struts.xml file. If we need to
change any information, we can simply
change it in the xml file.
2) POJO based actions
In struts 2, action class is POJO (Plain Old
Java Object) i.e. a simple java class. Here,
you are not forced to implement any
interface or inherit any class.
3) AJAX support
Struts 2 provides support to ajax technology.
It is
Faculty : Dr. used to make asynchronous request i.e. Date:
Ashima Mehta
6

4) Integration Support
We can simply integrate the struts 2 application with
hibernate, spring, tiles etc. frameworks.
5) Various Result Types
We can use JSP, freemarker, velocity etc.
technologies as the result in struts 2.
6) Various Tag support
Struts 2 provides various types of tags such as UI
tags, Data tags, control tags etc to ease the
development of struts 2 application.
7) Theme and Template support
Struts 2 provides three types of theme support:
xhtml, simple and css_xhtml. The xhtml is default
theme of struts 2. Themes and templates can be
used for common look and feel.
. : Dr. Ashima Mehta
Faculty Date:
7

Model 1 and Model 2 (MVC) Architecture


Before developing the web applications, we need to
have idea about design models. There are two types
of programming models (design models)
1.Model 1 Architecture
2.Model 2 (MVC) Architecture
Model 1 Architecture
Servlet and JSP are the main technologies to develop the web
applications.
Servlet was considered superior to CGI. Servlet technology
doesn't create process, rather it creates thread to handle
request. The advantage of creating thread over process is that
it doesn't allocate separate memory area. Thus many
subsequent requests can be easily handled by servlet.
Problem in Servlet technology Servlet needs to recompile if
any designing code is modified. It doesn't provide separation of
concern. Presentation and Business logic are mixed up.
Faculty : Dr. Ashima Mehta Date:
8

JSP overcomes almost all the problems of Servlet. It


provides better separation of concern, now
presentation and business logic can be easily
separated. You don't need to redeploy the
application if JSP page is modified. JSP provides
support to develop web application using JavaBean,
custom tags and JSTL so that we can put the
business logic separate from our JSP that will be
easier to test and debug.

Faculty : Dr. Ashima Mehta Date:


9

As you can see in the above figure, there is picture which show
the flow of the model1 architecture.
1.Browser sends request for the JSP page
2.JSP accesses Java Bean and invokes business logic
3.Java Bean connects to the database and get/save data
4.Response is sent to the browser which is generated by JSP
Advantage of Model 1 Architecture
•Easy and Quick to develop web application
Disadvantage of Model 1 Architecture
•Navigation control is decentralized since every page
contains the logic to determine the next page. If JSP page
name is changed that is referred by other pages, we need to
change it in all the pages that leads to the maintenance
problem.
•Time consuming You need to spend more time to develop
custom tags in JSP. So that we don't need to use scriptlet tag.
•Hard to extend It is better for small applications but not for
large
Faculty : Dr.applications.
Ashima Mehta Date:
1
0

Model 2 (MVC) Architecture


Model 2 is based on the MVC (Model View Controller) design
pattern. The MVC design pattern consists of three modules
model, view and controller.
Model The model represents the state (data) and business
logic of the application.
View The view module is responsible to display data i.e. it
represents the presentation.
Controller The controller module acts as an interface between
view and model. It intercepts all the requests i.e. receives input
and commands to Model / View to change accordingly.

Faculty : Dr. Ashima Mehta Date:


1
1

Advantage of Model 2 (MVC) Architecture


•Navigation control is centralized Now only
controller contains the logic to determine the next
page.
•Easy to maintain
•Easy to extend
•Easy to test
•Better separation of concerns
Disadvantage of Model 2 (MVC) Architecture
•We need to write the controller code self. If we change
the controller code, we need to recompile the class and
redeploy the application.

Faculty : Dr. Ashima Mehta Date:


1
2

Example to create struts 2 application in MyEclipse


You need to follow these steps to create struts 2 application.
1.Create a web project
2.Add struts 2 capabilities
3.Create input page (index.jsp)
4.Create the action class (Product.java)
5.Map the request with the action in (struts.xml) file and define
the view components
6.Create view components (welcome.jsp)
7.start server and deploy the project
1) Create a web project
To create web project, click on the file menu
- new - project - web project - write the project name e.g.
firststruts - finish.
2) Add struts 2 capabilities
To add struts 2 capabilities, select you project - click on
the myeclipse menu - add project capabilities - add struts
capabilities.

Faculty : Dr. Ashima Mehta Date:


1
3

Faculty : Dr. Ashima Mehta Date:


1
4

3) Create input page (index.jsp)


It uses struts core tags to create a form with fields.
index.jsp
1.<%@ taglib uri="/struts-tags" prefix="s" %>
2.<s:form action="product">
3.<s:textfield name="id" label="Product Id"></
s:textfield>
4.<s:textfield name="name" label="Product Name"></
s:textfield>
5.<s:textfield name="price" label="Product Price"></
s:textfield>
6.<s:submit value="save"></s:submit>
7.</s:form>

Faculty : Dr. Ashima Mehta Date:


1
5

4) Create the action class (Product.java)


It is the simple action class containing properties with setters and getters. It
contains the execute method also for defining the business logic.
Product.java
1.package com.javatpoint;
2.
3.public class Product {
4.private int id;
5.private String name;
6.private float price;
7.public int getId() {
8. return id;
9.}
10.public void setId(int id) {
11. this.id = id;
12.}
13.public String getName() {
14. return name;
15.}
16.public void setName(String name) {
17. this.name = name;
18.}
19.public float getPrice() {
20. return price;
21.}
22.public void setPrice(float price) {
23. this.price = price;
24.}
25.
26.public String execute(){
27. return "success";
28.}
Faculty : Dr. Ashima29.}
Mehta Date:
1
6
5) Map the request in (struts.xml) file and define
the view components
This xml file registers the action and view
components.
6) Create view components (welcome.jsp)
This jsp page displays the information set in the
action object.
welcome.jsp
1.<%@ taglib uri="/struts-tags" prefix="s" %>
2.
3.Product Id:<s:property value="id"/><br/>
4.Product Name:<s:property value="name"/
><br/>
5.Product Price:<s:property value="price"/
><br/>
7) start server and deploy the project
To start the server and deploy the project, right
click on your project - Run As - MyEclipse server
application
Faculty : Dr. Ashima Mehta Date:
1
7

For Forfffffdvxdvd
For configuration, interceptors, validation method, aware Interfaces, stuts2withI18N, zero
configuration,
Refer https://www.javatpoint.com/struts-2-interceptors-tutorial

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-CSE-306G)
1
8

Struts2withtiles
ADVANTAGES WITH TILES
Customization by centralized page We can customize the layout of all the pages by single
page (centralized page) only.
Code reusability A single part e.g. header or footer can be used in many pages. So it saves
coding.
Easy to modify If any part (tile) is modified, we don't need to change many pages.
Easy to remove If any part (tile) of the page is removed, we don't need to remove the
code from all the pages. We can remove the tile from our layout manager page.

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-CSE-306G)
1
9

Struts2withtiles
• Steps to create tiles application
• The steps are as follows:
• Add tiles library in your application
• Define Struts2TilesListener in web.xml file
• Create the input page (index.jsp)
• Create the Action class
• Extend the tiles-default package in your package and define all the result type as tiles
in struts.xml file
• Create the tiles.xml file and define all the tiles definitions
• Create the LayoutManager page
• Create the View components

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-CSE-306G)
2
0

We can integrate any struts application with hibernate. There is no requirement of extra efforts.
In this example, we going to use struts 2 framework with hibernate. You need to have jar files for struts 2
and hibernate.
Example of Hibernate and struts2 integration
In this example, we are creating the registration form using struts2 and storing this data into the
database using Hibernate. Let's see the files that we should create to integrate the struts2 application
with hibernate.
•index.jsp file to get input from the user.
•User.java A action class for handling the request. It uses the dao class to store the data.
•RegisterDao.java A java class that uses DAO design pattern to store the data using hibernate.
•user.hbm.xml A mapping file that contains information about the persistent class. In this case, action
class works as the persistent class.
•hibernate.cfg.xml A configuration file that contains informations about the database and mapping file.
•struts.xml file contains information about the action class and result page to be invoked.
•welcome.jsp A jsp file that displays the welcome information with username.
•web.xml A web.xml file that contains information about the Controller of Struts framework.

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-CSE-306G)
2
1

We can integrate any struts application with hibernate. There is no requirement of extra efforts.
In this example, we going to use struts 2 framework with hibernate. You need to have jar files for struts 2
and hibernate.
Example of Hibernate and struts2 integration
In this example, we are creating the registration form using struts2 and storing this data into the
database using Hibernate. Let's see the files that we should create to integrate the struts2 application
with hibernate.
•index.jsp file to get input from the user.
•User.java A action class for handling the request. It uses the dao class to store the data.
•RegisterDao.java A java class that uses DAO design pattern to store the data using hibernate.
•user.hbm.xml A mapping file that contains information about the persistent class. In this case, action
class works as the persistent class.
•hibernate.cfg.xml A configuration file that contains informations about the database and mapping file.
•struts.xml file contains information about the action class and result page to be invoked.
•welcome.jsp A jsp file that displays the welcome information with username.
•web.xml A web.xml file that contains information about the Controller of Struts framework.

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-CSE-306G)
2
2

index.jsp
In this page, we have created a form using the struts tags. The action name for this
form is register.
<%@ taglib uri="/struts-tags" prefix="S" %>

<S:form action="register">
<S:textfield name="name" label="Name"></S:textfield>
<S:submit value="register"></S:submit>

</S:form>

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-CSE-306G)
2
3

User.java
It is a simple POJO class. Here it works as the action class for struts and persistent class
for hibernate. It calls the register method of RegisterDao class and returns success as
the string.
package com.javatpoint;

public class User {


private int id;
private String name;
//getters and setters

public String execute(){


RegisterDao.saveUser(this);
return "success";
}

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSE_IT_CSIT_VI SEM Subject with Code : AJAVA(PCC-CSE-306G)
2
4

MAIL API
There are various ways to send email using JavaMail API. For this purpose, you must
have SMTP server that is responsible to send mails.
You can use one of the following techniques to get the SMTP server:
Install and use any SMTP server such as Postcast server, Apache James server, cmail
server etc. (or)
Use the SMTP server provided by the host provider e.g. my SMTP server is
mail.javatpoint.com (or)
Use the SMTP Server provided by other companies e.g. gmail etc.
Steps to send email using JavaMail API
There are following three steps to send email using JavaMail. They are as follows:
Get the session object that stores all the information of host like host name, username,
password etc.
compose the message
send the message

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSIT_VI SEM Subject with Code : CLOUD COMPUTING(PCC-IT-
2
5

MAIL API
Example to compose the message:
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient(Message.RecipientType.To,
new InternetAddress("[email protected]"));
message.setHeader("Hi, everyone");
message.setText("Hi, This mail is to inform you...");

Example to send the message:


Transport.send(message);

Faculty : Dr. Ashima Mehta Date:


Branch & Semester : CSIT_VI SEM Subject with Code : CLOUD COMPUTING(PCC-IT-
2
ublic class SendEmail 6

ublic static void main(String [] args){


String to = "[email protected]";//change accordingly
String from = "[email protected]";change accordingly
String host = "localhost";//or IP addressMAIL API

//Get the session object


Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);

//compose the message


try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");

// Send message
Transport.send(message);
System.out.println("message
Faculty : Dr. Ashima Mehta sent successfully...."); Date:
Branch & Semester : CSIT_VI SEM Subject with Code : CLOUD COMPUTING(PCC-IT-
• For receiving email, sending attachment,
receiving attachment, sending html,
forwarding, deleting email;
• Refer to the following link
• https://www.javatpoint.com/example-of-
sending-email-using-java-mail-api

27
• THANK YOU

28

You might also like