My problem is as follows and i hope you will help me find the appropriate solution.
I have an html form with 200 input fields of type text. The first input field contains the name of a book author and the remaining 199 fields hold each a title of a book that he or she has written. I also have a servlet that processes the form data and sends it to a file. The entered data is restricted to a combination of the characters "a...z A...Z". Please note the white space. Now, how do i go about with creating my html page. I started it like this but got stuck. The input fields are just too many and i have no idea on how to retrieve the 200 values in servlet.
I am so impatient to hear from you.
I think it is something doable but i cannot figure it out yet. I am going from the principle that if it can be done for 50 why not 200? I also want to mention that entry can be made at random inside the page. Just that an empty field must not be accounted for during writing to file.
I am going from the principle that if it can be done for 50 why not 200?
That's not really a principle. It certainly *can* be done, it's just not a good UI. But I take your point that this is a complication that you are not ready to tackle.
I also want to mention that entry can be made at random inside the page. Just that an empty field must not be accounted for during writing to file.
Well, that's easy to check in the servlet code - call trim() on the title string, and if its length is 0, don't save it.
Somebody hinted me to rather use a jsp for the entry page. These experienced people, they never tell in details how things work. They just give some clues and that's it.
Imagine the frustration when on the 99th line your power drops for a second due to a lightning stroke or you get called to the phone!
I would say it is time to back off thinking about the servlet / form mechanics and think about the user experience. Use the power of the technology to support your user not make the user a slave to the interface.
Let the application support entering one book at a time, building a list that is backed up to storage and displayed as part of the HTML .
Think of the support you could add - look up ISBN numbers on Amazon for example....
Thanks a lot for your support. They have reviewed the specs to a reasonable point. Now the form should contain 70 input fields. One for the author name and 69 for the book titles. Ulf suggested to change the input text fields names to bookTitle1, bookTile2, etc... But still they are just too many. I will have to test each of them in my servlet and that may take me hours. Do you have any more efficient mehod?
It's not like any piece of code will be replicated 70 times ... I think I'm missing something.
I am glad to hear from you. You are always there when i post a message, ready to give help. I also do not seem to understand you. Do you suggest that i create 69 input fields with names bookTitle1, bookTitle2,...,bookTitle69?
My last post was in response to yours, where you were talking about server-side code, and how it would take long. To which I replied I didn't understand why, and could you elaborate - which you have not done so far, so I can't add anything to that.
Now you're talking about the client-side. One way to handle 70 otherwise identical fields on the server is to give them predictable names like "bookTitle1" etc., precisely so that you don't have to duplicate code on the server, but can process them in a loop. (And since you ask specifically whether *I* would suggest that - well, only in so far as it solves your problem. I still think it's bad design, but I feel like a hung record saying that over and over, so I will stop saying it now.)
would return an array for "title", with each element being one title. The same is true for POST, but it was easier to illustrate as a GET.
Paul Ngom wrote:
But how am i going to retrieve them in my servlet?
There's a getParameterValues() method on the request object which returns a array of String values. See http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterValues%28java.lang.String%29
Paul Ngom wrote:Ulf,
Thanks for your reply. But the 200 text fields have to show at a go ready for entry.
I can guarantee that if somebody expects to enter 200 book names and then hit submit, probably the session will expire and the headache will be major. I would suggest you allow entering 10 at a time, and keep adding a number of times.
Otherwise: you generate the page from the servlet, in a for loop you generate the html code with the fields, and that will do the rendering. Personally, considering you have 200 times one field and not one collection of fields, you can give the same name and then retrieve them as an array with request.getParameterValues where you put a name and then you get a String array with all the values.
But again, 200 fields before you submit is an issue and if this is the requirement you have to talk to the business person out of this approach.