🚀 Master Java & Servlets with Ram N Java!
Subscribe to get clear, beginner-friendly tutorials right when they drop.
🔔 Subscribe to Ram N JavaWhat is URL Rewriting?
In Java web applications, session tracking typically relies on cookies to maintain user state across stateless HTTP requests. However, what happens when a client disables cookies in their browser?
This is where URL Rewriting comes in. URL rewriting is a session tracking technique where session data—specifically the unique session identifier—is appended directly to the destination URL of hyperlinks and form actions.
How URL Rewriting Works in Servlets
When a client clicks a link or submits a request containing a rewritten URL, the servlet container parses the URL, extracts the session ID, and associates the request with the corresponding active HttpSession.
In Java Servlets, the container appends a path parameter named jsessionid separated by a semicolon:
Encoding URLs with HttpServletResponse
To implement URL rewriting reliably, the HttpServletResponse interface provides built-in methods that automatically determine whether to append the session ID (e.g., if cookies are disabled):
response.encodeURL(String url): Used for encoding regular hyperlinks (anchor tags) and redirect destinations within HTML output.response.encodeRedirectURL(String url): Used when sending a redirect viaresponse.sendRedirect()to preserve the session ID across the redirection.
Pros and Cons of URL Rewriting
Advantages:
- Works reliably even when cookies are entirely turned off in client browsers.
- Fully supported and automated by standard servlet containers using
encodeURL().
Disadvantages:
- The session ID is visibly exposed in the browser's address bar and referrer logs, posing security risks if shared.
- Every internal link and form submission across the entire application must be explicitly encoded.
Recommended Next Watch
Continue exploring servlet session tracking techniques with these tutorials:
Click here to watch in Youtube : https://www.youtube.com/watch?v=HnjvCnrHkmk
Click the below Image to Enlarge

No comments:
Post a Comment