Creating Servlet

Creating Servlet by Implementing Interface

package com.servlet;
import java.io.IOException;
import jakarta.servlet.*;
import java.io.PrintWriter;
import java.util.Date;


public class FirstServlet implements Servlet{

    // Lifecycle methods
    ServletConfig sconf;
    @Override
    public void init(ServletConfig sconf) throws ServletException {
        this.sconf = sconf;
        System.out.println("Creating object");
    }

    @Override
    public void service(ServletRequest sreq, ServletResponse sres) throws ServletException, IOException {
        
        System.out.println("Servicing......");
        sres.setContentType("text/html");
        PrintWriter out = sres.getWriter();
        out.println("<h1>This is my first servlet.</h1>");
        out.println("<h2>Today's Date and time is "+new Date().toString()+".</h2>");
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


//  Non-life cycle methods.
    @Override
    public ServletConfig getServletConfig() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public String getServletInfo() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
}

Creating Servlet Using Generic Servlet


Next