Servlet Jsp Hello World Example

 Posted On  | Yashwant Chavan 

In this article we are going to cover how to write Hello world Servlet Jsp web application. Define servlet mapping in Deployment descriptor, Forward request to the Jsp Page.

Define servlet

Helloworld servlet sometimes called controller extends "javax.servlet.http.HttpServlet". Here we override the doGet() method. Which handles your request.

package com.technicalkeeda;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  RequestDispatcher rd = request.getRequestDispatcher("hello.jsp");
  rd.forward(request, response);


Deployment Descriptor (web.xml)

There is only one deployment descriptor for the web application. web.xml contains the configuration details of your application. Below helloworld servlet configuration is defined. When request comes to server it pick up the respected servlet and hadover the request to the controller. In above code we forward to hello.jsp file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
 xmlns="" xmlns:xsi=""

view Page(hello.jsp)

Nothing special about below jsp code, it looks like sample html code.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Sample Hello World Example</title>
 <h1>Hello World</h1>


Deploy the application tomcat server and enter the URL to access helloworld servet "http://localhost:8080/SampleProject/hello"

Servlet Jsp Hello world example

© 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0239 seconds.