Knowledgebase: Pre-Sales Questions
The Quintessential Servlet
Posted by Customer Service on 25 May 2005 12:39 AM
This example implements a servlet that handles GET requests.
    package com.mycompany;

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

public class MyServlet extends HttpServlet {
// This method is called by the servlet container just before this servlet
// is put into service. Note that it is possible that more than one
// instance of this servlet can be created in the same VM.
public void init() throws ServletException {
// Initialization code...
}

// This method is called by the servlet container to process a GET request.
// There may be many threads calling this method simultaneously.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
out.println("<html><head><title>A Simple Servlet</title></head><body>");
out.println("Today is "+(new java.util.Date()));
out.println("</body></html>");
out.close();
}

// This method is called by the servlet container just after this servlet
// is removed from service.
public void destroy() {
// Shutdown code...
}
}
(646 vote(s))
Helpful
Not helpful

Comments (0)