Friday, April 27, 2012

Struts 1.X Basic Example .Introducing ACTION class

                                                     STRUTS BASIC EXAMPLE

This is the first Example in developing a web application using STRUTS framework.

Objective: Struts basic example .
                     
User Enters enters name as input and the same is to be produced as Output .

Prerequisites : Java 1.6 , Apache Server 6.0 ,Struts Framework

Things you need to aware of :   ACTION class
Application Structure  :
                                           
 Project Name      :            BasicStruts




index.jsp        
----------------
// This is the welcome page loaded on browser when the application is executed


<form action="/BasicStruts/login.do">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>

Here When index.jsp is submitted to server then  request is handled by specific action class  which is referred by  " /BasicStruts/login.do ".
So the path   /login.do    is referred in    struts-config.xml  .

WelcomeStruts.jsp           
------------------------------
// This jsp page presents the output to the user as specified by action class. 

  <%String username= (String) session.getAttribute("username"); %>
Hello <%= username%>..... Welcome to Struts ..


web.xml
-----------------

<web-app>
 <servlet>
         <servlet-name>action</servlet-name>
           <servlet-class>org.apache.struts.action.ActionServlet </servlet-class>
 </servlet>
 <servlet-mapping>
       <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
      </servlet-mapping>
 <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
</web-app>

web.xml is configured such that every request will come to same servlet   org.apache.struts.action.ActionServlet  . 
ActionServlet  serves as FrontController .  
ActionServlet   routes all the requests to the  RequestProcessors


struts-config.xml
--------------------------

<struts-config>
 <action-mappings>
           <action path="/login" type="org.kmr.struts.MyAction ">
           < forward name="showname" path="/WelcomeStruts.jsp"/>
           </action>
 </action-mappings>
</struts-config>


/login------> It is  the request URL as specified in the jsp file index.jsp
org.kmr.struts.MyAction  ----> It is the Action Class which handles any reuest of the form /login.do

Action Class  ( MyAction.java )
--------------------------------------------------

package org.kmr.struts;
import  org.apache .struts.action.*;
import javax.servlet.http.*;

public class MyAction extends Action
{
 public ActionForward execute( ActionMapping am, ActionForm af, HttpServletRequest req, HttpServletResponse res)  throws Exception
 {

         String name=req.getParameter("name");
          HttpSession session= req.getSession();
          session.setAttribute("username",name);
          return am.findForward("showname");

 }
}

When the  line  return am.findForward("showname");  is encounterd then  struts-config.xml   is referred 
 at the line   < forward name="showname" path="/WelcomeStruts.jsp"/> ,  so WelcomeStruts.jsp is sent as a response to the browser .

Tuesday, April 3, 2012

Program to print 'n' twin Prime numbers


import java.util.Scanner;
public class TwinPrime {
   
    public static void main(String a[])
    {

         System.out.println("This program prints 1st n twin prime nummbers");
        Scanner sc= new Scanner(System.in);
        int input = sc.nextInt();
        int i=3,n=35;
        boolean k,task=true;
             
            int count =0;
             while(task)
              {
                     if((isPrime(i)) & ( isPrime(i+2)))
                     {  
                         count++;
                         System.out.println("  "+(i-2)+"  "+i );
                         if(count==input ) { task = false; }
                     }
                     i+=2;
              }
            

 System.out.println("Total no of twin prime Numbers:-"+count );

   
    }
    public static boolean isPrime(int n) {

    //check if n is a multiple of 2
    if (n%2==0) return false;

    //if not, then just check the odds
    for(int i=3;i*i<=n;i+=2) {
        if(n%i==0)
            return false;
    }
    return true;
}

}