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 “HelloServlet.java”. This will be our carpenter (Servlet) who handles requests.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

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

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="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

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"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Hello JSP</title>
</head>
<body>
    <h1>Welcome to our Website!</h1>
    <p>This is our cool new page.</p>
</body>
</html>

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!