Sunday, May 9, 2010

Web Servers and Application Servers

Starting with Web Programming, usually most programmers are acquainted with some servers like IIS from Microsoft or Apache HTTP Server. Those two servers are classified as Web Servers (Wiki) while some of the other well known servers like Apache Tomcat, Red Hat JBoss, Oracle Glassfish, IBM WebSphere, Oracle WebLogic, Apple WebObjects,... are always mentioned as Application Servers (Wiki). So what is the main difference between Web Servers and Application Servers?

1) The Web Server

A Web server handles the HTTP protocol. When the Web server receives an HTTP request, it responds with an HTTP response, such as sending back an HTML page. To process a request, a Web server may respond with a static HTML page or image, send a redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, or some other server-side technology. Whatever their purpose, such server-side programs generate a response, most often in HTML, for viewing in a Web browser.
Understand that a Web server's delegation model is fairly simple. When a request comes into the Web server, the Web server simply passes the request to the program best able to handle it. The Web server doesn't provide any functionality beyond simply providing an environment in which the server-side program can execute and pass back the generated responses. The server-side program usually provides for itself such functions as transaction processing, database connectivity, and messaging.
While a Web server may not itself support transactions or database connection pooling, it may employ various strategies for fault tolerance and scalability such as load balancing, caching, and clustering—features oftentimes erroneously assigned as features reserved only for application servers.

2) The Application Server

As for the application server, according to our definition, an application server exposes business logic to client applications through various protocols, possibly including HTTP. While a Web server mainly deals with sending HTML for display in a Web browser, an application server provides access to business logic for use by client application programs. The application program can use this logic just as it would call a method on an object (or a function in the procedural world).
Such application server clients can include GUIs (graphical user interface) running on a PC, a Web server, or even other application servers. The information traveling back and forth between an application server and its client is not restricted to simple display markup. Instead, the information is program logic. Since the logic takes the form of data and method calls and not static HTML, the client can employ the exposed business logic however it wants.
In most cases, the server exposes this business logic through a component API, such as the EJB (Enterprise JavaBean) component model found on J2EE (Java 2 Platform, Enterprise Edition) application servers. Moreover, the application server manages its own resources. Such gate-keeping duties include security, transaction processing, resource pooling, and messaging. Like a Web server, an application server may also employ various scalability and fault-tolerance techniques.

3) Examples

As an example, consider an online store that provides real-time pricing and availability information. Most likely, the site will provide a form with which you can choose a product. When you submit your query, the site performs a lookup and returns the results embedded within an HTML page. The site may implement this functionality in numerous ways. I'll show you one scenario that doesn't use an application server and another that does. Seeing how these scenarios differ will help you to see the application server's function.

Scenario 1: Web server without an application server

In the first scenario, a Web server alone provides the online store's functionality. The Web server takes your request, then passes it to a server-side program able to handle the request. The server-side program looks up the pricing information from a database or a flat file. Once retrieved, the server-side program uses the information to formulate the HTML response, then the Web server sends it back to your Web browser.
To summarize, a Web server simply processes HTTP requests by responding with HTML pages.

Scenario 2: Web server with an application server

Scenario 2 resembles Scenario 1 in that the Web server still delegates the response generation to a script. However, you can now put the business logic for the pricing lookup onto an application server. With that change, instead of the script knowing how to look up the data and formulate a response, the script can simply call the application server's lookup service. The script can then use the service's result when the script generates its HTML response.
In this scenario, the application server serves the business logic for looking up a product's pricing information. That functionality doesn't say anything about display or how the client must use the information. Instead, the client and application server send data back and forth. When a client calls the application server's lookup service, the service simply looks up the information and returns it to the client.
By separating the pricing logic from the HTML response-generating code, the pricing logic becomes far more reusable between applications. A second client, such as a cash register, could also call the same service as a clerk checks out a customer. In contrast, in Scenario 1 the pricing lookup service is not reusable because the information is embedded within the HTML page. To summarize, in Scenario 2's model, the Web server handles HTTP requests by replying with an HTML page while the application server serves application logic by processing pricing and availability requests.

4) Caveats

Recently, XML Web services have blurred the line between application servers and Web servers. By passing an XML payload to a Web server, the Web server can now process the data and respond much as application servers have in the past.
Additionally, most application servers also contain a Web server, meaning you can consider a Web server a subset of an application server. While application servers contain Web server functionality, developers rarely deploy application servers in that capacity. Instead, when needed, they often deploy standalone Web servers in tandem with application servers. Such a separation of functionality aids performance (simple Web requests won't impact application server performance), deployment configuration (dedicated Web servers, clustering, and so on), and allows for best-of-breed product selection.

Wednesday, May 5, 2010

Log4j vs java.util.logging

While Log4j and JUL are almost conceptually identical, they do differ in terms of functionality. Their difference can be summarized as, "Whatever JUL can do, Log4j can also do - and more." They differ most in the areas of useful appender/handler implementations, useful formatter/layout implementations, and configuration flexibility.

Tuesday, May 4, 2010

Making WAR archive for the GWT applications using Eclipse/Ant builder

In some web server like Tomcat or Glassfish, it is convenient to have an archive war to publish as your web application. But making war in GWT applications is not that simple due to the numerous dependencies and rich library included.
The most challenging part of the problem is to write the Ant build.xml file. In order to solve this issue, I have used a small trick. GWT comes with a utility called webAppCreator which not only sets up the GWT project and creates some handy starter app code, but also provides a build.xml and JDT launch configuration file. The syntax is like below:
webAppCreator -out HelloGWT com.mycompany.mywebapp.HelloGWT
In order to generate the war archive, you just copy the build.xml into your project folder. I make changes accordingly in the following points:
1)arg value="com.mycompany.mywebapp.HelloGWT" => "" in both target gwtc and devmode
2)zip destfile=".war" basedir="war"
Another issues is whether you want to use Eclipse to make war file or use normal Ant builder.
If your choice is Eclipse, go to Run > External Tools > External Tools Configurations, and create a new Ant build, pick the build.xml for your project, your project as the base directory, then move to the Targets tab, and select the war target, deselecting the build target since war depends on build already. It's a good idea to name this Configuration "MyWebApp war" or similar. When the target is run, your code will be compiled into the war subdirectory, which will then be zipped into MyWebApp.war in the project directory.
In my case, I preferred to make war file on my own using Ant builder. I modified the build.xml as below


(remove target "war" and "clean")
Using command line to locate your project folder. Run "ant" => making war.

Monday, May 3, 2010

Interview @Helios Lab Pte. Ltd

Although it is quite a long time since I had a job interview, I personally satisfied with my performance disregarding the outcome. This interview in some way benefited more than the actual result. Some of the main knowledges I somehow have heard of or reading through before or after the interview:
1) SPRING Framework: facilitates Domain Driven Design, isolates Business Logics and Displays.
2) Inversion of Control, Aspect Oriented Programming, Design Patterns, Java Message Services, Java Management Extensions, all the important features of J2EE.
3) Google Web Toolkit and J2EE.
4) GlassFish Java Server.
 The senior developer also asked me some Java questions which I cannot answer. One question mentioned about the Gabbage Collection Mechanism in Java, and the other questions asked about some Collections in Java which are Thread Safe.

Saturday, May 1, 2010

Testing SyntaxHighlighter

// Comment
public class Testing {
   public Testing() {

   public void Method() {
      /* Another Comment
      on multiple lines */
      int x = 9;

Compile 1st C++ Program in Ubuntu

Having installed Ubuntu for quite a long time, and I finally have learnt how to program C++ in this Unix environment. 

I type my C++ program using gedit that is available under Ubuntu.
I tried this code first hand and it does not worked.

$ gcc -o  hw1 hw1.cpp

I found out the reason is that gcc is used for only C program files and not C++. The following command is actually worked out.

$ g++ -o  hw1 hw1.cpp

This -o basically used to suggest name for the executable file that g++ would create. In case if you typed something like the following:

$ g++ hw1.cpp

You would be having a a.out in the same directory as the source C file. This is the default name of the executable that g++ creates. This would create problems when you compile many programs in one directory. So you override this with the -o option followed by the name of the executable.

Would create an executable by the name hw1 for your source code named hw1.cpp.

Running the executable that you created is as simple as typing the following at the prompt.

$ ./hw1
$ ./a.out

Or whatever you named your executable.