How to encode a URL in jsp?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    How to encode a URL in jsp?

    I want to encode a url and send it to another page.However, when I press the link, it gives me page not found..

    <%
    String url="newjsp.jsp ?name=vimal&id= 0812573&NIC=vim al basdeo&f=nasha sahdjsa hk";

    url=URLEncoder. encode(url,"UTF-8");
    out.println("<a href="+url+"><i mg src='ajith-billa2.jpg' alt='Ajit'></a>");


    %>



    My URL becomes http://localhost:8080/WebApplication1/newjsp.jsp%3Fna me%3Dvimal%26id %3D0812573%26NI C%3Dvimal+basde o%26f%3Dnasha+s ahdjsa+hk after being encoded


    How do I sort this prob out?
  • Abhishek Saha
    New Member
    • Feb 2011
    • 7

    #2
    If I'm understanding correctly, this is not a URL encoding problem.

    Problem might be the file location.

    When we do url encoding basically we apply these rules.
    • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.

    The special characters ".", "-", "*", and "_" remain the same.[/LIST]
    • The space character " " is converted into a plus sign "+".
    • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.
      For example using UTF-8 as the encoding scheme the string "The string ü@foo-bar" would get converted to "The+string+%C3 %BC%40foo-bar" because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).


    So i think you are encoding the url correctly.

    is your corrent file( i mean where this jsp code is being running, and the newjsp.jsp are in same folder?

    Do a dry run.

    Code:
    <%
    /* For the time being let these statements be commented
    String url="newjsp.jsp?name=vimal&id=0812573&NIC=vimal basdeo&f=nasha sahdjsa hk";
    
    url=URLEncoder.encode(url,"UTF-8");
    */
    out.println("<a href="http://localhost:8080/WebApplication1/newjsp.jsp?name=vimal&id=0812573&NIC=vimal basdeo&f=nasha sahdjsa hk"><img src='ajith-billa2.jpg' alt='Ajit'></a>");
    
    %>
    And check whether its working or not.

    Comment

    Working...