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:
- Servlet(ParameterServlet.java)
- HTML page(Form.html)
- An XML file(web.xml)
Step 1 - Create a servlet, for example, ParameterServlet.java
- package My;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Enumeration;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ParameterServlet extends HttpServlet
- {
- private static final long serialVersionUID = 1L;
- public ParameterServlet()
- {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- doGet(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
- out.println("<html><head>");
- out.println("<title>Servlet Parameter</title>");
- out.println("</head>");
- out.println("<body>");
- Enumeration parameters = request.getParameterNames();
- String param=null;
- while (parameters.hasMoreElements())
- {
- param=(String)parameters.nextElement();
- out.println(param + ":" + request.getParameter(param) + "<br>" );
- }
- out.println("</body></html>");
- out.close();
- }
- }
Step 2 - Create
a
HTML page for example Form.html
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Parameter Servlet Form</title>
- </head>
- <body>
- <form action="parameter" method=Post>
- <table width="400" border="0" cellspacing="0">
- <tr>
- <td>Name:</td>
- <td>
- <input type="text" name="name" size="20" maxlength="20">
- </td>
- <td>SSN:</td>
- <td>
- <input type="text" name="ssn" size="11" maxlength="11">
- </td>
- </tr>
- <tr>
- <td>Age:</td>
- <td>
- <input type="text" name="age" size="3" maxlength="3">
- </td>
- <td>email:</td>
- <td>
- <input type="text" name="email" size="30" maxlength="30">
- </td>
- </tr>
- <tr>
- <td> </td>
- <td> </td>
- <td> </td>
- <td>
- <input type="submit" name="submit" >
- <input type="reset" name="reset" >
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Step 3 - Create
a web.xml file if you use Eclipse IDE, by default created by IDE.
- <?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" 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">
- <servlet>
- <servlet-name>ParameterServlet</servlet-name>
- <servlet-class>My.ParameterServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ParameterServlet</servlet-name>
- <url-pattern>/parameter</url-pattern>
- </servlet-mapping>
- </web-app>
Step 4 - Right
Click on project or application ->
Run As -> Run on Server.
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.
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.
And the output of this application is given below.