Java.Servlets.What happens to symbols when i don’t encode them in URL, give example ?

🧠 What Happens if You Don’t Encode Symbols in a URL?

  • The browser or server may misinterpret your URL.
  • Special symbols have specific meanings in URLs.
  • If they are not properly encoded, they can break the URL structure or cause wrong behavior.

🎯 Examples: What Happens With Some Symbols

SymbolMeaning in URLWhat happens if unencoded?
?Start of query parametersEverything after ? is treated as a parameter list
&Separates parametersStarts a new parameter
=Separates key and valueDefines the value for a parameter
#Start of fragmentBrowser jumps to fragment, server ignores everything after #
/Path separatorTreated as a new directory
+In form encoding, means spaceCould be misinterpreted if not handled correctly

🔥 Real Examples:


✅ Example 1: & without encoding

Suppose you want to send:

product=Fish & Chips

And you build URL:

https://example.com/menu?dish=Fish & Chips

Browser interprets:

  • dish=Fish (before &)
  • and a new parameter Chips without a value!

Wrong parsing!

✅ Correct:

String dish = "Fish & Chips";
String encodedDish = URLEncoder.encode(dish, StandardCharsets.UTF_8);

Result:

https://example.com/menu?dish=Fish+%26+Chips

Now server correctly reads:
dish = Fish & Chips


✅ Example 2: # without encoding

Suppose you send:

https://example.com/search?query=fun#games

What happens?

  • Browser sees #games as a fragment (like a bookmark inside the page).
  • Server only receives /search?query=funeverything after # is NOT sent to server.

⚡ Server never sees “games” at all!

✅ Correct way:

You must encode #:

  • %23

Then:

https://example.com/search?query=fun%23games

Now server receives query=fun#games.

✅ Example 3: = without encoding

Suppose:

name=John=Smith

Without encoding:

https://example.com/register?name=John=Smith

Browser/server might think:

  • name = John
  • Extra =Smith confuses parsers.

✅ Correct way:

Encode = as %3D inside parameter value.

🚀 Quick Practical Rule:

If symbol is used for…Problem
Structure of URL (like ?, &, =, /, #)Must encode if part of data!
Data value itself (like text)Always encode!

✅ Always encode data values, not the structural parts.


⚡ Quick Summary

Without EncodingWith Encoding
Fish & ChipsBreaks into 2 parametersEncoded properly
fun#gamesFragment ignored by serverEncoded as %23
John=SmithConfuses key=value parsingEncoded as %3D

🛠️ Final Memory Trick:

“If it’s data, encode it.”
“If it’s URL structure, leave it alone.”

This entry was posted in Без рубрики. Bookmark the permalink.