Servlets - Examples

Servlets are Java classes which service HTTP requests and implement the javax.servlet.Servlet interface. Web application engineers typically compose servlets that extend javax.servlet.http.HttpServlet, a abstract class that implements the Servlet interface and is uncommonly intended to handle with HTTP requests.

Sample Code

Following is the example source code structure of a servlet guide to indicate Hello World −

//Import required java libraries 


import javax.servlet.*; 

import javax.servlet.http.*; 

//Extend HttpServlet class 

public class HelloWorld extends HttpServlet { 

private String message; 

public void init() throws ServletException { 

//Do required initialization 

message = "Hello World"; 


public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set reaction content type 


//Actual rationale goes here. 

PrintWriter out = response.getWriter(); 

out.println("<h1>" + message + "</h1>"); 


public void destroy() { 

//do nothing. 



Compiling a Servlet

Let us make a record with name with the code appeared. Spot this file at C:\ServletDevel (in Windows) or at/usr/ServletDevel (in Unix). This way area must be added to CLASSPATH before proceeding further.

Assuming your environment is setup appropriately, go in ServletDevel catalog and assemble as pursues −

$ javac 

If the servlet depends upon some other libraries, you have to incorporate those JAR files on your CLASSPATH too. I have included just servlet-api.jar JAR record since I'm not utilizing some other library in Hello World program.

This command line utilizes the built-in javac compiler that accompanies the Sun Microsystems Java Software Development Kit (JDK). For this command to work legitimately, you have to incorporate the area of the Java SDK that you are utilizing in the PATH environment variable.

If everything goes fine, above arrangement would deliver HelloWorld.class file in a similar catalog. Next segment would clarify how an compiled servlet would be conveyed in production.

Servlet Deployment

By default, a servlet application is located at the way <Tomcat-installationdirectory>/webapps/ROOT and the class record would live in <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes.

If you have a completely qualified class name of com.myorg.MyServlet, at that point this servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class.

For now, let us copy HelloWorld.class into <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes and make following passages in web.xml file located in <Tomcat-establishment directory>/webapps/ROOT/WEB-INF/









Above sections to be made inside <web-app>...</web-app> tags accessible in web.xml record. There could be different passages in this table officially accessible, yet never mind.

You are nearly done, presently given us a chance to begin tomcat server utilizing <Tomcat-installationdirectory>\bin\startup.bat (on Windows) or <Tomcat-installationdirectory>/receptacle/ (on Linux/Solaris and so forth.) lastly type http://localhost:8080/HelloWorld in the program's location box. In the case of everything goes fine, you would get the accompanying result

© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3