Ajava Unit II
Ajava Unit II
ADVANCED JAVA
PCC-CSE-306G
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.
UNIT-II
STRUTS ARCHITECTURE
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
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
For Forfffffdvxdvd
For configuration, interceptors, validation method, aware Interfaces, stuts2withI18N, zero
configuration,
Refer https://www.javatpoint.com/struts-2-interceptors-tutorial
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.
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
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.
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.
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>
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;
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
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...");
// 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