YouTip LogoYouTip

Servlet First Example

# Servlet Example A Servlet is a Java class that serves HTTP requests and implements the **javax.servlet.Servlet** interface. Web application developers typically write Servlets that extend `javax.servlet.http.HttpServlet`, an abstract class that implements the Servlet interface specifically for handling HTTP requests. ## Hello World Example Code Here is the source code for a Servlet that outputs Hello World: // Import required java librariesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;// Extend HttpServlet classpublic 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 response content type response.setContentType("text/html"); // Actual logic is here PrintWriter out = response.getWriter(); out.println("

" + message + "

"); } public void destroy() { // Do nothing }} ## Compiling the Servlet Let's write the above code in a file named HelloWorld.java. 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, navigate 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 didn't use any other libraries in the Hello World program. This command uses the `javac` compiler included in the Sun Microsystems Java Software Development Kit (JDK). For this command to work, the PATH environment variable needs to be set to the path of the Java SDK. If everything goes well, the compilation above will generate a HelloWorld.class file in the same directory. The next section explains how to deploy the compiled Servlet in production. ## Servlet Deployment By default, a Servlet application is located at the path `/webapps/ROOT`, and its class files are placed in `/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 HelloWorld.class to `/webapps/ROOT/WEB-INF/classes` and create the following entry in the **web.xml** file located at `/webapps/ROOT/WEB-INF/`: HelloWorld HelloWorld HelloWorld /HelloWorld The above entry should be created within the `...` tags in the web.xml file. There may already be various entries in this file, but don't worry about them. At this point, you are basically done. Now, let's start the Tomcat server using `binstartup.bat` (on Windows) or `/bin/startup.sh` (on Linux/Solaris, etc.). Finally, type **http://localhost:8080/HelloWorld** in your browser's address bar. If everything goes well, you will see the following result: ![Image 2: Servlet Example](#)
← Servlet Form DataServlet Life Cycle β†’