12 - Program to list Emirp numbers from the list entered by user in Struts

Introduction:

  • This program will explain you how to list out all the Emirp numbers from the given starting and ending limits.
  • The inputs required are two numbers from two text fields and a Submit button

Requirements are:

  • Please find following link for program requirements.

“ _______________________________________”

Determine Emirp Numbers Application:

  1. Steps for creating Web Application:

  • Start NetBeans IDE 8.0.1(this is my installed version) and click on New Project. 

Figure : Creating new Project in NetBeans 8.0 (II) – Naming and saving project.

                       Figure : Creating new Project in NetBeans 8.0 (II) – Naming and saving project.

  • Select the first option (i.e. Web Application). (Note: the second option is to be selected when you already have a developed project and you want to add some extra features to it. And the third option creates a Web Application without any framework, which is not useful t us presently).
  • Then click on next and give your project a title. The default title is normally set to “Web Application 1” and it increments so on and so forth accordingly.
  • The default saving location of your project is in Users -> My Documents folder. It is a good habit to save the projects in Drives other than C Drive. 

Figure : Creating new Project in NetBeans 8.0 (IV) – Selecting server

              Figure : Creating new Project in NetBeans 8.0 (IV) – Selecting server

Figure : Creating new Project in NetBeans 8.0 (IV) – Selecting framework

  • Click on next and select the required server. The default server is Glassfish server. For fast server startup – shutdown as well as fast application access and run, it is advisable to use Apache Tomcat server. The version of Apache should be greater than 7.0.
  • Then click on next and select the framework you want to use. The current available options are:
    • Java Server Faces
    • Spring MVC
    • Hibernate
    • Struts 1.3
    • Struts 2
  • As Struts 2 is option is available (before installing struts 2 plugin, this option was disabled), select it and click on finish.

 

  1. Steps for writing Emirp Number application:
  • The first page of Web application looks complicated.
  • So, let us delete the files which are already given and we will create new files as per our requirements.
  • Before deleting anything, let us first understand the directory structure of Struts 2 applications.
  • You have been given following :
    • Web Application1
      • Web Pages
        • Meta – INF
          • Context.xml
        • Web – INF
          • Web.xml
        • Example
          • HelloWorld.jsp
      • Source Packages
        • Default package
          • Example.xml
          • Struts.xml
        • Example
          • HelloWorld.java
          • Package.properties
          • Package_es.properties
      • Libraries
        • All struts libraries
      • Configuration Files
        • MANIFEST.MF
        • Context.xml
        • Struts.xml
        • Web-fragment.xml
        • Web.xml
  • Here, the required things are : (rest all can be deleted)
    • Web Application 1
      • Web Pages
        • Meta – INF
          • Context.xml
        • Web – INF
          • Web.xml
      • Source Packages
        • Default package
      • Libraries
        • All struts libraries
      • Configuration Files
        • MANIFEST.MF
        • Context.xml
        • Web-fragment.xml
        • Web.xml
  • All the JSP’s (Java Server Pages), responsible for input – output activities are to be placed in the Web Pages folder. All the actions, that is, what should happen on button click or on dropdown menu click etc. are to be placed in the source packages folder. And the path to find these and connect these pages is to be placed in “configuration files” folder.

Note :

Steps to create Struts Application:

  1. Create JSPs
  2. Create action pages according to JSPs
  3. Connect the JSPs and action pages using configuration files (such as web.xml and struts.xml)
  • Now the web application is empty and will run nothing. So to begin from scratch, let us create a folder named “jsp” inside the web pages folder. And create “first.jsp” by selecting new Java server page document.
  • As the page is JSP (that is Java Server Page = HTML+ Java), we can embed HTML code in JSP page.  
  • The requirements here are of a Label to display instructions for entering two numbers, two text fields and a submit button.
  • Thus the first.jsp code will be:
// first.jsp

<%-- 
    Document   : first
    Created on : Nov 14, 2014, 12:49:46 PM
    Author     : Infinity
--%>

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<%@taglib  uri = "/struts-tags" prefix = "s" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
        <title>Emirp Number</title>
    </head>
    <body>
       <s:form action = "click" method = "post">
          <s:label value = "Enter the number to check"/>
          <s:textfield name = "number1"/>
          <s:textfield name = "number2"/>
          <s:submit value = "CHECK"/>
      </s:form>
    </body>
</html>
  • The same form can be created by HTML tags instead of struts tags.
  • After running this jsp, following output can be seen:

Fig - Program output screen

  • On button click, the page is transferred to “click” action page which is still unavailable. So error message will appear.
  • We now need a result page which displays the retrieved emirp number on submit button click. So create another jsp page named as “next.jsp” inside jsp folder.
  • Add the following statement to the result page for displaying user name :

The emirp numbers are: <s:property value="answer"/>

  • <s:property> tag is used to access the form field names from form elements using value attribute. Make sure that the value for “value” attribute is same as the name of form element you want to fetch.
  • So the next.jsp looks like:
// next.jsp

<%-- 
    Document   : next
    Created on : Nov 14, 2014, 1:19:42 PM
    Author     : Infinity
--%>

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<%@taglib prefix = "s" uri = "/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
        <title>Answer Page</title>
    </head>
    <body>
        <h1> 
        Emirp Numbers from 
        <s:property value = "number1"></s:property> 
        to 
        <s:property value = "number2"></s:property> 
        are : 
        <s:property value = "answer"></s:property> 
        </h1>
    </body>
</html>
  • Now to create action pages, create a folder named jsp_action inside source packages. The folder here is known as package.
  • Create action class “emirp_check” inside “jsp_action” package as seen in previous tutorial.
  • To make the java class support Actions in Struts, we should always extend it with base class “ActionSupport.java” which is residing under xwork package.
  • This action class resembles Java Beans class wherein, support for getter and setter methods for all the input variables must be provided.
  • The input variables here are variable “number1”, “number2”, “flag”, “flag1”, “answer”, “c” and “c1” (check the name attribute of text field in our form).
  • Here :
    • Answer variable : stores all the emirp number
    • Flag variable  : Boolean variable used to determine whether the number prime or not
    • Flag1 variable : Boolean variable used to determine whether the reverse number is prime or not
    • Counter c variable: integer variable used to count the iterations for prime number
    • Counter c1 variable: integer variable used to count the iterations for reverse prime number.                                                                                                                                                                                                       Logic for Emirp Numbers:
  • The number which is itself prime and its reverse is also prime are termed as EMIRP numbers.
  • So our first task is to find whether the numbers from the listed upper and lower limits are prime or not.
  • For example, if user enters lower limit as 50 and upper limit as 100, then the prime numbers are:

53, 59, 61, 67, 71, 73, 79, 83, 89, 97

 

  • Of these numbers, the numbers who are prime + prime by reverse are termed as emirp numbers. So these numbers are:

71, 73, 79, 97

  • So the required getter and setter methods are as follows:
public void setC(int c)
    {
        this.c = c;
    }

    public int getC()
    {
        return c;
    }

    public void setC1(int c1)
    {
        this.c1 = c1;
    }

    public int getC1()
    {
        return c1;
    }

    public void setFlag(boolean flag)
    {
        this.flag = flag;
    }

    public void setFlag1(boolean flag1)
    {
        this.flag1 = flag1;
    }

    public boolean getFlag()
    {
        return flag;
    }

    public boolean getFlag1()
    {
        return flag1;
    }

    ublic void setAnswer(String answer)
    {
        this.answer = answer;
    }
    public String getAnswer()
    {
        return answer;
    }
    public void setNumber1(int number1)
    {
        this.number1 = number1;
    }

    public void setNumber2(int number2)
    {
        this.number2 = number2;
    }
    public int getNumber1()
    {
        return number1;
    }

    public int getNumber2()
    {
        return number2;
    }
  • Now, on button click this class will be executed. We need following methods:
    • check(): that returns string of “success” if the number is emirp. This method will in-turn call other methods for first determining whether the numbers are prime, then reverse the number and then again determine whether the reverse number is prime or not.
    • check_prime(): this method returns true (Boolean) if the number is prime. The number is passed as an integer argument to this function
    • rev(): this method returns the integral reverse of the input number as arguments to it.
  • So the action class (emirp_check.java) will be as follows:
//emirp_check.java

package jsp_action;

import com.opensymphony.xwork2.ActionSupport;

public class emirp_check extends ActionSupport
{

    private int number1, number2, c = 0,c1 = 0;
    private String answer = "";
    private boolean flag, flag1;

    public void setC(int c)
    {
        this.c = c;
    }
    public int getC()
    {
        return c;
    }
    public void setC1(int c1)
    {
        this.c1 =c1;
    }
    public int getC1()
    {
        return c1;
    }
     public void setFlag(boolean flag)
    {
        this.flag = flag;
    }
    public void setFlag1(boolean flag1)
    {
        this.flag1 = flag1;
    }
    public boolean getFlag()
    {
        return flag;
    }
    public boolean getFlag1()
    {
        return flag1;
    }
    public void setAnswer(String answer)
    {
        this.answer = answer;
    }
    public String getAnswer()
    {
        return answer;
    }
    public void setNumber1(int number1)
    {
        this.number1 = number1;
    }
    public void setNumber2(int number2)
    {
        this.number2 = number2;
    }
    public int getNumber1()
    {
        return number1;
    }
    public int getNumber2()
    {
        return number2;
    }
    public String check()
    {
      int value_temp;
       for(int i = number1 ; i <= number2 ; i++)
        {

          flag = check_prime(i);
          if(flag)
            {
                value_temp = rev(i);
                flag1 = check_prime(value_temp);
                if(flag1)
                {
                    answer += " " +  String.valueOf(i);
                 }
           }
        }
       return "success";
}

boolean check_prime(int x)
{
c=0;
for(int i = 1 ; i <= x ; i++)
{    
    if(x % i  == 0)
        {
           c++;
        }
 }
if(c == 2)
    {
                  return true;
    }
    else
    {
                 return false;
    }
}

int rev(int i)
    {

     int temp = i;
        int rev = 0;
        while(i != 0)
        {
          rev = rev * 10;
                          rev = rev + i % 10;
                          i = i/10;
        }
                return rev;
    }
}

 

  • The last thing left is to co-ordinate the action classes with the JSP pages. This can be done by creating struts.xml file. This is also known as configuration file of Struts 2 Framework. The file consists of mapping of actions to respected JSP pages. What should be executed when is specified in here. The file contains mapping inside <struts> tags as follows:
  • In our example, struts.xml can written as:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>

   <!-- Configuration for the default package. -->
    <package name = "default" extends = "struts-default">
        <action name = "click" class = "jsp_action.emirp_check" method = "check">
            
            <result name = "success">next.jsp</result>
        </action>
    </package>
</struts>
  • Just like our struts.xml file, we also have the web.xml file (inside the Web – INF folder) which is responsible mainly for session configuration, start-up file configuration, server settings etc.
  • In web.xml, change the welcome file (inside welcome file list tag) to our index.jsp. After corrections, web.xml should look like:
<?xml version = "1.0" encoding = "UTF-8"?>
<web-app version = "3.1" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern> /* </url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>jsp/first.jsp</welcome-file>
    </welcome-file-list>
</web-app>
  • Running the application will show following page:

Figure : first successful run of application

 

                                    Figure : first successful run of application

  • Enter two numbers in the text fields and click on the button. The activity page is executed, which results in success if emir numbers are found and from the <result> tag, the value for success is executed. That is, the result page (next.jsp) is displayed.  

Figure : enter two numbers for list of emirp numbers

                                    Figure : enter two numbers for list of emirp numbers

Figure : Result page (next.jsp) – displays all the emirp numbers.

                           Figure : Result page (next.jsp) – displays all the emirp numbers.

Like us on Facebook