IT Definitionjava

What is a Servlet?

A servlet is a class that can receive and answer HTTP requests on a Java webserver (or more precisely a servlet container). A standard implementation of the javax.servlet.servlet interface is javax.servlet.http.HttpServlet.

Servlet example

Servlets that receive HTTP requests in a servlet container like Tomcat, Jetty or in a JEE Appserver inherit from the class javax.servlet.http.HttpServlet. The class contains methods for the various HTTP verbs (GET, POST, PUT, DELETE), which can be overwritten by the implementation. The most common methods are probably doGet(req, res) and doPost(req, res).


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BeispielServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();

        writer.println("<head><title>Java example servlet</title></head>");
        writer.println("<h1>Servlet example</h1>");
        writer.println("<p>Content of the HTML page</p>");


The servlet in the example returns a simple HTML page. Normally, in a large Java web application not every page is assembled as a string. Technologies like JSP, JSF or Spring MVC are better suited.
java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More

Leave a Reply

Your email address will not be published. Required fields are marked *