Wednesday, August 12, 2009

Can you provide a sample Servlet ?

Here's the sample implementation of a Servlet to print "Welcome to the World of Servlets." on to the browser

class HelloServlet extends HttpServlet
{
/**
* Handle the HTTP GET method bybuilding a simple web page.
*/

public void doGet (HttpServletRequestrequest, HttpServletResponse response) throws
ServletException,IOException
{
PrintWriterout;

String title= "Hello Servlet";

//set content type and other response header fields first
response.setContentType("text/html");

// then write the data of the response
out =response.getWriter();
out.println(""); <br /> out.println(title); <br /> out.println("");
out.println("

"+ title + "

");
out.println("

Welcometo the World of Servlets.");
out.println("");
out.close();
}
}

No comments:

Post a Comment