Application- 7 : Creating a Calculated Day in a Week Application with Struts
Previous Home Next

In this chapter, you create a very simple application for the purpose of Calculated Day in a week with Struts from start to end. For each piece of the application, we show you the code and then explain the code. 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 computeDay. computeDay application contain following page-

    • Three View file name as index.jsp, and display.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- 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.

    Step- 3: Open index.jsp page, from your struts application and code it as describe below.

    <%-- Document: index.jsp
    Description : First Page of EmailLogin 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; charset=UTF-8">
    <title><bean:message key="welcome.title"/></title>
    <body style="background-color: white">
    <html:form action="/compute">
    <h3><bean:message key="welcome.heading"/></h3>
    <p><bean:message key="welcome.message"/></p>
     <html:errors property="pre_gregorian"/>
    <table border="0" cellspacing="4" cellpadding="4">
    <td><html:errors property="" />
    <bean:message key="" /></td>
    <td><html:text property="day" size="20" 
    maxlength="2" /></td>
    <td><html:errors property="month"/>
    <bean:message key="welcome.month" /></td>
    <td><html:text property="month" size="20" 
    maxlength="2" /></td>
    <td><html:errors property="year"/>   
    <bean:message key="welcome.year" /></td>
    <td><html:text property="year" size="20" 
    maxlength="4" /></td>
    <td align="center"><html:submit value="" /></td>
    <td align="center"><html:reset value="" /></td>

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

    <%--  Document : dispaly.jsp
    Description  : Second Page of EmailLogin 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>JSP Page</title>
    <h1>Check your Day!</h1>
    <p> Calculated, Day of Week : <strong> <bean:write
    name="computeForm" property="setDayOfWeek"/></strong> </p>
    <p> Return to <a style="cursor: auto; color: green"
    href="index.jsp"> home Page </a></p>

    Step- 5: 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" xmlns="
    <!-- 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- 5: 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"
    <form-bean name="computeForm" type="r4r.computeForm"/>
    <action input="/index.jsp" name="computeForm" 
    path="/compute" scope="request" type="r4r.computeAction">
      <forward name="success" path="/dispaly.jsp"/>
      <forward name="failure" path="/index.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- 7: 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 computeDay application

    /* Save as a */
    package r4r;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    public class computeForm extends org.apache
    	.struts.action.ActionForm {
        private String setDayOfWeek;
        private int day, month, year;
        public int getDay() {
            return day;
        public void setDay(int day) {
   = day;
        public int getMonth() {
            return month;
        public void setMonth(int month) {
            this.month = month;
        public int getYear() {
            return year;
        public void setYear(int year) {
            this.year = year;
        public String getSetDayOfWeek() {
            return setDayOfWeek.toString();
          void setDayOfWeek(String setDayOfWeek) {
           this.setDayOfWeek = setDayOfWeek;
        public computeForm() {
        /*This is the action called from the Struts framework.
        public ActionErrors validate(ActionMapping mapping,
    		HttpServletRequest request) {
            ActionErrors errors = new ActionErrors();
            if (month == 0) {
                errors.add("month", new ActionMessage("error.month"));
            if (year == 0 || year < 4) {
                errors.add("year", new ActionMessage("error.year"));
            if (year < 1582) {
    				new ActionMessage("error.pre_gregorian"));
            return errors;

    Step- 8: Action class of computeDay application

    /* Save as a */
    package r4r;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    public class computeAction extends org.apache
    	.struts.action.Action {
     /* forward name="success" path="" */
     private static final String SUCCESS = "success";
     private static final String FAILURE = "failure";
     /* This is the action called from the Struts framework.  */
     public ActionForward execute(ActionMapping mapping, 
    	 ActionForm form,HttpServletRequest request,
    	 HttpServletResponse response)throws Exception {
      ActionErrors errors = new ActionErrors();
      computeForm dateData = (computeForm) form;
      //Taking parameter form the Bean Class
      int day = dateData.getDay();
      int month = dateData.getMonth();
      int year = dateData.getYear();
      //Define Varable
      int dayOfWeek, valcen, valleap, valmon,
    	  valyear, valday;
      //Declear centuries
      int[] centuries = new int[4];
      centuries[0] = 2;
      centuries[1] = 0;
      centuries[2] = 5;
      centuries[3] = 3;
      //Complete List of Month in a single year
      int[] months = new int[13];
      months[1] = 5;
      months[2] = 1;
      months[3] = 0;
      months[4] = 3;
      months[5] = 5;
      months[6] = 1;
      months[7] = 3;
      months[8] = 6;
      months[9] = 2;
      months[10] = 4;
      months[11] = 0;
      months[12] = 2;
      //Complete List of Days in a single week
      String[] daysOfWeek = new String[7];
      daysOfWeek[0] = "Sunday";
      daysOfWeek[1] = "Monday";
      daysOfWeek[2] = "Tuesday";
      daysOfWeek[3] = "Wednesday";
      daysOfWeek[4] = "Thursday";
      daysOfWeek[5] = "Friday";
      daysOfWeek[6] = "Saturday";
      try {
    if (month < 3) {
     year--; // Subtract 1 from year
    valcen = centuries[year / 100 % 4];
    valleap = year % 100 / 4;
    valyear = year % 100 % 7;
    valmon = months[month];
    valday = day % 7;
    dayOfWeek = valcen + valleap + valyear + valmon + valday;
    dayOfWeek = dayOfWeek % 7;
    //Set setDayofWeek property into Bean Class
      //Set the parameter to the JSP page
    request.setAttribute("dateData", dateData);
      } catch (Exception e) {
    errors.add("exception", new ActionMessage
     throw new UnsupportedOperationException
     /* Save error messages keys into HTTP request 
     attribute for use by the <html:errors> tag*/
      saveErrors(request, errors);
      if (errors.isEmpty()) {
    return mapping.findForward(SUCCESS);
      } else {
    return mapping.findForward(FAILURE);

    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 Validation, click submit button
    Previous Home Next