Christian Vielma
Christian Vielma

Reputation: 16005

Java EE Servlet rewrite servlet path

I have a simple user application. I have a MainController Servlet that will be in charge of general interaction in the application. This servlet will be "listening" for url-patterns: /, /index, etc (any other form of index). I also have an UserController Servlet that is in charge of login among other things, this one "listens" from /CheckLogin url-pattern.

I have form in a jsp, in the url /, that when I submit it, it points to the servlet at /CheckLogin. Then UserController servlet process it and either the login is ok or wrong it redirect to / (the MainController will be in charge of determining the page to show depending if the user logged in or not).

All work flawlessly except that after submitting the first time, then the form page has url /CheckLogin. I would like to remove it and show only / (even if logged or not). How do I do this (plain Java EE, not Spring or any other framework)?

Upvotes: 1

Views: 578

Answers (1)

Paul Wostenberg
Paul Wostenberg

Reputation: 449

I'm not sure if this is what you're looking for, but if you do a forward instead of a redirect, the URL on the client browser will not be updated: RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse; an explanation can be found at Forward vs Redirect.

In addition, it might be a better idea to implement authentication as a Filter instead of a Servlet; that way, the Filter can be used to intercept any URL you deem needs to be secured.

Is there a specific reason you're not using a web framework? They're meant to take care of things security and routing for you, generally.

Upvotes: 2

Related Questions