• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic