Example of Hello Servlet Class

Here we write a class called HelloServlet class which show the how can write a servlet.
First we write a HelloServlet class which extends the  HttpServlet and override the public void doGet(HttpServletRequest request,HttpServletResponse response) method.
See the code....

package com.javaforecast4u;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String docType ="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0  Transitional//EN\">\n";
    out.println(docType +
             "<HTML>\n" +
             "<HEAD><TITLE>Hello Servlet</TITLE></HEAD>\n" +
             "<BODY>\n" +
             "<H2>Hello Servlet</H2>" +
             "</BODY>" +
             "</HTML>");
    }
}

0 comments :

Post a Comment