This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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
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
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.
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: sequence of form information is not the same WHY???