protected void doGet(HttpServletRequest request, 
                     HttpServletResponse response) 
                     throws ServletException, IOException 
{	
   int lngCookieSet    = 0;
   String strCookieVal = "";
   String cookieName   = "Name";
	
   Cookie[] cookies = request.getCookies();
		
   if(cookies != null && cookies.length > 0)
   {
      for(int i=0;i<cookies.length;i++)
      {
        if(cookieName.equals(cookies[i].getName()))
        {
          lngCookieSet = 1;
          strCookieVal = cookies[i].getValue();
        }
      }
   }	
		
   if(lngCookieSet == 1)
   {
     PrintWriter prw = response.getWriter();	
     prw.print(strCookieVal);
   }
   else
   {
     Cookie cook = new Cookie("Name", "Mugil");
     cook.setMaxAge(24*60*60*365);//1 Year
     response.addCookie(cook);	
   }
}

Notes
1. check cookies != null otherwise the compiler will generate null pointer exception
2. The new Cookie constructor will take Cookie Name and Value as Parameter.
3. The addCookie Will add cookie to response Header to set cookie on Client Side

Running Iterator Through Bean Stored in Map List

public class Iterator extends ActionSupport
{
   Map nmap = new HashMap<String, List<Person>>();
	
   public String execute()
   {
      Person objPerson    = new Person();

      objPerson.userName  = "Name1";
      objPerson.age 	    = 25;
      List<Person> myList = new ArrayList<Person>();
      myList.add(objPerson);
		
      nmap.put("Mugil", myList);
	
      objPerson.userName   = "Name2";
      objPerson.age 	     = 26;
      List<Person> myList2 = new ArrayList<Person>();
      myList2.add(objPerson);
		
      nmap.put("Mani", myList2);
		
      objPerson.userName    = "Name3";
      objPerson.age 	      = 27;
      List<Person> myList3  = new ArrayList<Person>();
      myList3.add(objPerson);
		
      nmap.put("Vinu", myList3);
		
      setNmap(nmap);
		
      return SUCCESS;
   }
	
   public Map getNmap() 
   {
     return nmap;
   }

   public void setNmap(Map nmap) 
   {
     this.nmap = nmap;
   }
}

Person Bean Code

class Person
{
    String userName;
    int age;

    public int getAge() {
      return age;
    }

    public void setAge(int age) {
      this.age = age;
    }

    public String getUserName() {
      return userName;
    }
	
    public void setUserName(String userName) {
      this.userName = userName;
    }
}

JSP Page Using Struts2 Tag

<table>
<s:iterator value="nmap">
     <s:iterator var="Person" value="value">
      <tr>
     	<td>
    	  <s:property value="key" />	
    	</td>
        <td>
      	  <s:property value="age" />
    	</td>
      </tr>
      </s:iterator>
</s:iterator>
</table>

Once a Time I had a Scenario where I should display a Page with List of employees movement from Last Friday to this week Saturday if Saturday is over(you are in Sunday).If you are in any other day other than Saturday(Mon..Fri) then you should choose date of Last before Saturday to Friday.

The above would be made with the following

Image 1
Image 1

As you can see in the above diagram I have added two calendar.

In First I should Get Date between 15 to 21 since today date is this 28 and Saturday is not over
In Second I should Get Date between 22 to 28 since today date is this 30 nd Saturday is over

  java.util.Date dt    = new java.util.Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  String currentTime   = sdf.format(dt);
  Calendar cal         = Calendar.getInstance();

  int daysBackToSat = cal.get(Calendar.DAY_OF_WEEK);
  
  //Checks if this weeks friday is Over.in My Case 28 falls on Friday.
  //Returns 6 the below case is True so adds 13 to get before Saturday
  if(daysBackToSat < 7)
   daysBackToSat += 7; //6+7
 
  //Subtracting 13 Days for Last Saturday 
  //Since this Saturday is not Over
  cal.add(Calendar.DATE, daysBackToSat * -1);
  System.out.println("Start Date" + sdf.format(cal.getTime()));

  cal.add(Calendar.DATE, 6);
  System.out.println("End Date" + sdf.format(cal.getTime()));

Output Code
2013-06-15
2013-06-21

Action Class Code

public class DropDown extends ActionSupport{
    private List arrUserDetails = new ArrayList<Users>();

    public DropDown()
    {
        Users objUsers = new Users();
        objUsers.setUserId("101");
        objUsers.setUserName("User1");

        arrUserDetails.add(objUsers);

        objUsers.setUserId("102");
        objUsers.setUserName("User2");

        arrUserDetails.add(objUsers);

        objUsers.setUserId("103");
        objUsers.setUserName("User3");

        arrUserDetails.add(objUsers);
    }

    public String execute(){
        return SUCCESS;
    }

    public List getArrUserDetails() {
        return arrUserDetails;
    }


    public void setArrUserDetails(List arrUserDetails) {
        this.arrUserDetails = arrUserDetails;
    }
}

Users Bean Class

 public class Users
{
    private String userId;
    private String userName;

    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

JSP Page Struts2 Tag

 <s:select label="Departments" list="departmentList" listKey="deptId" listValue="deptName" name="department"/>

Below I have a List of Time to be added together

  List<String> arrTime = new ArrayList<String>();
  int hour      = 0;
  int minutes   = 0;
  int seconds   = 0;
  String Nmin   = null;
  String Nsec   = null;
  String result = null;

  arrTime.add("09:05:25");
  arrTime.add("09:30:15");
  arrTime.add("10:15:01");
  arrTime.add("08:19:49");
  arrTime.add("09:17:40");

  for (Iterator itr = arrTime.iterator(); itr.hasNext();) 
  {
    String Time = (String) itr.next();

    if (Time != null) 
    {
      String[] rowtime = Time.split(":");
      hour            += Integer.parseInt(rowtime[0]);
      minutes         += Integer.parseInt(rowtime[1]);
      seconds         += Integer.parseInt(rowtime[2]);
    }
  }

  hour    += minutes/60;
  minutes += seconds/60;

  minutes %= 60;
  seconds %= 60;

  if (minutes < 10)
   Nmin = "0" + String.valueOf(minutes);
  else
   Nmin = String.valueOf(minutes);
	 
  if (seconds < 10)
   Nsec = "0" + String.valueOf(seconds);
  else
   Nsec = String.valueOf(seconds);

  result = hour + ":" + Nmin + ":" + Nsec;
  
  System.out.println(result);

Output
46:28:10