Application-5 : Creating a Simple Lottery Annotation Application with Struts
Previous Home Next

In this chapter, you create a simple Lottery Annotation application with Struts from start to end. For each piece of the application, we show you the code and then explain the code.

Note:-This application design in NetBeans IDE- 6.8 with Struts 1.3.

Requirement for your struts application-

  • At least one view component is required in every Application.
  • A subclass of ActionForm is used to submit the date as application needed.
  • A subclass of Action class which provide specific processing functionality for the application.
  • A web.xml file is required for calling ActionServlet and struts-config.xml.
  • A struts-config.xml file is required for updating and mapping the update.

Let's Start the application, this application provide a good understanding how struts framework works and also understanding the development of Struts applications.

Now, Following the some steps for develop your first application name as lotteryDraw. lotteryDraw application contain following page-

  • Three View file name as index.jsp, welcomeStruts.jsp, and success.jsp
  • One JavaBeans file name as used for store data as application need.
  • one Action file name as, used for provide specific processing functionality.
  • Two XML file name as web.xml and struts-config.xml file, used for provide control, mapping and validation in application.

Step- 1 : Start your IDE ( NetBeans 6.8 with struts 1.3).

Step- 2 : Open a new project( Crtl+Shift+N), name it , add server( Apache Tomcat 6.0.20 ), then add struts framework 1.3 , and don't forget to add Struts TLDs.

When you click Finish button, your NetBeans IDE configure everything in place and project open in your IDE.

<%-- Document    : index.jsp
Description : First Page of lotteryDraw Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!-- request global forward to the welcomeStruts.jsp-->
<jsp:forward page=""/>

Step- 4: Open welcomeStruts.jsp page from your struts application and code it as describe below.

<%-- Document: welcomeStruts.jsp
Description : Second Page of lotteryDraw Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="" 
prefix="bean" %>
<%@ taglib uri="" 
prefix="html" %>
<%@ taglib uri=""
prefix="logic" %>
<html:html lang="true">
<meta http-equiv="Content-Type" content="text/html;
<title><bean:message key="welcome.title"/></title>
<body style="background-color: white">
<logic:notPresent name="org.apache.struts.action.MESSAGE" 
<div  style="color: red">
ERROR:  Application resources not loaded -- check servlet
container logs for error messages.
<html:form method="post" action="/lottery" >
<h3 align="center"><bean:message key="welcome.heading"/></h3>
<p align="center"><bean:message key="welcome.message"/></p>
<hr align="center" width="600" size="2">
<table align="center" border="1" cellspacing="8" cellpadding="9"
style="border-color: #0066FF ">
<td><html:errors property="age" />
<html:errors property="shortAge" />
<bean:message key="welcome.age" />
<td><html:text property="age" maxlength="2" size="20"/></td>
<td><html:errors property="lottery" />
<bean:message key="" />
<td> <select name="lottery" size="4">
<option value="malamaal weekly"> Malamaal weekly </option>
<option value="Mega Million">Mega Million</option>
<option value="Super Lotto">Super Lotto</option>
<option value="Fantasy 5">Fantasy 5</option>
<option value="Big Spin">Big Spin</option>
<option value="Daily Derby">Daily Derby</option>
<td align="center"><html:submit property="submit"
value=" Play " /></td>
<td align="center"><html:reset property="reset" 
value=" Reset "/></td>
<font face="Arial" size="3" color="#008000"> 
<p align="center">When you click Play button, your 
lottery number will be generated. </p> </font>

Step- 5: Open success.jsp page from your struts application and code it as describe below.

Document: success
Description : Third Page of lotteryDraw Application
--%><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@ taglib uri=""
prefix="bean" %>
<meta http-equiv="Content-Type" 
content="text/html; charset=UTF-8">
<title>success Page</title>
<h1>Hello Guest! Please Note down your Number </h1>
<table border="1" cellspacing="5" cellpadding="5"
<td>Today Date:</td>
<td><strong> <bean:write name="LotteryForm"
<td>Selected Lottery:</td>
<td><strong> <bean:write name="LotteryForm"
property="lottery"/> </strong></td>
<td>Lottery Number:</td>
<td><strong> <bean:write name="LotteryForm"
property="number"/> </strong></td>
<ul><p>Good Luck for draw,
Wish to Play again, then <a style="cursor: auto;color: green"
href="index.jsp">Click</a> here.

Step- 6: Open web.xml file place in WEB-INF folder, this file is used to map the ActionServlet( work as backbone of application, every Action, Validation and ActionForm class control by this class) , struts-config.xml file, welcome file and five different TagLibrary.

<!-- Save as a web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
<!-- ActionServlet map here, however you can't find this class
within the project but it work as a backbone of the project -->
<!-- Your struts-config.xml file map here-->
<!-- Action class map with URL -->
<!-- Welcome file map here: -->
<!-- All the Tag Library which used with in the project -->

Step- 7: Open struts-config.xml file place in WEB-INF folder, this file is used to map Model class, Exception class, Validation class, Action class( Action class further choose View class) and ApplicationResource class.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation
//DTD Struts Configuration 1.3//EN"
<!-- Java Bean class Map here:-->
<form-bean name="LotteryForm" 
<forward name="welcome"path="/"/>
<!-- Action related stuff map here -->
<action input="/welcomeStruts.jsp" name="LotteryForm" 
path="/lottery" scope="request" 
<!-- Action class return success the call that page-->
<forward name="success" path="/success.jsp"/>
<!--Call that page until above condition not true -->
<forward name="failure" path="/welcomeStruts.jsp"/>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
<controller processorClass="org.apache.struts.tiles
<message-resources parameter="com/myapp/struts
<!-- ========= Tiles plugin =========-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" 
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />

<!-- ========== Validator plugin ========== -->
<plug-in className="org.apache.struts.validator

Step- 8: Now introduce Bean class into struts-config.xml file, As we define earlier, about how to introduce Form and Action class in the struts-config.xml file. Now, Bean class of lotteryDraw application

/* Save as a */
package r4r.struts;
import java.util.Calendar;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class LotteryForm extends org.apache
.struts.action.ActionForm {
private String lottery, date, number;
private int age;
//Getter/Setter property of all above parameter 
public int getAge() {
return age;
public void setAge(int age) {
this.age = age;
public String getLottery() {
return lottery;
public void setLottery(String lottery) {
this.lottery = lottery;
   //Getting random number
public String getNumber() {
int num1, num2, num3, num4, num5, num6;
//Produce only single random number
num1 = (int) (0 + (Math.random() * 9));
num2 = (int) (0 + (Math.random() * 9));
num3 = (int) (0 + (Math.random() * 9));
num4 = (int) (0 + (Math.random() * 9));
num5 = (int) (0 + (Math.random() * 9));
num6 = (int) (0 + (Math.random() * 9));
number = num1 + " " + num2 + " " + num3 + " " 
	+ num4 + " " + num5 + " " + num6;
return number;
public void setNumber(String number) {
this.number = number;
public void setDate(String date) { = date;
//Getting today date in format of DD/MM/YYYY
public String getDate() {
StringBuffer date = new StringBuffer();
Calendar calendar = Calendar.getInstance();
date.append(calendar.get(Calendar.MONTH) + 1);
return date.toString();
public LotteryForm() {
/* This is the action called from the Struts framework. */
public ActionErrors validate(ActionMapping mapping,
	HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getAge() == 0) {
errors.add("age", new ActionMessage("error.age.required"));
if (getAge() < 19) {
new ActionMessage("error.shortAge.required"));
if (lottery == null) {
	new ActionMessage("errr.lottery.required"));
return errors;

Step- 9: Action class of lotteryDraw application

/* Save as a */
package r4r.struts;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class LotteryAction extends 
	org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
/** This is the action called from the Struts framework.
 * @return */
public ActionForward execute
(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward(SUCCESS);

Run your application:

Now your application is complete, so now start run your application. Start your server( which you embedded when configure our struts application), then right click your application and click upon Run option or open your welcome( index.jsp) page and press shift+F6.

Check the validation-

Previous Home Next