servlet does not read space


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: servlet does not read space

  1. #1
    Join Date
    Nov 2004
    Posts
    4

    Unhappy servlet does not read space

    Hey folks,

    I am trying to write a servlet reading a database and pass the info to another servlet. Below is the line that I have a problem with.

    out.println("<INPUT TYPE = radio name = selection VALUE= "+city[i]+">");

    city[i] is actually the name of cities that I retrieved from a database. "i" is the index for the cities. The line works fine until cities with more than one word. For example, when I tried to read city[5], which has "New York" stored inside as a string, it will only read "New", which makes value = New, not New York.

    Can anybody help me on this? I am going crazy...

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I may be wrong ofcourse but isn't the string '%20' a way to represent blank values in parameters ?
    eschew obfuscation

  3. #3
    Join Date
    Nov 2004
    Posts
    4

    Unhappy %20 does not work

    Well, %20 may represent a space, but it does not work in this situation. city[i] actually has a string such as "Atlanta", "New York" or "Williamsburg" stored inside. However, when the "value" attribute tries to read it, it will read up to a space. For example, to read "San Diego", it will only read "San" and stop. Since city[i] is a variable, it is not possible to be programmed in the form of "San%20Diego".

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I suspect this problem to have a very easy solution, but I can't come up w. one yet.

    Even if a blank is embedded in the string variable it is possible to replace a blank w. '%20'

    If I had had my apache JServ setup ready I would have tested it.

    I mean, sending strings of text between servlets works, (how about a textarea ?)
    eschew obfuscation

  5. #5
    Join Date
    Nov 2004
    Posts
    4

    %20

    I am not sure how your can replace a blank with %20 when using variable. If you know, please tell me and I will be very grateful. I am going crazy here. I cannot believe I am stuck because of this simple thing.

    By the way, I am using Apache Tomcat server, which is free from Apache website

  6. #6
    Join Date
    Oct 2004
    Posts
    311
    Ok, the solution to this problem is a fairly simple one.
    In you input tag you have the folowin code for the value:
    Code:
    out.println("<INPUT TYPE = radio name = selection VALUE= "+city[i]+">");
    This will result in the following html soucecode:
    Code:
    <input type = radio name = selection value=New York>
    Now with HTML attributes are read in a very straighforward way. Only the first word after the "=" is regarded as the value belonging to that aatribute. The way to salve this problem, is to tell the HTML parser that both New and York are one word. This can be done by ensuring the are antered into your HTML source like this:
    Code:
    <input type="radio" name="selection" value="New York"/>
    The " around the attribute values tell the parser that whatever is behind the = and is withing the same block of "" should be treated as a single word.
    With this in mind, it is always good programming practice to have all attribute values in HTML tags between "". If you always do this, you are sure that it will always work.
    One problem when writing this from a servlet however is that you will have to escape the "inside the HTML tag:
    Code:
    out.println("<input type=\"radio\" name=\"selection\" value=\""+city[i]+"\">");
    Reason for this is that Java uses "as the start and end of a String declaration, and if you wouldn't escape the ones inside you HTML tag, Java would just consider them to be starting / ending your String declaration.

  7. #7
    Join Date
    Nov 2004
    Posts
    4

    Talking Many thanks!!!

    Thanks for your reply. Now my problem has been solved. I am so happy that I join Java Boutique. Again, thanks so much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center