In this example we will see how to do coding, compiling, deploying, and running a simple Hello World servlet which will just print "Hello World" in the browser.
To learn life cycle of servlet, click here
We will useTomcat server to run our HelloWorld servlet. To download tomcat, click here
Lets create our project folder in webapps lets say TestServlet
Example:
C:\apache-tomcat-7.0.11\webapps\TestServlet
Now lets create our first hello world servlet, lets say HelloWorldServlet.java
HelloWorldServlet.java
C:\apache-tomcat-7.0.11\webapps\TestServlet\WEB-INF\classes\HelloWorldServlet.java
After adding the file web.xml, restart your tomcat server. Run below mentioned file to start tomcat server.
Windows
C:\apache-tomcat-7.0.11\bin\startup.bat
Linux
C:\apache-tomcat-7.0.11\bin\startup.sh
Now we are done, open browser and hit below mentioned URL.
http://localhost:8080/TestServlet/hwservlet
Output
To learn life cycle of servlet, click here
We will useTomcat server to run our HelloWorld servlet. To download tomcat, click here
Lets create our project folder in webapps lets say TestServlet
Example:
C:\apache-tomcat-7.0.11\webapps\TestServlet
Now lets create our first hello world servlet, lets say HelloWorldServlet.java
HelloWorldServlet.java
C:\apache-tomcat-7.0.11\webapps\TestServlet\WEB-INF\classes\HelloWorldServlet.java
/* HelloWorldServlet.java */ import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) { PrintWriter pw = null; try { pw = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } pw.println("<html>"); pw.println("<body>"); pw.println("<h2>Hello World!!</h2>"); pw.println("</body>"); pw.println("</html>"); } }
As you can see HelloWorldServlet extends HttpServlet, hence it needs to import package javax.servlet.http. In tomcat it is provided as a part of servlet-api.jar file. Which is in the directory C:\apache-tomcat-7.0.11\lib. To compile our class just include the said jar file in classpath.
Example:
$javac -cp C:\apache-tomcat-7.0.11\lib\servlet-api.jar HelloWorldServlet.java
You should see HelloWorldServlet.class file generated in the same folder.
web.xml
C:\apache-tomcat-7.0.11\webapps\TestServlet\WEB-INF\web.xml
C:\apache-tomcat-7.0.11\webapps\TestServlet\WEB-INF\web.xml
web.xml file (also known as the deployment descriptor). This file is the heart of a web application, and every web application must have it. It contains the information needed by the servlet container in order to run the web application, such as servlet declarations and mappings, properties, authorization and security constraints, and so forth.
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>HWServletName</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HWServletName</servlet-name> <url-pattern>/hwservlet</url-pattern> </servlet-mapping> </web-app>
After adding the file web.xml, restart your tomcat server. Run below mentioned file to start tomcat server.
Windows
C:\apache-tomcat-7.0.11\bin\startup.bat
Linux
C:\apache-tomcat-7.0.11\bin\startup.sh
Now we are done, open browser and hit below mentioned URL.
http://localhost:8080/TestServlet/hwservlet
Output