Monday, May 16, 2011

Introduction to Struts 2 configuration on Tomcat

The configuration of Struts 2 is different from the way Struts 1 configuration. This blog demonstrates  the steps required to configure Struts 2 on Tomcat.
These are the steps:
(1)    Install Sun JDK 1.6
(2)    Install Apache Tomcat 6.0.32
(3)    Download struts-2.2.1.1-apps.zip from struts web site.
(4)    This contains struts2-blank.war file. This is an empty war file containing struts2 configuration. We will use this to configure the struts2 web application.
(5)    Copy struts2-blank.war file to the webapps folder of the Tomcat server.
(6)    Start the Tomcat server.
(7)    Type the URL on the web browser - http://localhost:8080/struts2-blank/example/HelloWorld.jsp
(8)    You should see the following screenshot of the application, which means that the application is working fine.


Now, let’s look at various configuration elements that goes with configuring struts2 application.



Web.xml
The web.xml file is used to configure the FilterDispatcher class that handles all requests to struts2 framework. FilterDispatcher class is configured as a filter and maps all the application requests to Struts2 framework.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>



Struts.xml
The mappings for MVC are declared in the struts.xml file. This file is located in the folder – WEB-INF/src/java

        <action name="HelloWorld" class="example.HelloWorld">
            <result>/example/HelloWorld.jsp</result>
        </action>   

   <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>



HelloWorld.jsp
The following code snippet shows the use of struts tag library to create the view.

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title><s:text name="HelloWorld.message"/></title>
</head>

<body>
<h2><s:property value="message"/></h2>

<h3>Languages</h3>
<ul>
    <li>
        <s:url id="url" action="HelloWorld">
            <s:param name="request_locale">en</s:param>
        </s:url>
        <s:a href="%{url}">English</s:a>
    </li>
    <li>
        <s:url id="url" action="HelloWorld">
            <s:param name="request_locale">es</s:param>
        </s:url>
        <s:a href="%{url}">Espanol</s:a>
    </li>
</ul>

</body>
</html>



HelloWorld.java
The execute method is called to process the request. This method generally call the business POJO classes and fetches / updates data to database. Once done, this call the view to be rendered based on the return string from the execute method.

public class HelloWorld extends ExampleSupport {

    public String execute() throws Exception {
        setMessage(getText(MESSAGE));
        return SUCCESS;
    }

    /**
     * Provide default valuie for Message property.
     */
    public static final String MESSAGE = "HelloWorld.message";

    /**
     * Field for Message property.
     */
    private String message;

    /**
     * Return Message property.
     *
     * @return Message property
     */
    public String getMessage() {
        return message;
    }

    /**
     * Set Message property.
     *
     * @param message Text to display on HelloWorld page.
     */
    public void setMessage(String message) {
        this.message = message;
    }
}


The view to be rendered is again the same page – HelloWorld.jsp, the same page is called with different message. The following screenshot shows the HelloWorld example.

The above description demonstrates how easy it is to setup Struts 2 MVC web framework. It also demonstrates that it is quite different from configuring Struts 1 framework.

Wednesday, May 4, 2011

Setting up Amazon Elastic Load Balancer

Amazon ELB is a very convenient and easy to setup as a component in the Enterprise Web Applications to balance the incoming web request load to available pool of instances. In addition to this, Amazon ELB also detects the health of instances in the pool and can change the traffic pattern of requests to route them to only healthy instances.
ELB also has facility to enable stickiness in the sessions so that request from the same client is load balanced by the same instance in the pool. An additional use of ELB is to map requests from port HTTP 80 to HTTP port 8080 of the application servers or web servers running on the EC2 instance.

How to setup ELB?
Step 1: Click on the create new load balancer button:

Step 2:
Select the application path on the application server to which the ELB will send ping requests to check the health of the instances.

Step 3 : Select the instances to be added to the pool of instances available for load balancing:

Step 4: Confirm the details and click the create button.
This will create the ELB for the pool of EC2 instances and now you can send requests to the ELB on HTTP port 80 from the address bar of your web browser, ELB will forward the request to the ELB and the response will be rendered to the browser.

For more details of Amazon Elastic Load Balancer, visit here - http://aws.amazon.com/elasticloadbalancing/




 

Tuesday, May 3, 2011

Using Amazon CloudWatch Webservice

Using Amazon Cloudwatch
Amazon AWS has launched Amazon Cloudwatch sometime back, and recently I used it for one of the projects I am working on.
Amazon Cloudwatch is a web service that provides monitoring and alerts for Amazon AWS resources.  It also provides operational metrics on Amazon EC2 resources like elastic load balancers, instances, etc. The operational metrics includes parameters like CPU Utilization, Disk read/writes, and network traffic. Please note that there is a charge to use this service.

It is very easy to setup Amazon Cloud Watch web service. Primarily, I found two uses of CloudWatch as follows:
·         Trigger email generation when a certain parameter exceeds certain value
·         Auto-scale up or down the instances when a certain parameter exceeds certain value.
The parameters mentioned in the above 2 points can be any operational metrics.

How to setup?
Here’s how to setup email generation using Amazon Cloudwatch:
Step 1:
Select parameter, in this case I have selected CPU Utilization:
Step 2:
Next step is to setup alarm and email address:
That’s it !!
The alarm is setup using Amazon CloudWatch web service. When the CPU utilization of the selected instance will cross the threshold of 5 min, an email will be sent to the email id setup.

For more information you can look at the link - http://aws.amazon.com/cloudwatch/