Skip to main content

Dynamic Web Content - From HTML to Servlets and beyond (Part II)

Towards Servlets 

From the earlier post I've discussed about the techniques of dynamic web content loading. This post is about servlets, java servlet pages (jsps) and java.
Java Servlets are an efficient and powerful solution for creating dynamic content for the Web. Over the past few years Servlets have become the fundamental building block of mainstream server-side Java. The power behind Servlets comes from the use of Java as a platform and from interaction with a Servlet container. The Java platform provides a Servlet developer with a robust API, object-orientated programming, platform neutrality, strict types, garbage collection, and all the security features of the JVM. Complimenting this, a Servlet container provides life cycle management, a single process to share and manage application-wide resources, and interaction with a Web server. Together this functionality makes Servlets a desirable technology for server-side Java developers. 

Servlets are always part of a larger project called a Web Application. A Web Application is a complete collection of resources for a Web site. Nothing stops a Web Application from consisting of zero, one, or multiple Servlets, but a Servlet container manages Servlets on a per Web Application basis. Web Applications and the configuration files for them are specified by the Servlet specification. 


When a browser invokes a servlet, this is what happens.

  • Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
  • Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
  • Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
  • Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
  • Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.

    This is what the servlet actually does.

     

The servlet container init() the servlet only once and for subsequent requests different threads are spawned to call the service() method of the servlet. Servlet has a mapping in a file called web descriptor or web.xml. When client requests a servlet this mapping is used to find the corresponding servlet class.

There is also something called jsps. what is the difference between a jsp and a servlet?


A very basic difference:
  • Servlet is [html] in java
  • JSP is java in [html]
Things will be much clearer by looking at some code examples. Here goes a servlet example.

And here's a jsp example.



However a jsp is actually a servlet!!! It is comprised of two very specific syntax styles: scriptlet and markup – a scriptlet simply being blocks of Java code that are mixed with markup, which is the standard HTML or XML. This is what happens when a jsp is invoked by a client.




Other differences are:
  • JSP is a webpage scripting language that can generate dynamic content while Servlets are Java programs that are already compiled which also creates dynamic web content
  • Servlets run faster compared to JSP
  • JSP can be compiled into Java Servlets
  • It’s easier to code in JSP than in Java Servlets
  • In MVC, jsp act as a view and servlet act as a controller.
  • JSP are generally preferred when there is not much processing of data required. But servlets are best for use when there is more processing and manipulation involved.
  • The advantage of JSP programming over servlets is that we can build custom tags which can directly call Java beans. There is no such facility in servlets.
  • We can achieve functionality of JSP at client side by running JavaScript at client side. There are no such methods for servlets.

Servlet Advantage

1.     Servlets provide a way to generate dynamic documents that is both easier to write and faster to run.
2.     provide all the powerfull features of JAVA, such as Exception handling and garbage collection.
3.     Servlet enables easy portability across Web Servers.
4.     Servlet can communicate with different servlet and servers.
5.     Since all web applications are stateless protocol, servlet uses its own API to maintain  session

Servlet Disadvantage

1.     Designing in servlet is difficult and slows down the application.
2.     Writing complex business logic makes the application difficult to understand.
3.     You need a Java Runtime Environment on the server to run servlets. CGI is a completely language independent protocol, so you can write CGIs in whatever languages you have available (including Java if you want to).

Comments

Popular posts from this blog

Detaching a process from terminal - exec(), system(), setsid() and nohup

Linux processes are created by fork() and exec(). The very first process of POSIX systems is init and subsequent processes are derived from the init as parent. These subsequent processes are child processes. During forking the parent process copies itself - with all I/O, address space, stack, everything. The only thing that is different is the process ID. The parent and child will have 2 different process IDs. The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl("/bin/sh", "sh", "-c", command, (char *) 0); system() returns after the command has been completed. system() executes a command specified in command by calling /bin/sh -c command , and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.  system() calls are often made within programs to execut

Vaadin vs GWT

From Chapter 1 of book of Vaadin I quote the following. Vaadin Framework is a Java web application development framework that is designed to make creation and maintenance of high quality web-based user interfaces easy. Vaadin supports two different programming models: server-side and client-side . The server-driven programming model is the more powerful one . It lets you forget the web and program user interfaces much like you would program a desktop application with conventional Java toolkits such as AWT, Swing, or SWT. But easier. While traditional web programming is a fun way to spend your time learning new web technologies, you probably want to be productive and concentrate on the application logic. The server-side Vaadin framework takes care of managing the user interface in the browser and the AJAX communications between the browser and the server . With the Vaadin approach, you do not need to learn and deal directly with browser technologies, such as HTML or JavaScript.

C++ Callbacks using function pointers vs boost bind +boost function

In C, the most common uses of callbacks are as parameters to library functions like qsort , and as callbacks for Windows functions, etc. For example you might have a library that provides some sorting functions but you want to allow the library user to provide his own sorting function. Since the arguments and the return values do not change depending on the sorting algorithm, this can be facilitated in a convenient manner using function callbacks. Callbacks are also used as event listeners. onMouseClick(), onTerminalInput(), onData(), onConnectionStatus(), onRead() are probably some examples you've already seen in different libraries. The libraries have these callback functions and the users of the library are supposed to implement them. The library has a function pointer to these functions and calls them on their event loop which will invoke the code of the inherited classes of the library user. The implementation of function pointers is simple: they are just "code p