---
title: "Server-side programming and Java Servlet"
subject: "Web Technology"
chapter: 3
key: "semester_7/web_technology:server-side-programming-servlet"
source: https://www.bctengineer.com/q/semester_7/web_technology/server-side-programming-servlet
site: BCT Engineering
---

# Server-side programming and Java Servlet

Web Technology · Chapter 3: Server-side programming

- Asked in: 2068 Chaitra (old course), 6 marks
- Video: [Introduction to Servlet And Its Working | CodeTechNix](https://www.youtube.com/watch?v=ztjt0Lzi8iU) — Journey with Murad
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/web_technology/server-side-programming-servlet
- Whole chapter: https://www.bctengineer.com/md/semester_7/web_technology/chapter-3

## Question

What is server-side programming? Explain how a Java Servlet works.

## Short answer

Server-side programming runs code on the server to generate dynamic responses, accessing databases and sending only HTML to the browser. A servlet is a Java class loaded once in a container; init() runs once, service() calls doGet or doPost per request in a thread, and destroy() runs at shutdown.

## Answer

#### Server-side programming
**Server-side programming** is writing programs that run on the web server to process client requests and generate dynamic responses. The server executes the code, accesses databases and files, applies business logic and sends only the resulting HTML (or JSON/XML) to the browser. Examples: PHP, JSP/Servlets, ASP.NET, Python (Django/Flask), Node.js. Uses: login and sessions, database-driven pages, form processing, e-commerce.

#### Java Servlet
A **servlet** is a Java class that runs inside a servlet container (web container) such as Apache Tomcat or Jetty. It extends `HttpServlet` and handles HTTP requests. Unlike CGI, a servlet is loaded once and each request runs in a lightweight **thread**, so it is fast and portable.

#### How a servlet works (life cycle)
1. The browser sends an HTTP request to a URL mapped to the servlet.
2. The web server passes it to the servlet container.
3. **Loading and `init()`:** On the first request (or at startup) the container loads the class, creates one instance and calls `init()` once.
4. **`service()`:** For every request the container creates `HttpServletRequest` and `HttpServletResponse` objects and calls `service()` in a new thread, which calls `doGet()` or `doPost()`.
5. The servlet reads parameters, may use JDBC to access the database, and writes the response through `response.getWriter()`.
6. The container sends the response back to the browser.
7. **`destroy()`:** When the container shuts down or unloads the servlet, `destroy()` is called once to release resources.

```text
Browser -> Web server -> Servlet container
                           init() (once)
                           service() -> doGet()/doPost()  (per request)
                           destroy() (once)
Browser <- response <------+
```

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

public class HelloServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<h1>Hello, " + req.getParameter("name") + "</h1>");
  }
}
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Where does a servlet run?
   A. In the browser
   B. In the DNS server
   C. Inside a servlet container such as Tomcat
   D. Inside the database
   Answer: C. The container manages loading and lifecycle. (Key point: Servlet container)

2. How many times is init() called?
   A. On every request
   B. Never
   C. Twice
   D. Once, when the servlet is loaded
   Answer: D. init() runs once after the container creates the instance. (Key point: init())

3. How are requests handled compared with CGI?
   A. Each request runs in a lightweight thread
   B. Each in a new process
   C. Sequentially only
   D. In the browser
   Answer: A. Servlets are loaded once, so they are faster. (Key point: Per-request)

4. What does service() do?
   A. Calls destroy()
   B. Calls doGet() or doPost()
   C. Loads the class
   D. Opens the database only
   Answer: B. service() dispatches according to the HTTP method. (Key point: service())

## Related in Web Technology

- [Short note: Server side programming](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Ashort-note-server-side-programming) — Web Technology, chapter 3
- [Client-side versus server-side scripting](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Aclient-vs-server-side-scripting) — Web Technology, chapter 3, asked 3×
- [Server side languages and multi-tier architecture](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Aserver-side-languages-multitier) — Web Technology, chapter 3

## Related in other subjects

- [Java simulation of single server queue](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Ajava-simulation-single-server) — Simulation and Modeling, chapter 9, asked 5×
- [XML and JavaScript for client-side web apps](https://www.bctengineer.com/question.md?key=semester_8%2Finternet_and_intranet%3Axml-javascript-client-side) — Internet and Intranet, chapter 6
