HelloWorldpublic class HelloWorld extends HttpServlet This is a simple Hello World servlet example to show the main
servlet methods. |
Fields Summary |
---|
private String | greeting |
Methods Summary |
---|
public void | destroy()This method is called once by the container just before the servlet
is taken out of service.
In this example, the method resets the servlet instance variable.
greeting = null;
| public void | doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)This method is called by the container for each GET request.
In this example, the method writes a line of text to the
response body.
PrintWriter out = response.getWriter();
out.println(greeting);
| public void | init()This method is called once by the container when the servlet is
initialized, before any requests are delivered to the servlet.
In this example, the method reads a servlet init parameter
value and saves it in an instance variable. If the parameter
is not found, a default value is saved instead.
ServletConfig config = getServletConfig();
greeting = config.getInitParameter("greeting");
if (greeting == null) {
greeting = "Hello World!";
}
|
|