import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WelcomeServlet extends HttpServlet{
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<?xml version = \"1.0\"?>");
out.printf("%s%s%s", "<!DOCTYPE html PUBLIC",
"\"-//W3C//DTD XHTML 1.0 Strict//EN\"",
"\"http://www.w3.org/TRxhtml1-strict.dtd\">\n");
out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<title>A Simple Servlet Example</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Welcome to Servlet !</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>
Java How to Program JSP and Servlet Chapter Examples
</display-name>
<description>
This is the web application in which we demonstrate our JSP and Servlet example.
</description>
<servlet>
<servlet-name>welcome1</servlet-name>
<description>
A simple servlet that handles an HTTP get request
</description>
<servlet-class>
WelcomeServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Welcome1</servlet-name>
<url-pattern>Welcome1</url-pattern>
</servlet-mapping>
</web-app>
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http:www.w3.org/TRxhtml 1/DTD/xhtml 1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Handling an HTTP get Request</title>
</head>
<body>
<form action = "/jhtp6/welcome1" method = "get">
<p><label>Click the button to invoke the servlet<input type = "submit" value = "Get HTML document"/>
</label></p>
</form>
</body>
</html>