Interview Questions, Answers and Tutorials

Creating a basic web application using Servlets and JSP

Creating a basic web application using Servlets and JSP

Imagine you’re building a little house on the internet where people can visit and do cool stuff. To make this happen, we need two special helpers: Servlets and JSP. They’re like the carpenters and decorators of our internet house.

What are Servlets and JSP? Servlets are like the carpenters. They handle the behind-the-scenes work when someone visits our website. They deal with requests and responses, just like how a carpenter builds and fixes things in a house. JSP (JavaServer Pages) are like decorators. They make our website look nice and pretty. They mix HTML with Java code to create dynamic web pages.

Getting Started: First, we need to set up our tools. We’ll use Java programming language, a web server like Apache Tomcat, and a text editor like Notepad++.

Building Our Web Application:

Step 1: Setting up the Project: Create a new folder for your project. Let’s call it “MyWebApp”.

Step 2: Writing the Servlet: Inside “MyWebApp”, create a Java file named “”. This will be our carpenter (Servlet) who handles requests.

import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<h1>Hello, World!</h1>");

Step 3: Configuring Servlet: We need to tell our web server about our Servlet. Create a file named “web.xml” inside the “WEB-INF” folder (create if not exists) and add the following code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""

Step 4: Creating JSP Page: Now, let’s create a JSP file to decorate our page. Inside “MyWebApp”, create a file named “hello.jsp”.

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html>
    <title>Hello JSP</title>
    <h1>Welcome to our Website!</h1>
    <p>This is our cool new page.</p>

Step 5: Running the Application: Copy the “MyWebApp” folder to the “webapps” folder inside Apache Tomcat installation directory. Start Tomcat server. Open a web browser and navigate to http://localhost:8080/MyWebApp/hello. You should see “Hello, World!” message along with our decorated JSP page.

Conclusion: That’s it! You’ve just built your first web application using Servlets and JSP. Servlets handle requests and responses, while JSP helps in creating dynamic web pages. Keep practicing and exploring to build even cooler stuff on the internet!