Quintessential Servlet
Example program for Quintessential servlet
Quintessential servlet is not any special kind of servlet. It is just a
proper and appropriate way to write a servlet. This quintessential servlet means
that servlet is having accurate way of code written for an organizations servlet. The word "Quintessential"
means appropriate, best, classical, essential format of code writing . This
example implements a servlet that handles GET requests. The
example of this "Quintessential servlet" is given as
below:
package com.mycompanyname;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServletName 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
getServletContext().log("getinit init");
// Get the value of an initialization parameter
String value = getServletConfig().getInitParameter("param1");
// Get all available intialization parameters
java.util.Enumeration enum =
getServletConfig().getInitParameterNames();
for (; enum.hasMoreElements(); ) {
// Get the name of the init parameter
String name = (String)enum.nextElement();
// Get the value of the init parameter
value = getServletConfig().getInitParameter(name);
}
// The int parameters can also be retrieved using
the servlet context
value = getServletContext().getInitParameter("param1");
}
// 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...
}
}
|
Download Source Code