How to Retrieve Data from an HTML Page Using a Servlet

Introduction

 
In this article, you will make an application (RetrieveServletData) to retrieve all the data from an HTML page and then display the data in the browser. For this application we create:
  1. Servlet(ParameterServlet.java)
  2. HTML page(Form.html)
  3. An XML file(web.xml)
Step 1 - Create a servlet, for example, ParameterServlet.java
  1. package My;  
  2. import java.io.IOException;  
  3. import java.io.PrintWriter;  
  4. import java.util.Enumeration;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. public class ParameterServlet extends HttpServlet   
  10. {  
  11.     private static final long serialVersionUID = 1L;  
  12.     public ParameterServlet()   
  13.       {  
  14.         super();     
  15.       }  
  16.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   
  17.       {  
  18.         doGet(request, response);  
  19.       }  
  20.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   
  21.       {  
  22.         response.setContentType("text/html");  
  23.         PrintWriter out=response.getWriter();  
  24.         out.println("<html><head>");  
  25.         out.println("<title>Servlet Parameter</title>");  
  26.         out.println("</head>");  
  27.         out.println("<body>");  
  28.         Enumeration parameters = request.getParameterNames();  
  29.         String param=null;  
  30.             while (parameters.hasMoreElements())  
  31.             {  
  32.                   param=(String)parameters.nextElement();                    
  33.                   out.println(param + ":" + request.getParameter(param) + "<br>" );                    
  34.             }  
  35.        out.println("</body></html>");  
  36.        out.close();  
  37.       }  
  38. }  
Step 2 - Create a HTML page for example Form.html 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>Parameter Servlet Form</title>  
  6. </head>  
  7. <body>  
  8. <form action="parameter" method=Post>  
  9. <table width="400" border="0" cellspacing="0">  
  10.   <tr>  
  11.     <td>Name:</td>  
  12.     <td>  
  13.     <input type="text" name="name" size="20" maxlength="20">  
  14.     </td>  
  15.     <td>SSN:</td>  
  16.     <td>  
  17.     <input type="text" name="ssn" size="11" maxlength="11">  
  18.     </td>  
  19.   </tr>  
  20.   <tr>  
  21.     <td>Age:</td>  
  22.     <td>  
  23.     <input type="text" name="age" size="3" maxlength="3">  
  24.     </td>  
  25.     <td>email:</td>  
  26.     <td>  
  27.     <input type="text" name="email" size="30" maxlength="30">  
  28.     </td>  
  29.   </tr>  
  30.   <tr>  
  31.     <td> </td>  
  32.     <td> </td>  
  33.     <td> </td>  
  34.     <td>  
  35.         <input type="submit" name="submit" >  
  36.        <input type="reset" name="reset" >  
  37.      </td>  
  38.     </tr>  
  39. </table>  
  40. </form>  
  41. </body>  
  42. </html>  
Step 3 - Create a web.xml file if you use Eclipse IDE, by default created by IDE.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">  
  3.   <servlet>  
  4.     <servlet-name>ParameterServlet</servlet-name>  
  5.     <servlet-class>My.ParameterServlet</servlet-class>  
  6.   </servlet>  
  7.   <servlet-mapping>  
  8.     <servlet-name>ParameterServlet</servlet-name>  
  9.     <url-pattern>/parameter</url-pattern>  
  10.   </servlet-mapping>  
  11. </web-app>  
Step 4 - Right Click on project or application -> Run As -> Run on Server.
 
RunRetrieveServlet 
 
Step 5 - Browser is open and at the end of URL enter the Form.html for example http://localhost:9999/RetriveServletData/Form.html Enter all the information in text boxes and click on submit Query button.
 
OpenBrowser 
 
After clicking on submit query button, you see the URL is changed http://localhost:9999/RetriveServletData/parameter that is, this page is redirected to the given URL pattern in web.xml file.
 
url 
 
And the output of this application is given below.
 
outputof-RetrieveServlet 


Similar Articles