![]() |
The Java Developers Almanac 1.4 |
|
e1035. The Quintessential ServletThis 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...
// See e1036 Getting and Setting Initialization Parameters for a Servlet
}
// 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...
}
}
e1037. Returning an Image in a Servlet e1038. Saving Data in a Servlet e1039. Logging a Message in a Servlet © 2002 Addison-Wesley. |