0% found this document useful (0 votes)
21 views2 pages

Servlet Programs

The document provides an example of a servlet lifecycle, including an HTML form that triggers a servlet request. It contains the necessary web.xml configuration for servlet mapping and a Java class that implements the servlet lifecycle methods: init, service, and destroy. The servlet outputs a message when called from the HTML page and initializes with a console message.

Uploaded by

Shankar Deva CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Servlet Programs

The document provides an example of a servlet lifecycle, including an HTML form that triggers a servlet request. It contains the necessary web.xml configuration for servlet mapping and a Java class that implements the servlet lifecycle methods: init, service, and destroy. The servlet outputs a message when called from the HTML page and initializes with a console message.

Uploaded by

Shankar Deva CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.
}
}

You might also like