File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Enumeration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Enumeration" Watch "Enumeration" New topic
Author

Enumeration

Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
I have ShowMessage.html and ShowMessageServlet.java . All work fine.
<HTML>
<HEAD>
<TITLE>XYZ Corporation, Service IT </title>
</HEAD>
<BODY bgcolor="#FCF054">
<H1>Requ�te </H1>
<HR><BR>
<FORM ACTION="/projet/servlet/ShowMessageServlet" METHOD="POST">
<TABLE CELLSPACING="2" CELLPADDING="2">
<TR>
<TD >Firstname :</TD>
<TD><INPUT TYPE="Text" NAME="firstName" SIZE="20"></TD>
</TR>
<TR>
<TD>Lastname:</TD>
<TD><INPUT TYPE="Text" NAME="lastName" SIZE="20"></TD>
</TR>
<TR>
<TD>email:</TD>
<TD><INPUT TYPE="Text" NAME="email" SIZE="20"></TD>
</TR>
<TR>
<TD>phone :</TD>
<TD><INPUT TYPE="Text" NAME="phone" SIZE="20"></TD>
</TR>
</TABLE>
<BR>
<HR><BR>
<INPUT TYPE="Submit" VALUE="Submit Request">
</FORM>
</BODY>
</HTML>

My Servlet :
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Importation des autres paquetages Java
import java.io.*;
public class ShowMessageServlet extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String NoValue="NoValue entered";
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("<HTML><HEAD><TITLE>");
out.println(" Confirmation de requ�te ");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
out.println("<P><U><H3><B>Here are your information :</B></H3></U></P>");
Enumeration enum = req.getParameterNames();
while(enum.hasMoreElements()) {
String paramName=(String)enum.nextElement();
out.println("<TABLE>");
out.println("<TR><TD><B>"+paramName+":</B></TD>");
String paramValue =req.getParameter(paramName);
if(paramValue.length()==0) {
out.println("<TD>"+NoValue+"</TD></TR>");
}
else{
out.println("<TD>"+paramValue+"</TD></TR>");
out.println("</TABLE>");
}
}
out.println("</BODY></HTML>");
out.close();
}

}

My response is :
phone: NoValue entered
email: NoValue entered
lastName: OKUCU
firstName: Engin
But in my ShowMessage.html 'firstName' is the first line and in my response i get it as the last line.
How can I resolve it ?thanks.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
If I understand your question correctly, then I think your problem is because getParameterNames() does not return the names in any particular order.
I'm not sure if there is a way to dynamically order them like that. I don't think they are ordered in any way when they are added to the request object. Anyone else have any ideas?


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi engin!
it depends upon the WebServer we are using to host JSP/Servlet that is recieving the request parameters via querystring or html form submission.
on Tomcat4.0.5 it gives me the different order than on iPlanet4.1 Enterprise Server....
(i tested this). and this i guess will depend upon which storage structure the WebServer is using to store the parameters (mostly Hashtable or Hashmap is used i believe)...same thing applies for Cookie storage as well.
but i don't think we ever depend upon the ordering of the request parameters...
regards
maulin
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Maulin thank you for your explanation.
is there a way to retrieve the parameters by order ? (other method than getParametersNames())
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
I ran into something similar to your problem with a project I had that was reading in quite a few params. I'll print my way of dealing with it and maybe it will help you, although I'd be interested to see if there is a more elegant way to do this.

-Pat
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Pat thank you.
Sorry what you did i also know. But my question is : guess anyone who want to retrieve the parameters in the HTML file but without knowing what are into. Your don't understand me? Example:
You did write a line with :
String[] paramsSort = {"foo", "moreFoo", "andMoreFoo", "etc."};
OR for my HTML file
String[] paramsSort = {"fistname", "lastname", "email", "phone"};

By writing this line, it was like you did already know wich parameter were in the HTML file.
I want to retrive the name and value from my file BY ORDER without knowing which parameter are in this file. Is there for this a way ?
Thank you very much
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
I'm not sure I have a solution for you then. As far as I know, the parameter order is not guaranteed to be in the same order as they are on your HTML page. This was the problem I also had...but fortunately I knew all the parameter names and was able to sort them in the order I wanted with the above example.
I suppose you could specify the order of the params you know your getting, and then iterate through the HashMap for the remaining ones, but again they will not be sorted in any consistent manner.
What about including hidden form fields that specify the order you want them in. For each param you could also write:
<input type="hidden" name="1" value="your param">
and then in your servlet you could retrieve all the params again.
While running through the while loop that puts your params and values in the HashMap you catch each hidden field like this:

Again though, this pretty much depends on you knowing the parameter names and sort order you want when the HTML form is built.
Other than this I don't know. I can't see how else you could sort parameter names when you don't know all the ones you'd be getting...other than sorting by alphabetical order. If you find a good alternative please let me know too :-)
-Pat
[ September 29, 2002: Message edited by: Pat Wallwork ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi engin
If we are submitting the form using POST method then there is a way to retrieve the parameters in the order submitted in the form. Though I am not *dead* sure about it. Just tested that on Tomcat4.0.5 and iPlanet4.1 ES.
The trick would be to read the parameters from the Inputstream of the request using getReader() method on Request object instead of doing request.getParameterNames() and then use Enumberation...
Here is a JSP that reads inputstream directly from the request,
<%@ page language="Java" import="java.io.*,java.util.*"%>
<HTML>
<BODY>
<TABLE border='1' cellpadding='2' cellspacing='0' width=700>
<TR>
<TD>Parameter Name</TD>
<TD>Parameter Value</TD>
</TR>
<%
try {
BufferedReader br = request.getReader();
String line = br.readLine();
System.out.println("printing request...");
if (line != null ) {
System.out.println(line);
//line = br.readLine();
}
br.close();
StringTokenizer stk = new StringTokenizer(line,"&");
while(stk.hasMoreTokens()) {
System.out.println("Token:"+stk.nextToken());
}

} catch(Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}
/**
* COMMENTED OUT THE ENUMERATION ACCESS TO THE REQUEST PARAMETERS
*/
/*
Enumeration params = request.getParameterNames();
String paramName="";
String paramValue="";
while( params.hasMoreElements()) {
paramName = (String)params.nextElement();
paramValue = request.getParameter(paramName);
%>
<TR>
<TD><%=paramName%></TD>
<TD><%=paramValue%></TD>
</TR>
<%
}
*/
%>
</TABLE>
</BODY>
</HTML>
Here is an output on Tomcat and iPlanet Webservers respectively,
Tomcat:
=====
Starting service Tomcat-Standalone
Apache Tomcat/4.0.5
Starting service Tomcat-Apache
Apache Tomcat/4.0.5
printing request...
firstname=1&lastname=8&email=3&phone=4&b=2&b=25
Token:firstname=1
Token:lastname=8
Token:email=3
Token hone=4
Token:b=2
Token:b=25
-----------------------------------------------
iPlanet 4.1 ES:
==============
bash-2.03$ printing request...
firstname=1&lastname=8&email=3&phone=4&b=2
Token:firstname=1
Token:lastname=8
Token:email=3
Token hone=4
Token:b=2
---------------------------------------------
Hope this helps.
Regards
Maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enumeration