Pages

Servlet Example by implementing Servlet interface

package com.javaoneworld.classes;


import java.io.*;
import javax.servlet.*;

public class MyFirstServlet implements Servlet{
    ServletConfig config=null;

    //init method
    public void init(ServletConfig config){
        this.config=config;
        System.out.println("servlet is initialized");
    }

    //service
    public void service(ServletRequest req,ServletResponse res)
            throws IOException,ServletException{

//setting content type as text/html
        res.setContentType("text/html");

//object of PrintWriter class to display the output
        PrintWriter out=res.getWriter();
        out.print("<html><body>");
        out.print("<b>first servlet example</b>");
        out.print("</body></html>");

    }
    //destroy
    public void destroy(){System.out.println("servlet is destroyed");}

    //ServletConfig
    public ServletConfig getServletConfig(){return config;}

    //ServletInfo
    public String getServletInfo(){return "javaoneworld servlet";}

}

No comments:

Post a Comment