Simple Generic Servlet Example To Display Hello Welcome On The Web page

1. Simple Generic Servlet Example To Display Hello Welcome On The Web page.

For Developing Any Servlet Program You should follow below things mandatorily.

  • Load servlet-api.jar into eclipse lib folder as shown below.
    Simple Generic Servlet Example To Display Hello Welcome On The Web page 1
  • You can Load Servlet-api.jar through Project Properties And Java Build Path Also As Shown below.
    Simple Generic Servlet Example To Display Hello Welcome On The Web page 2
  • You can Load servlet-api.jar from Apache Tomcat Installed Lib Folder.
    For Example :- C:\Program Files\Apache Software Foundation\Tomcat 8.5\lib

index.html

<html>
<body>
<form action="./First">
<input type="submit" value="click me"/>
</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
</web-app>

First.java

import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class First extends GenericServlet {

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

response.getWriter().print("Hello Welcome");
}

}

Leave a Comment