Searching...
Sunday, 30 September 2012

Java Servlet Example

12:23

Servlet

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. See more about Servlet .

Servlet Interface

The Servlet interface is the central abstraction of the Servlet API and it defines methods which servlets must implement, see below Servlet root hierarchy.

Example

 This example is xml based ..
Create a maven web-app archetype project and add below dependencies 

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

Create a servlet class

package com.vinod;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class MyFirstServlet
 */

public class MyFirstServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */

    public MyFirstServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<HTML><TITLE>Http Servlet Example</TITLE><BODY>");
        out.println("<H2>Hello world</H2><HR>");
        out.println("</BODY><HTML>");
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Web.xml 

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
        <display-name>Archetype Created Web Application</display-name>
        <servlet>
            <servlet-name>MyFirstServlet</servlet-name>
            <servlet-class>com.vinod.MyFirstServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>MyFirstServlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>

Deploy application in to any web server and hit the below url

http://localhost:8080/vinod-web-project/MyFirstServlet

Download example

https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-web-project

Reference:
Done!!!

0 comments: