• Post Reply Bookmark Topic Watch Topic
  • New Topic

URLEncoder.encode() method restrictions?  RSS feed

 
john von
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i couldn't run this line under DECLARATION:
URLEncoder.encode("Hello World1", "UTF-8");
any know restrictions?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"jazzy",
We're really pleased that you are here with us in the JSP forum, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
JSP Forum Bartender
 
john von
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about that - jazzy
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No prob! Now please define "couldn't run". I can't diagnose much from your description.
 
john von
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.
This line works fine:

Output:
Hello+World
But NOT this line:

What could be the problem with the second line? My tomcat did not specify any specific problem. It seems that the line of code cant work inside a jsp declaration.
[ April 14, 2004: Message edited by: john von ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you expect to happen? A declaration will cause no output. It takes whatever you put inside it and places it at the class level of the servlet generated on the behalf of your JSP.
 
john von
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't expect an output. My question is, Why can't i even declare it and print the value of "x" in latter part of my code?
Try for yourself
 
john von
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any solution from this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still haven't adequately described what the problem is.
I suspect that a problem you will encounter is that the call to encode throws exceptions (at least an UnsupportedEncodingException, if I recall correctly). Since there is no convenient way to capture (and more importantly, intelligently deal with) exceptions at that point, trying to do somthing like this doesn't make much sense.
You could probably get around this by bending over backwards, making the variable static, and using a static block to initialize it, where you can catch the exception (but then what will you do with it?).
So why do you insist on doing this? It's not a very good idea.
 
john von
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if i didn't made it clear. Here's the simpliest description of the problem:
WHY THESE CODES DOES NOT WORK?
<%! String x = java.net.URLEncoder.encode("Hello World", "UTF-8"); %>
<%= x %>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"doesn't work" is the most useless phrase on earth.
Did you even read my reply? I can't offer you any other help if you are going to be coy.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What could be the problem with the second line? My tomcat did not specify any specific problem.
Really? My installation of Tomcat 4.1.27 was very clear about the problem.
It says exactly what Bear suggested it would say.
Perhaps the lesson to remember here, is that a class definition is created by the Servlet container using the JSP. As Bear said, "A declaration ... takes whatever you put inside it and places it at the class level of the servlet generated on the behalf of your JSP." In other words, these items become instance properties of the generated class definition. If their initialization might throw a checked exception, then you've a problem to address.
The code in your first example is placed inside of a method, called _jspService I think. This method, like many servlet service methods, is declared to throw IOException and ServletException. UnsupportedEncodingException is a subclass of IOException, and so handling it in this context is not required (since the method is declared to throw any IOException).
Got it?
[ April 19, 2004: Message edited by: Dirk Schreckmann ]
 
john von
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dude, i don't know what term or lingo do you want me to use and i don't know how you interpret "doesnt work" in a programmers forum with codes below it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!