Tuesday, November 21, 2006

Common features

Although Web server programs differ in detail, they all share some basic common features.

  1. HTTP responses to HTTP requests: every Web server program operates by accepting HTTP requests from the network, and providing an HTTP response to the requester. The HTTP response typically consists of an HTML document, but can also be a raw text file, an image, or some other type of document; if something bad is found in client request or while trying to serve the request, a Web server has to send an error response which may include some custom HTML or text messages to better explain the problem to end users.
  2. Logging: usually Web servers have also the capability of logging some detailed information, about client requests and server responses, to log files; this allows the Webmaster to collect statistics by running log analyzers on log files.

In practice many Web servers implement the following features too.

  1. Configurability of available features by configuration files or even by an external user interface.
  2. Authentication, optional authorization request (request of user name and password) before allowing access to some or all kind of resources.
  3. Handling of not only static content (file content recorded in server's filesystem(s)) but of dynamic content too by supporting one or more related interfaces (SSI, CGI, SCGI, FastCGI, PHP, ASP, ASP .NET, Server API such as NSAPI, ISAPI, etc.).
  4. Module support, in order to allow the extension of server capabilities by adding or modifying software modules which are linked to the server software or that are dynamically loaded (on demand) by the core server.
  5. HTTPS support (by SSL or TLS) in order to allow secure (encrypted) connections to the server on the standard port 443 instead of usual port 80.
  6. Content compression (i.e. by gzip encoding) to reduce the size of the responses (to lower bandwidth usage, etc.).
  7. Virtual Host to serve many web sites using one IP address.
  8. Large file support to be able to serve files whose size is greater than 2 GB on 32 bit OS.
  9. Bandwidth throttling to limit the speed of responses in order to not saturate the network and to be able to serve more clients.

Thursday, August 03, 2006

An Introduction to Java Server PagesT and ServletsT

What are Java Server Pages?

Java Server PagesT (JSP) is a server-side scripting technology. If you have worked with Microsoft's Active Server Pages (ASP), then you will find a few similarities between the two. The main difference is that while ASP is proprietary and cannot run on non-Microsoft servers, at least not without a plug-in such as ChiliASPT, JSP can be implemented by any vendor, hence the now impressive availablility of JSP and Servlet supporting engines, some freely available, and others at varying commercial prices.
Like ASP, you can use other scripting languages like VBScript and JavaScript. In JSP the default language is Java, consequently, unlike ASP, you have the full power of the Java API in your armory. If you are only beginning to explore Java, you can do so at a very basic level, and then as your proficiency increases you can write very complex server side applications with JSP, Servlets, and JavaBeans.
Unlike ASP, JSP is interpreted only once after the servlet has been compiled. What servlet, you might ask. A JSP is really a servlet. When a JSP page is requested for the very first time, the JSP engine takes all of the contents of the JSP and converts them into a servlet class and then compiles it into bytecode. If there has been no change to the JSP page since its first compilation, the JSP page will be interpreted only once. So, unlike ASP which interprets the code inside every ASP page every time it is called, JSP is interpreted only once. Consequently, JSP's are faster than ASP's.
The main attraction of JSP is that it enables the separation of code from the presentation part of a web page. This is achieved through the use of JavaBeans. Instead of intermingling Java code with HTML in one page, JavaBeans encapsulate the code.
If you are unfamiliar with JavaBeans (oops, here comes another tutorial!), we'll develop a very simple one later in this tutorial to demonstrate how it is used from a JSP. Briefly, a JavaBean contains setter and getter methods eg. setMyAge, getMyAge. These are properties of the JavaBean which can be set or retrieved through JSP Action tags, as shown below.

Which to use, JSP or Servlet?

When constructing the architecture of a JSP/Servlet based application using the MVC (Model View Controller) pattern, then this technology would fit into the MVC framework as follows:
Model: JavaBeans - their main function is to encapsulate the processing of data.
View: JSP - used in the presentation layer of the application; this is the what the end-user sees and interacts with. Any Java code in the JSP should only be related to passing requests to the servlet and in retrieving data from a JavaBean.
Controller: Servlet - their capabilities are best utilized in routing user requests, and in managing the application's workflow. Some data pre-processing can also occur in this part of the application. The servlet should not be used to print out any html.
As we work through the upcoming example, the MVC roles discussed above will become clearer.