Servlet Instance
Servlet is a Java class that serves HTTP requests and implements the javax.servlet.Servlet interface. Web application developers typically write Servlets to extend javax.servlet.http.HttpServlet and implement the abstract class that specializes in handling HTTP requests.
Hello World Example Code
Below is the example source code for a Servlet that outputs Hello World:
// Import necessary Java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend the HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException {
// Perform necessary initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Set the response content type
response.setContentType("text/html");
// Actual logic goes here
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy() {
// Do nothing
}
}
Compiling the Servlet
Let's write the above code in the HelloWorld.java file and place this file in C:\ServletDevel (on Windows) or /usr/ServletDevel (on UNIX). You also need to add these directories to your CLASSPATH.
Assuming your environment is set up correctly, go to the ServletDevel directory and compile HelloWorld.java as follows:
$ javac HelloWorld.java
If the Servlet depends on any other libraries, you must include those JAR files in your CLASSPATH. Here, I only included the servlet-api.jar JAR file because I did not use any other libraries in the Hello World program.
This command line uses the javac compiler built into the Sun Microsystems Java Software Development Kit (JDK). For this command to work, the PATH environment variable needs to be set to the Java SDK path.
If everything goes smoothly, the compilation will produce the HelloWorld.class file in the same directory. The next section will explain how to deploy the compiled Servlet in production.
Servlet Deployment
By default, Servlet applications are located under the path <Tomcat-installation-directory>/webapps/ROOT, and class files are placed in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.
If you have a fully qualified class name com.myorg.MyServlet, then this Servlet class must be located at WEB-INF/classes/com/myorg/MyServlet.class.
Now, let's copy the HelloWorld.class to <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes and create the following entry in the web.xml file located at <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/:
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
The above entry should be created within the <web-app>...</web-app>
tags in the web.xml file. There may already be various entries available in the file, but do not be concerned about them.
You have essentially completed the setup. Now, let's start the Tomcat server using <Tomcat-installation-directory>\bin\startup.bat
(on Windows) or <Tomcat-installation-directory>/bin/startup.sh
(on Linux/Solaris, etc.), and finally enter http://localhost:8080/HelloWorld in the address bar of your browser. If everything goes smoothly, you will see the following result: