IT & Programming

Java Servlets & Web Application Basics — Advanced Java Beginner's Guide Episode 13 (Updated June 2026)

Learn Java Servlets for web application development — servlet lifecycle, doGet, doPost, request handling, response writing, and deployment. Advanced Java Episode 13 for Indian IT students.

AB
ABC Trainings Team
June 16, 2026 — 7 min read

Java Servlets & Web Application Basics — Advanced Java Beginner's Guide Episode 13 (Updated June 2026) (Updated June 2026)

If you've been asking yourself how websites actually work behind the scenes — how a form submission triggers a login, or how clicking Place Order on Flipkart sets off a chain of events — the answer starts with web application concepts and Java Servlets. Servlets are the backbone of Java web development, and even though frameworks like Spring Boot have abstracted them away, every Java web developer needs to understand what's happening at the Servlet layer. NASSCOM projects a critical shortage of Java web developers in India through 2027, and companies like TCS, Infosys, Wipro, and HCL continue to run massive Java EE codebases that depend on Servlet technology. Episode 13 of our Advanced Java series covers the fundamentals of web applications and Servlets, starting from how HTTP works right through to deployment on Apache Tomcat.

TL;DR
  • A Servlet is a Java class that runs on a web server and handles HTTP requests — it's the foundation of Java web development
  • The Servlet lifecycle has three stages: init() once on startup, service() on every request, destroy() on shutdown
  • doGet() handles URL-based requests (displaying data); doPost() handles form submissions (creating/updating data)
  • HttpServletRequest gives you everything about the incoming request; HttpServletResponse lets you write the reply
  • Servlets are packaged in a .war file and deployed on a Servlet container like Apache Tomcat or WildFly

What Is a Java Servlet and What Problem Does It Solve?

A Java Servlet is a Java class that extends the capabilities of a web server by processing HTTP requests and generating HTTP responses. Before Servlets, web servers executed separate CGI programs for each request — expensive and slow. Servlets run inside a Servlet container (like Apache Tomcat) as persistent objects, handling thousands of concurrent requests efficiently. Think of a Servlet as a Java class that sits between the browser and your application logic: it receives the request, calls the right business methods, and sends back a response. Even if you eventually use Spring Boot (which uses embedded Tomcat under the hood), understanding raw Servlets makes you significantly more effective when debugging web application issues.

Java Servlets & Web Application Basics — Advanced Java Beginner's Guide Episode 13 (Updated June 2026)
Real student workshop at ABC Trainings

The Servlet Lifecycle — init, service, and destroy

Every Servlet goes through three lifecycle stages managed by the Servlet container. The init() method is called once when the Servlet is first loaded — use it for one-time setup like opening database connections or loading configuration. The service() method is called for every incoming request — you never override this directly; instead, the container calls doGet() or doPost() based on the HTTP method. The destroy() method is called once when the Servlet is about to be unloaded — use it to release resources like database connections. What most beginners don't realise is that a single Servlet instance serves all concurrent requests — which means instance variables are shared and almost always a threading bug waiting to happen. Keep your Servlets stateless: all per-request data should be in local variables.

Servlet MethodHTTP MethodTypical UseHas Body?
doGet()GETDisplay pages, read dataNo
doPost()POSTForm submit, create dataYes
doPut()PUTUpdate existing resourceYes
doDelete()DELETERemove resourceNo

Handling GET and POST Requests — doGet and doPost

HTTP has several request methods, but doGet() and doPost() handle the vast majority of web application traffic. doGet() is invoked for GET requests — typically when a user types a URL, clicks a link, or the browser needs to display a page. GET requests should be safe (no side effects) and idempotent (calling them multiple times has the same effect). doPost() is invoked for POST requests — form submissions, login forms, data creation. POST requests can have a request body carrying the form data. The standard rule: use GET for reading data, POST for writing data. Servlets that handle both often delegate from doGet() to doPost() or vice versa using the same helper method.

Java Servlets & Web Application Basics — Advanced Java Beginner's Guide Episode 13 (Updated June 2026)
Real student workshop at ABC Trainings

Working with HttpServletRequest and HttpServletResponse

HttpServletRequest encapsulates everything about the incoming HTTP request. Call request.getParameter("username") to get a form field or URL parameter by name. Call request.getHeader("User-Agent") to get any HTTP header. Call request.getSession() to access or create the user's session. Call request.getAttribute("someObject") to retrieve objects set by other Servlets or filters earlier in the request chain. HttpServletResponse controls everything about the outgoing response. Call response.setContentType("text/html") before writing any content. Call response.getWriter() to get a PrintWriter for sending text. Call response.sendRedirect("/success.jsp") to issue an HTTP redirect. Get these two objects right and you can build any web application logic.

Deploying a Servlet on Apache Tomcat

Servlets are packaged in a Web Archive (.war) file — a JAR file with a specific directory structure including WEB-INF/web.xml (the deployment descriptor), WEB-INF/classes (compiled Servlet classes), and WEB-INF/lib (JAR dependencies). Apache Tomcat is the most popular open-source Servlet container for development and production. To deploy a .war on Tomcat: place the file in Tomcat's webapps directory and start Tomcat — it automatically extracts and deploys the application. In production environments at companies like Infosys and Wipro, deployment is automated via CI/CD pipelines (Jenkins, GitLab CI) that build the .war file from source, run tests, and deploy to staging before production. Modern Spring Boot packages Tomcat inside the JAR, eliminating the separate deployment step entirely.

Maharashtra's CMYKPY (Chief Minister Yuva Karya Prashikshan Yojana) offers stipends of ₹6,000–₹10,000 per month for IT students in Java and web development training. ABC Trainings helps eligible candidates apply and connects them with IT companies in Pune's Wagholi and Hadapsar corridors, Sambhajinagar CIDCO, and Sangli for real-world apprenticeship placements.

Get the IT & Programming Brochure + Fees + Batch Dates on WhatsApp

Free 1:1 counselling. Placement track record. CMYKPY/PMKVY eligibility check.

💬 Get Brochure on WhatsApp📞 Call 7039169629

About the author: Amit Kulkarni. 8 yrs leading IT training at ABC Trainings, ex-Infosys.

Visit Our Centers

  • Wagholi (Pune): 1st Floor, Laxmi Datta Arcade, Pune-Ahilyanagar Highway. Call 7039169629
  • Hadapsar (Pune HQ): 1st Floor, Shree Tower, opp. Vaibhav Theater, Magarpatta. Call 7039169629
  • Cidco (Chh. Sambhajinagar): Kalpana Plaza, opp. Eiffel Tower, N-1 Cidco. Call 7039169629
  • Osmanpura (Chh. Sambhajinagar): S.S.C Board to Peer Bazar Road, near Jama Masjid. Call 7039169629
  • Sangli: Shubham Emphoria, 1st Floor, Above US Polo Assn., Sangli-Miraj Rd, Vishrambag. Weekend batches available. Call 7039169629

💬 WhatsApp 7774002496

FAQs

What is a Java Servlet and how is it different from a JSP?

A Java Servlet is a Java class that runs on a web server to handle HTTP requests and generate responses — it contains Java logic that processes data and controls application flow. A JSP (JavaServer Pages) is a view technology that mixes HTML with Java tags to render dynamic content. In the MVC pattern, Servlets act as Controllers (business logic and routing) and JSPs act as Views (presentation). Servlets were introduced first; JSPs were added to make view code easier to write than raw PrintWriter HTML.

What is the lifecycle of a Java Servlet?

The Servlet lifecycle has three stages. init() is called once when the Servlet is first loaded by the container — use it for one-time initialisation. service() is called for every HTTP request — the container dispatches to doGet(), doPost(), etc. based on the HTTP method. destroy() is called once when the Servlet is unloaded (server shutdown or redeployment) — use it for cleanup like closing database connections. A single Servlet instance handles all concurrent requests, so never store per-request state in instance variables.

When should I use doGet vs doPost in a Servlet?

Use doGet() when the request is reading or displaying data and has no side effects — URL navigation, search results, displaying a form. Use doPost() when the request modifies data — form submissions, login actions, placing an order. GET requests append parameters to the URL (visible in the browser bar and server logs); POST requests send parameters in the request body (not visible in the URL). Always use POST for sensitive data like passwords and for any operation that changes server state.

Do I still need to learn Servlets if I'm going to use Spring Boot?

Yes. Spring Boot uses an embedded Tomcat Servlet container and Spring MVC's DispatcherServlet under the hood. Understanding the Servlet lifecycle, request/response handling, and the difference between doGet and doPost makes you a more effective Spring developer — you'll understand why @GetMapping and @PostMapping exist, how request parameters map to method arguments, and how to debug issues that occur at the HTTP layer. Servlet knowledge is also regularly tested in Java technical interviews at TCS, Infosys, and Wipro.

A

ABC Trainings Team

Expert insights on engineering, design, and technology careers from India's trusted CAD & IT training institute with 11 years of experience and 2000+ trained professionals.