2. Design Notes:

Let us try to make a list of the different things which we need to write.

  • A simple HTML page, with two text fields and a submit button. Will that do? Well, let us start with it and see.
  • A servlet class. It needs to get the parameters from request and print it out. It also needs to validate first name and last name and send back to the page, if validation error is found.
  • web.xml - We need to register our servlet in this deployment descriptor.

3. Writting the code:

  • HTML: MyFirstPage.html will mainly contain two simple text input and a submit button. Below is the html code:
<html>
  <head>
    <title>Test HTML</title>
  </head>
  <body>
  Please enter first Name and last name
  <form name="form" action="">
  First Name: <input type="text" name="firstName" ><br>
  Last Name: <input type="text" name="lastName" ><br><br>
  <input type="submit" name="submit" value="Submit" >
  </form>
  </body>
</html>
  • Java Servlet: MyFirstServlet
    • It needs to extend javax.servlet.http.HttpServlet to be a Servlet.
    • It overrides the doGet and doPost methods. In the doPost we have our code to fulfill the requirement for our simple webapp.
    • From doGet, we give a call to doPost method. So, irrespective of the GET or POST request from MyFirstPage.html, we will have the same logic.
package in.techfreaks.servlet;

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class MyFirstServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String strErrMsg = "Please enter first name and last name";
String strFirstName = request.getParameter("firstName");
String strLastName = request.getParameter("lastName");
if(strFirstName!=null && !strFirstName.equals("") && strLastName!=null && !strLastName.equals("")) {
PrintWriter out = response.getWriter();
out.println("Name : "+strFirstName+" "+strLastName);
} else {
request.setAttribute("errMsg", strErrMsg);
request.getRequestDispatcher("../MyFirstPage.html").forward(request, response);
}

}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

}


The above servlet will have to be compiled before it can be deployed. You will have to include the servlet jar in classpath for compilation, without which we will get compilation errors. Since we have Tomcat already installed, we can use servlet jar from Tomcat installation itself. The path for the jar file is <Tomcat Install Dir>\lib\servlet-api.jar

  • Creating the deployment descriptor (web.xml):
    The deployment descriptor (web.xml) is a mandatory configuration file as per the Servlet specification. For our example, we have only the bare minimum entries here as needed for our requirement. It mainly contains entries to registered our new servlet. Also, it needs to map the servlet to appropriate URL pattern. Below is the complete web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation=http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 <display-name>Simple WebApp</display-name>
  <description>
    Simple Web Application
  </description>  <servlet>
    <servlet-name>MyFirstServlet</servlet-name>
    <servlet-class>in.techfreaks.servlet.MyFirstServlet</servlet-class>
  </servlet>  <!-- Define the Manager Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>MyFirstServlet</servlet-name>
      <url-pattern>/servlet/*</url-pattern>
  </servlet-mapping>
</web-app>

Note that our servlet is mapped in such a way that, it will be called whenever the URL contains /servlet/ in it. For example: http://localhost:8080/simple-webapp/servlet/ABC will also call our servlet. By the servlet will be called, we mean that, the service method of our servlet will be called, which will internally call either doGet or doPost based on the type of call to our servlet.