Servlet Introduction

What is Servlet ?

Servlet is a technology that is used to develop web application.

Servlet is an API that is written in Java language.

Servlet is server side programming language that is used to create dynamic pages.

In MVC(Model View and Controller) servlet works as a controller.

Servlet is a container also that contains servlet instance.

Servlet is based on configuration and you can use mapping file or annotation to confure the servlet.

Servlet is a predefine interface that exist on javax.servlet package.

Servlet is a server side programming language that means you need a server to run servlet programs.

Servlet is based on life cycle that is used to perform task.

There are three way to create a Servlet:

  1. By implements Servlet Interface
  2. By extends GenericServlet class
  3. By extends HttpServlet class
Before servlet program execution you need to understand the project structure to create a web application.

src : In this folder you can keep the java file like model class, servlet class, database etc.

WebContent : This folder contains web related resources like html file, jsp file, images, css, js etc. You can organize group of image files in images folder, css files in css folder, and js files in js folder etc.

WEB-INF : In this folder you can hold lib folder where you can keep external jar files and web.xml file is deployment descriptor file where servlet class is mapped but in servlet 3.0 you can remove web.xml and mapped all the thing via annotation.

Servlet interface has following life cycles methods :

Hello World using servlet interface.

In src folder create a package with any name in my case I am using com.deepsingh44.blogspot.controller and then create a class HelloWorldServlet like below:

    
    package com.deepsingh44.blogspot.controller;
    import java.io.IOException;
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;

    public class HelloWorldServlet implements Servlet {
    private ServletConfig arg0;

    @Override
    public void destroy() {
    System.out.println("destroy here..."); 
    }

    @Override
    public ServletConfig getServletConfig() {
    return arg0;
    }

    @Override
    public String getServletInfo() {
    return "Hello World";
    }

    @Override
    public void init(ServletConfig arg0) throws ServletException {
    this.arg0 = arg0;
    System.out.println("initialized here...");  
    }

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
    throws ServletException, IOException {
    
    //this is used to write the content in console
    System.out.println("serve to client from here");

    //that is used to create dynamic response to the client's browser.
    arg1.getWriter().print("Hello World in plain text on browser");

    //if you want to set html content on browser then you need to set content type.
    arg1.setContentType("text/html");
    arg1.getWriter().print("<h1>Hello World in html on browser</h1>");
    
    }
}

After creating servlet class you need to mapped this in web.xml file to create an url to access from browser like below :

        
      <web-app>

            <servlet>
            <servlet-name>firstdemo</servlet-name>
            <servlet-class>com.deepsingh44.blogspot.controller.HelloWorldServlet
            </servlet-class>
            </servlet>
        
            <servlet-mapping>
            <servlet-name>firstdemo</servlet-name>
            <url-pattern>/helloservlet</url-pattern>
            </servlet-mapping>
        
        </web-app>
    

Then run your application on server (Tomcat) and get result like below:

It generate an error page not found because you are not call your url then you need to call url like below:

Now you can get successfully result.

No comments: