[Português]
 [Home]
 [Back]
 [Help]

Ricardo's Home Page

Java Servlet HelloWorld


 Java
 Links
 Programs
 Info
 Guestbook

The Hello World Servlet should reply to your browser the message "Hello World" each time it is run.
Ok, this is not the most useful servlet one can write, but I'll try to use it to explain the basis of a Java servlet.
Please take a look at the code:


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet
{
   public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException
   {
      ServletOutputStream out = response.getOutputStream();
      out.println("Hello World !!!");
   }
}

You can take a look at the Hello World Application Example to see diferences between the two.

The first three lines begin with the import statement.
Java language was first built with expandability in mind. For that reason, only a small part of the language is taken for granted. All the rest, including code written by your self, most of the SDK or any other code provided by other sources, must be explicitly imported.
By importing a class or a package, we are telling the compiler where to find the definition of some element we are using ahead in our code.
For example, in line 1 we wrote import java.io.IOException; witch tells the compiler that the definition of the class IOException (that we are going to need in line 8) is defined in the package java.io.
This is all the compiler needs to check the program sintax and link all the peaces of our program together.
For now, all we need to know is that any Servlet must have at least those three impots at the begining of the code.

In line 5 we define our class.
HelloWorldServlet is the name we choose for our class.
We are defining it as a public class, that is, a class that any one can see, including our Java enabled web server.
Finally, we have the extends HttpServlet statement.
This means that our HelloWorldServlet will be a sub-class of HttpServlet.
HttpServlet is a class defined in the JSWDK and is known by the web server.
Any Servlet must be a subclass of HttpServlet so that the web server can know how to talk with it.
Been a subclass, means all the methods and properties of the parent class are inherited by the children class, i.e., we get almouts all the work done.

As we seen in the Hello World Application Example we need a pair of curly brakets delimiting our class body.
They are present in lines 6 and 13.

This pair of matching curly brackets define the begining and end of our class code.

At line 7 and 8, there is a very long statement.
Please notice that it takes two lines only for readability.
In reality you could write it in one line or split it through several lines and the compiler would accept it.
So, let see what we have.
We start our statement with the public keyword.
This tels the compiler this method shoul be seen by the outside world, that is, our java enabled web server.
This is necessary because, when we try to run the servlet the JSWDK will try to access the doGet method, so it should be accessible to the outside (public).
The void statement tells the compiler that the method is not returning any value to the calling system.
Now we have our method name doGet.
For our servlet to work we must use this same name.
As we seen before, the class we are building is a sub-class of HttpServlet.
One of the methods defined in HttpServlet that are called when a browser ask the web server to run a servlet is the method doGet.
For the servlet to do what we want we only need to rewrite the doGet method acordingly.
After the name of the servlet we can see a pair of matching parenthesis surrounding a couple of parameters the method would receive.
The first, HttpServletRequest request, is an object of the class HttpServletRequest and is the way the web server passes information abaout the browser request to our servlet.
As we don't need this for now, lets skip to the second parameter.
The second parameter, HttpServletResponse response is an object of class HttpServletResponse and contains the response the web server will be sending to the browser that called our servlet.
Of course this response is empty when the doGet method is called so that it can filled with what ever should be sent to the browser.
Finally whe have, at line 8, the throws IOException statement.
Java language forces you to handle exceptions.
An exception is any thing going wrong.
In our case, in the next couple of lines we are going to send data to the client browser, so an exception could be a breack in the communications to that browser, during our work.
As we are to lazy to handle this exception in our program it throws the exceptions to the calling system, the web server.

Now we have a pair of matching curly brakets at lines 9 and 12 that surrounds the body of our doGet method.

In the first line of our program (line 10) we have ServletOutputStream out = response.getOutputStream();.
This means we are creating an object out of class ServletOutputStream and assigning it the output stream associated with the response object.
In practice, we are getting and reference to the session established between the browser and our web server, so that the only thing we have to do next, is fill it with the data we want the browser to receive, and the web server will do the derivery.
Please notice the semi colon at the end of the statement.
In java, as in C or C++ language, each statement must end with a semi colon.
Notice that method or class definitions and code blocks (pairs of matching curly brackets) don't need a semi colon at the end.

Next line (line 11) we have the statement out.println("Hello World !!!");.
Now we are sending to the browser the "Hello World !!!" sentence.
We are using the ServletOutputStream object out and printing to it our message.

Now we have our servlet written.
We need to save it to a file with the same name of the class we defined with an extension ".java".
Our file will be HelloWorldServlet.java
Please notice that java is case sensitive, so you need to respect the capitalization.
Notice also that the line numbers in the above example should not be inserted in the file and are used only for the improve readability in our example.

Now we need to compile and run our example.
If you have not done so you must begin by downloading and installing the JDK and the JSWDK.
Now we need to compile our application.
We can do it by calling the java compiler as follows.


D:\Example\HelloWorldServlet>javac HelloWorldServlet.java

If you have difficulties in finding the javac compiler you may need to add the complete path to the compiler (c:\jdk1.2\bin\javac for example) or add it to your environment.
Now you must have a new file named HelloWorldServlet.class in the same directory of the source file HelloWorldServlet.java.
This is our servlet, ready to run.

To run our new servlet we should have a web server (I sudgest Apache 1.3) prepared to call servlets (I'd sudgest JServ).
After that you have to put our servlet HelloWorldServlet.class available to the web server and invoque it from the browser with the following statement.
http://localhost/servlet/HelloWorldServlet I'm assuming you have installed the web server and servlet support in your own machine.
if this is not the case you must replace localhost acordingly.
The servlet directory is the default directory for servlets if you are using JServ.
If this is not the case you should change it acordingly.

That's it for now.
You should try to mess around with the example presented.
Try to change the sentence the application outputs, try to repeat line 11 several time with diferent sentences, etc...
Try to change line 11 with the following lines.

      out.println("");
      out.println("Hello World !!!");
      out.println("");
  
After that try to add the following line before line 10:
      response.setContentType("text/html");
  

Last update:
2000/01/08
This page hosted by
Get your own Free Homepage
Prepared by:
Ricardo Lindengrün

Click Here!

1