aspose file tools*
The moose likes Servlets and the fly likes sequence of form information is not the same WHY??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sequence of form information is not the same WHY???" Watch "sequence of form information is not the same WHY???" New topic
Author

sequence of form information is not the same WHY???

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
Hey,
I have made a servlet which recieves information through an html form and shows it back in the
clients broweser window. Everything works absolutely fine. However, the information retrieved
from the html form is not shown in the same sequence as that of the textfields in the html form.
What can I do to solve this problem?
Thanks to all.
Bye,
Tualha Khan


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12785
    
    5
Since you don't show the code, my guess is that your code
that extracts the parameters from the request and prints them
back does so by using the getParameterNames() method which returns an Enumeration.
Since parameters in a request are held in a Hashtable (or similar collection), the order of names in the Enumeration has nothing to do with the order of names in the form.
Bill

------------------
author of:
Daniel Olson
Ranch Hand

Joined: Jan 05, 2001
Posts: 81
If you are using the getParameterNames() method, the only way I can think of to get your form information in the correct sequence is to
1. On the form page, name the form elements in a way that can be sorted.
2. When you loop through the enumeration, create an array or some collection that can be sorted.
3. Sort the array or collection.
4. Print out the sorted array or collection.
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
Hey,
Heres my code, please suggest what should I do to get the sequence in the rder of html form.
================================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class myServlet3 extends HttpServlet
{
public void init(ServletConfig cfg) throws ServletException
{
super.init(cfg);
System.out.println("In init()");
}
public void destroy()
{
super.destroy();
System.out.println("In destroy()");
}
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//resp.setContentType("text/plain");
resp.setContentType("text/html");
PrintWriter out=new PrintWriter(resp.getOutputStream());
out.println("<html>");
out.println("<head>");
out.println("<title>");
out.println("The Registration Servlet");
out.println("</title>");
out.println("<body bgcolor=black text=white>");
out.println("<h1><center>VSNL New Account Registration</center></h1>");
out.println("<hr>");
out.println("<p>");
Enumeration enum=req.getParameterNames();
out.println("<table cols=2>");
while(enum.hasMoreElements())
{
String name=(String)enum.nextElement();
out.println("<tr>");
out.println("<td><b>");
out.println(name+"    </b></td><td><b> : </b>"+req.getParameter(name));
out.println("</td></tr>");
}
out.println("</table>");
out.println("<hr>");
out.println("<h3><center>");
out.println("Your Unique Access Number is : <u><font color=yellow>"+((long)(Math.random() * (long)(100000000)))+"</font></u>");
out.println("<br>");
out.println("Please change your Access Number As soon as possible");
out.println("</center></h3>");
out.println("<p>");
out.println("<hr>");
out.println("<center><h6>");
out.println("<a href=mailto:tualhakhan@yahoo.co.in>webmaster</a>");
out.println("<br>");
out.println("2001 © Turner Group NashVille");
out.println("</h6></center>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
======================================================
Bye,
Tualha Khan
PS: I am TOO WEAK in Collections!!!
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Tuala
Daniel has detailed very clearly what is to be done. Before making the change in servelet code, we need to change the naming of the form elements (i.e. form which calls this servelet), so that their names if sorted would appear in the same order as they physically display on the form.
For eg. prefix the name of first form element with A_, second form element with B_, third form element with C_.
Then use the enumeration to populate a collection which can be sorted, or a collection which is presorted. eg. you may define a local variable Set s=new TreeSet() and then do s.add(name) for every name retrieved through the enumeration.
Then get an iterator on s - Iterator it=s.iterator(). Then using it get each name (now they will appear in the order they physically appear on the form) and retrieve the value using getParameter(name) to retrieve the values.
Roughly the new code will be:-
Set s=new TreeSet();
while(enum.hasMoreElements())
{
String name=(String)enum.nextElement();
s.add(name);
}
Iterator it=s.iterator();
while(it.hasNext()) {
String name=(String)it.next();
out.println("<tr>");
out.println("<td><b>");
out.println(name+" </b></td><td><b> : </b>"+req.getParameter(name));
out.println("</td></tr>");
}
The API on Collections is very clear, and the framework is very simple. A good book like Core Java Vol-II would also help in making you strong in Collections.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sequence of form information is not the same WHY???