aspose file tools*
The moose likes Servlets and the fly likes URL encoding and decoding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL encoding and decoding" Watch "URL encoding and decoding" New topic
Author

URL encoding and decoding

Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
Hello:

We need to URL encode and decode.

We need to provide a .jar file and use that .jar file to decrypt the item and use that for a parameter on the querystring.

Do you have any suggestions on how to create the .jar file?

Also do you know of any examples utilizing java servlets which are available for study?

Thanks,
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Encode and Decode are already built into most modern web-application frameworks.

If you're adding your own processing, then creating a jar with the class(es) is very easy in Eclipse.

If you package this jar with your war, or externally you should have no problems.

WP
Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
How would I know if it was built in to an existing application which I inherited?

Also I have found this technique here:

public class URLParamEncoder {

public static String encode(String input) {
StringBuilder resultStr = new StringBuilder();
for (char ch : input.toCharArray()) {
if (isUnsafe(ch)) {
resultStr.append('%');
resultStr.append(toHex(ch / 16));
resultStr.append(toHex(ch % 16));
} else {
resultStr.append(ch);
}
}
return resultStr.toString();
}

private static char toHex(int ch) {
return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
}

private static boolean isUnsafe(char ch) {
if (ch > 128 || ch < 0)
return true;
return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0;
}

}


I need to call this class from a simple servlet.

I think I need to insert the call into this portion of the servlet:



Am I going in the right direction here?

Also do you know where I could find out about making the Decode URL class into a jar file?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Why aren't you just using URLEncoder and URLDecoder?

And you use the word "decrypt". Encoding is not encrypting. I hope you aren't doing this for any security purposes!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
Do you mean importing it into the class?

If so can you name it? I tried a couple of things and my IDE / project cannot find it.

Thanks,
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

They are in the rt.jar under java.net package.

WP
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Michele Smith wrote:
Am I going in the right direction here?


why cant you use JSTL ?

<c:url value=' servletPath ' >
<c:param name = 'key' value = 'data' />
</c:url>

if key/data contains any non alphanumeric then, param tag encode the data for you.
Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
Thanks William, I got it, thanks a lot!

why cant you use JSTL ?

<c:url value=' servletPath ' >
<c:param name = 'key' value = 'data' />
</c:url>

if key/data contains any non alphanumeric then, param tag encode the data for you.


My questions are this, my parameter name is parentid

it's value is numeric.

I have never used JSTL before.

Where would I put that code?

One last thing, 'key' and 'data' refer to what exactly?

Thanks,
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Michele Smith wrote:
I have never used JSTL before.

http://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html
Michele Smith wrote:
One last thing, 'key' and 'data' refer to what exactly?

they are parameter name and it's value
Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
It looks like JSTL is used in JSP can it be used in XML 1.0?

Thanks,
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

No.
Sorry, if you are not going to do anything with jsp urls
Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
are there any alternatives? To working with XML 1.0?

thanks,
Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
Hello I did as Bear suggested, using the


import java.net.URLEncoder;

and put this in my servlet:



When I reference my xsl stylesheet link with this parameter as follows:



I wonder what I might be doing wrong.

Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: URL encoding and decoding