Servlet Programs
Life Cycle Example
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Servlet Lifecycle Example</title>
</head>
<body>
<form action="S1" method="post">
<input type="submit" value="Make request" />
</form>
</body>
</html>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>ServletLifecycle</servlet-name>
<servlet-class>ServletLifecycleExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLifecycle</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ServletLifecycleExample extends GenericServlet {
@Override
public void init() {
// initialize the servlet, and print something in the console.
[Link]("Servlet Initialized!");
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// the service method will
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("Servlet called from html page!");
}
@Override
public void destroy() {
// close connections etc.
}
}