Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Introduction To Java Servlets

Rated 3.68 (Ratings: 3)

Want more?

 

Daniel Cody

Member info

User since: 14 Dec 1998

Articles written: 146

In the past couple years, the web has shifted from being a fun loving, care-free place to showcase your Pamela Lee collection to a serious, down to earth place for major companies to do business. What helped cause the shift was the ability to make interactive sites that served up relevant information to the user. This in no small part was due to the use of CGI scripts. Tried and true, CGI is the force behind almost every search engine, bulletin board, and guestbook on the web.

As a Java programmer, I didn't want to have to learn a language like perl to make an email form or to search my site. Sure, I could write another applet, but what if the user's browser doesn't support Java, or they have it turned off altogether? CGI scripts are notoriously slow (you have to start a new process for every CGI request) and aren't scaleable (for the same reason). After a bit of poking around, I found the answer to all these needs : Java Servlets.

What are they?

Basically, servlets can be thought of as server side Java. Servlets run in the webserver just like an applet runs in the browser, but the great thing is that only YOU need to have Java support, all they need is a browser window. Just like running an applet an the browser, you need a Java Virtual Machine(JVM) to run a servlet. Also, your webserver has to support the Java Servlet API. This API, developed by JavaSoft, defines how the servlet communicates with the server. The webserver I use and like to recommend to my friends and clients is Suns Java Webserver because it supports the servlet API and comes with its own JVM.

So, are you left out of the servlet experience if you don't have the Java Webserver? Of course not. There are 3rd party server add-ons them that are available for almost every webserver. A good list can be found at http://jeeves.javasoft.com.

So now that you understand how to make them work on your server, let's show a simple servlet that will get a user's name and email address and prints it back to a page.

import java.io.*;

import java.util.*;

import javax.servlet.*; /* call the servlet methods here*/

import javax.servlet.http.*;

public class SimpleFormServlet extends HttpServlet {

public void service(HttpServletRequest req, HttpServletResponse res)

throws IOException

{

Enumeration keys;

/*the parameters we are getting from the <input> tag in our HTML file*/

String name = req.getParameter("name");

String email = req.getParameter("email");

/*Start building our generated HTML page here*/

res.setContentType("text/html");

ServletOutputStream out = res.getOutputStream();

out.println("<HEAD><TITLE> Our Output </TITLE></HEAD><BODY>");

out.println("<h1> The name and email from our servlet : </h1>");

/*Print out our param's here..*/

out.println("<P> <b>Name was:</b> " +name);

out.println("<P> <b>Email was:</B> " +email);

out.println("</BODY></HTML>");

}

public String getServletInfo() {

return "A simple servlet";

}

}

The first thing you may notice while looking at the source is the import javax.servlet.*; line. This is simply importing methods (such as how to post and retrieve information to and from the webserver) that we use in servlets from the Java Servlet Developers Kit(JSDK). If you have any version of the JDK 1.1.x (such as 1.1.4) you will need to download the new version of the JDK that has the JSDK included within it. Two for the price of one :) As for what we needed in the HTML page, it is fairly minimal :

<FORM ACTION="http://yourserver/servlet/SimpleFormServlet" METHOD="POST">

<P>

Your Name: <INPUT NAME="name" TYPE="text" SIZE="30" MAXLENGTH="90">

<P>

Your Email Address: <INPUT NAME="email" TYPE="text" SIZE="30" MAXLENGTH="90">

<P>

<INPUT NAME="submit" TYPE="submit" VALUE="Submit to Mail Servlet"><BR>

<INPUT NAME="name" TYPE="reset" VALUE="Clear this Form"><BR>

</FORM>

Looking at the source of the servlet, you can see where we ask for the 'name' and 'email' parameters, and then how we put them back into the HTML page that we generate. You could add as many parameters as you wanted to get from the user, and put them back into the generated HTML code in hundreds of different combinations.

Sure, this is an extremely simple example of a servlet, but it shows how basic data is passed between the HTML form and the Java servlet. More complex uses for servlets are showing up all the time. In my next article on servlets, we'll get in to some more complex uses for them like e-mailing form information to you, using them for counters, and uploading files to the webserver.

As an alternative to CGI based scripts, servlets are a scaleable solution with the potential to help create dynamic, interactive, and informative sites. So try some out, re-write them, and have fun with an awesome alternative to CGI!

Do you agree or disagree? Have a comment on servlets? Let me know or post a comment below.

Dan lives a quiet life in the bustling city of Milwaukee, WI. Although he founded what would become evolt.org in 1998, he's since moved on to other projects and is now the owner of Progressive Networks, a Zimbra hosting company based in Milwaukee.

His personal site can be found at http://dancody.org/

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.