Header Ads

How to get multiple form data in servlet

We have already learnt that how to send single form data to servlet in my  previous post
http://www.askqtp.com/2012/01/how-to-write-html-page-to-invoke.html

Now I am writing some advanced technique how you can send multiple data of a form to the server.

Sample HTML will have lot more textboxes...


 <input type="text" name="text1" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="text" name="text2" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="text" name="text3" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="text" name="text4" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="submit" name="Submit" value="Submit"/>


The html overall looks like---

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
    </head>
    <body>
        <form action="servlet2" method="get" enctype="text/plain" name="sample">
            <input type="text" name="text1" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="text" name="text2" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="text" name="text3" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="text" name="text4" value="text" maxlength="20" size="20"/>
            <br/>
            <input type="submit" name="Submit" value="Submit"/>
        </form>
     
    </body>
</html>


Here there are four text boxes trying to send data to the server...The Name of servlet which is trying to catch those data is "servlet2"

In servlet there are three ways how you can catch those data...

  • public Enumeration ServletRequest.getParametersName()...will give you the parameters names...for this example-text1,text2,text3,text4..so you will get the list and need to iterate by calling another method getParameter(); 
  • public String ServletRequest.getParameter(String name)---When you know the exact name of fieldname and want to get the corresponding value. This will return only String value. 

Lets check the first function--

        String temp=null;
        Enumeration param=request.getParameterNames();
        while(param.hasMoreElements()) {
            temp=(String)param.nextElement();
            out.println(temp);
             
        }
        value=request.getParameterValues("text1");
        out.println(value[0]);
For the second function....

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        String data=request.getParameter("text");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Servlet2</title></head>");
        out.println("<body>");
     
        out.println("<p>Your data is </p>"+data+"--->"+request.getParameter("text1"));
        out.println("<p>Your data is </p>"+data+"--->"+request.getParameter("text2"));
        out.println("<p>Your data is </p>"+data+"--->"+request.getParameter("text3"));
        out.println("<p>Your data is </p>"+data+"--->"+request.getParameter("text4"));

        out.println("</body></html>");
        out.close();
    }

       

When you are getting more than one value through a single variable

Well this might occur in very rare...but yes Servlet also have a answered this situation
say all of your field names are text or you are appending all the values to a single variable and sending it to server viz servlet.
So there has to have a mechanism to read that value as well...




        String[] value;
        value=request.getParameterValues("text1");
        out.println(value[0]);
        or else you can find out the size of the array and loop through for the values..


so how the servlet will look like--



import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet2 extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    /**Process the HTTP doGet request.
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        String data=request.getParameter("text");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Servlet2</title></head>");
        out.println("<body>");
     
        out.println("<p>The servlet has received a GET. This is the reply.Your data is </p>"+data+"--->"+request.getParameter("text1"));
        out.println("<p>The servlet has received a GET. This is the reply.Your data is </p>"+data+"--->"+request.getParameter("text2"));
        out.println("<p>The servlet has received a GET. This is the reply.Your data is </p>"+data+"--->"+request.getParameter("text3"));
        out.println("<p>The servlet has received a GET. This is the reply.Your data is </p>"+data+"--->"+request.getParameter("text4"));
     
     
        //method two
        String temp=null;
        String[] value;
        Enumeration param=request.getParameterNames();
        while(param.hasMoreElements()) {
            temp=(String)param.nextElement();
            out.println(temp);
             
        }
        value=request.getParameterValues("text1");
        out.println(value[0]);
        out.println("</body></html>");
        out.close();
    }

 
}

300x250-CMS

output---Client end












output---servlet end

Powered by Blogger.