> Overview (Steps)

Struts lesson 3: Utilizing the Action servlet of Struts

For this document and all references (links) please obey the hints and regulations concerning copyright, disclaimer and trademarks.

Last revision of this document:
2005-07-04

First a confession:
The two previous two lessons had 'Struts' in their title – but did not use any element of Struts.

They were build into the framework of Struts as a preparation for the more sophisticated lessons like this one.
The two previous lessons should have trained to build web-pages (either HTML or JSP) and pack the created web-site into WAR- and EAR-files.
(If the appreviations sound strange, it is recommended to work again through JS_Struts01 – HTML in a WAR and JS_Struts02 – JSP in an EAR.)

And now another confession – or better a warning:
There is no Intermediate Test within this lesson. So please be careful when typing the example as this lesson might be seeming pretty long and getting boring.

Preface:

A graphical scheme of the task:

This lesson utilizes the features of Struts.

* From the 'index.jsp' page the Struts-Action-Controller is triggered.
* The Action-Servlet, written in this lesson, contains also business-logic ('Model' in Struts-terminology).
* The code of the business-logic decides, which of 2 follow-up-pages is displayed.
* From each of the follow-Up-pages the Struts-Action-Controller can be triggered again.

Credits:

I derived that lesson from a lot of sources found in the internet. I thank the numerous authors for their contribution.

Prerequisites:

Preparation:

Create a new project (named JS_Struts03) by copying the Struts-framework (JS_Struts00):

top.

Writing the JSP-code:

For instructions how to create JSP-files, please see the instruction with screen shots at Writing the JSP-code of JS_Struts02 – JSP in an EAR.

A short explanation of the newly introduced elements in the code.
These elements are interpreted by the Java-Application-Server as soon as the web-page is called.

A short explanation of the newly introduced elements in the code follows.
These elements are interpreted by the Java-Application-Server as soon as the web-page is called.

A short explanation of the newly introduced elements in the code follows.
These elements are interpreted by the Java-Application-Server as soon as the web-page is called.

top.

Defining the Libraries in the Java Build Path:

Here all the standard-libraries with classes called either by individual code or by Struts-functions are defined.
These are defined now as otherwise errors are indicated when the Java-code for the servlet is written.

top.

Writing the Servlet:

This is the Action-servlet as requiered by the Struts-framework.
As the business-logic (the 'model' according to Struts-terminolgy) is pretty simple it is also incorporated in this servlet.
The separation between 'Action' (Struts-'Controller') and 'Model' is done in the next lesson.

A short explanation of the code.
The elements are interpreted by the Java-Application-Server as soon as the web-page is called.

top.

Defining the Struts-mapping files 'web.xml' and 'struts-config.xml':

In the JSP-files are 'forms' defined where the 'actions' trigger a process performed by the Struts-framework.
The processing is partly defined by the 'struts-config.xml' file within the folder 'WEB-INF'.
.

top.

Making the packaging configuration and pack the HTML-files into a WAR (Web-ARchive):

For instructions how to define the files to be packed into the WAR-file, please see the instruction with screen shots at Making the packaging configuration and pack the HTML-files into a WAR (Web-ARchive) of JS_Struts02 – JSP in an EAR.

top.

Defining of the 'application.xml'-file as a prerequisite for an EAR (Enterprise-ARchive):

As the project should be registered under another name (strutslesson03) than the name of the WAR-file (JS_Struts03.war), it is necessary to find out which parameter-file is responsible for that.
As the concept of the WAR does not know a different registration-name, the WAR-file (and the parameter-file(s)) is packed into an EAR (Enterprise-ARchive) file.
In one of the following lessons is also shown, that the EAR can contain other parameter-files, e.g. references to a database.



Define the (different) registration name (strutslesson03) under which the web-site will be registered in the Java-Application-Server.
This parameter is set.

For a short explanation of the elements please see at Defining of the 'application.xml'-file of JS_Struts02 – JSP in an EAR. .

top.

Making the packaging configuration and pack the files into an EAR (Enterprise-ARchive):

This is the final packaging-configuration.

Pack the WAR-file and the 'application.xml' parameter-file into the EAR-file..

For instructions how to define the files to be packed into the EAR-file, please see the instruction with screen shots at Making packaging configuration and pack the files inta an EAR of JS_Struts02 – JSP in an EAR.

top.

Run / Test:

This is the final verification if the generated 'JS_Struts03.ear' file (with an Enterprise ARchive structure) is running error-free within a Java-Application-Server.

For the conceptional background of deployment to a Java-Application-Server and the assumptions of the directories with the Eclipse-workspace and and the Java-Application-Server-deployment, please see Run / Test in JS_Struts01 – HTML in a WAR .

Note: There is also a functionality in Eclipse to define the Java-Application-Server and deploy and undeploy with an Eclipse-function.
I was not pretty happy with that feature. My
JBoss-Application-Server is running on another machine. When this machine is not running during the start of Eclipse, the Eclipse-function gets confused, reports that a changed WAR-file is deployed - but the old file is not overwritten.
So I prefer to stay on the more secure side and copy the files using the 'Terminal'-window.

top.

Related Documents: