Servlets Tutorial for Beginners

Java_logo_and_wordmarklargeAs a Java programmer, there are many technologies you should become familiar with above and beyond standard Java programming. As corporations continue to move to a more technology driven business model, it’s your responsibility as an IT professional to provide them with solutions for any business needs that arise now or in the future.

In this article, you will learn about Java Servlets, why they are important to today’s businesses, and some helpful resources you can use to learn more about adding Java Servlets to your existing programming repertoire.  It is assumed you have some basic Java programming experience.  If not, you should start with Java Fundamentals I & II.

What are Java Servlets?

If you have spent any time in the IT profession, you are probably familiar with Common Gateway Interface (CGI). If you aren’t familiar with this term, CGI basically refers to the API used by Web servers to interface directly with web applications. CGI has taken on many forms over the years and been accomplished in various programming languages.

Java Servlets are Java’s answer to CGI programming. A servlet is a program that is run on a Web server and can build dynamic web pages. There are quite a few reasons why a business would want to create dynamic web pages “on-the-fly” including:

  • The webpage can be based on data submitted by the user. A good example of CGI is the results page obtained from a search engine. Programs that process sales orders for e-commerce websites also use CGI to generate this dynamic content. Remember that it does not have to be done using Java Servlets, but it is one of the better options as you will see below.

  • Businesses also rely on CGI when the data on a website changes frequently. News and weather websites typically rely on CGI to keep content fresh without constant web developer attention.

  • If the website uses information gathered from a corporate database, CGI is often relied upon to generate the content. E-commerce websites that list current prices and stock levels use CGI to fetch this content on demand from the company’s backend infrastructure.

Why Use Servlets over Other CGI Options?

Since you already have experience programming in Java, you realize how versatile this platform is for both standalone and web-based applications. It only makes sense to use Java technology to create CGI-based Web services.

As a general rule, Java Servlets are more efficient, more powerful, easier to use, and less expensive than traditional CGI alternatives. Servlet developers also tend to get paid better than Perl programmers. Better salary opportunities aside for a moment, you should be using servlets for the following reasons:

Efficiency – In traditional CGI, each HTTP request starts a new CGI process. Even if the CGI code is perfectly implemented, there is often a significant amount of overhead just in starting the CGI process at its execution time. When you use servlets, the JVM stays loaded in memory and each request is handled by a Java thread (not an entire OS process). As an example, if there are X number of simultaneous CGI requests in a traditional CGI model, that means that the code for the CGI program is loaded into memory X number of times. This becomes extremely taxing on the Web server. In a servlet environment, however, there are X threads but only one copy of the servlet class running at any given time. The result is improved efficiency and scalability across multiple platforms.

Convenience – Assuming you are already experienced as a Java programmer, it makes no sense to learn a new language like Perl just to perform CGI functions; especially when you have such a powerful solution right at your fingertips. In addition, servlets have an extensive infrastructure for many HTML related tasks which makes development much easier.

If you need to brush up on your HTML skills to incorporate servlets properly, check out the HTML5 Fundamentals course.

Power – Unfortunately, traditional CGI scripts leave much to be desired. For example, regular CGI programs cannot talk directly to Web servers meaning that an entire interface needs to be created. Servlets can communicate directly with Web servers; simplifying operations that need direct access to backend data stores. Servlets are also unique because they can share data with other servlets (think database connection pools) and maintain information between requests which makes session tracking extremely simple.

Portability – The portability of Java extends directly to servlets. In fact, just about every major Web server currently in use has support for Java Servlets either directly or via a plug-in.

Inexpensive – From a development standpoint, implementing servlets is much less expensive than other options that require custom coding to interface properly with Web servers. Java servlets are ready to go and can keep the cost to your business extremely low without sacrificing the benefits of dynamic content.

Difference between Servlets and Java Server Pages (JSP)

As you progress in the world of dynamic web content based in Java, you will likely come across Java Server Pages as well. JSP is another effective way to create dynamic web content using Java. The easiest way to explain the difference between the two is simply that a servlet is HTML in Java while JSP is Java in HTML.

Both techniques are effective and can be used independently, but there are a few differences. As a general rule, servlets run faster than JSP; however, JSP can be compiled into Java servlets.   You can learn more about the differences and when to use each technology in the Servlets & JSPs course.

As a Java web developer, you should be familiar with both technologies and how they can complement each other in a single implementation. Also remember that many functions of JSP can be achieved at the client side using JavaScript. This reduces the Web server load during peak usage periods and is often a technique relied upon for large businesses with consistently high traffic.  If you are unfamiliar with using scripting within HTML, Javascript Made Easy is a good place to learn how.

Although standalone Java applications may have reached their peak in popularity a few years ago, there is no question that dynamic web content built on Java technology is still in high demand by businesses around the world. If you are not currently using Java Servlets to create dynamic content, you are missing out on an exciting field with unlimited career opportunities.