I use MVC pattern and my servlet serves me to process the request and give me a new page path to go. What if I just want to redirect from one page to another using servlet? Would it be ok or I should just use regular link redirecting?
Asked
Active
Viewed 212 times
2 Answers
2
You mean, first POST and then redirect? This makes not really sense if you aren't sending any data. Just make it a normal GET link and let the servlet if necessary do the preprocessing on doGet()
. The advantage is that it's bookmarkable and SEO-friendly.

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
-
I mean, if I want just to go from one page to another, should I always use servlet or just regular tag in jsp is ok? I've just thought that usual redirecting is not so secured. – And Dec 22 '11 at 13:03
-
Just use a normal link. You can also just use servlets on GET requests. Its `doGet()` will be invoked. Security is absolutely a non-concern in normal page-to-page navigation. To learn more about servlets, read http://stackoverflow.com/tags/servlets/info – BalusC Dec 22 '11 at 13:04
1
If you don't have additional tasks to do, only redirecting then you don't need a servlet - the simple GET link is OK.
But if you have some business processing, it would be better to delegate this kind of job to Servlet (controller). After the processing, the Servlet will redirecting (dispatching) to another .JSP (view).
Should be GET or POST? This depends what data you want to pass.

lukastymo
- 26,145
- 14
- 53
- 66
-
If the business processing is idempotent, you could just do it in the `doGet()` method and make the link to point to the URL of the servlet. Redirecting makes no sense as all preprocessed data would be lost (unless you store in session, which in turn makes no sense for request scoped data). I also suggest you to go through the servlets wiki page here: http://stackoverflow.com/tags/servlets/info – BalusC Dec 22 '11 at 13:10
-
@BalusC, "redirecting" it's not really redirecting :) (browser side) - I meant dispatching (use RequestDispatcher to do server redirect to some view). To learn more about RequestDispatcher read [stackoverflow.com/tags/servlets/info](http://stackoverflow.com/tags/servlets/info) – lukastymo Dec 22 '11 at 13:26
-
Sorry, I can't help if you didn't get your terms right. No worries further :) – BalusC Dec 22 '11 at 13:27
-
It's called "forwarding". See also http://stackoverflow.com/questions/7949034/in-the-context-of-java-servlet-what-is-the-difference-between-url-rewriting-and – BalusC Dec 22 '11 at 14:06
-
RequestDispatcher has forward (then it's forwarding) but you can also include (including) - dispatching includes forwarding and including [stackoverflow.com/questions/3095990/requestdispatcher-in-java](http://stackoverflow.com/questions/3095990/requestdispatcher-in-java) – lukastymo Dec 22 '11 at 14:11
-
Yes, I know that :) Include should however not be used for JSPs as in MVC views, but only for servlets as in request/response chaining. See also http://stackoverflow.com/questions/3024949/how-do-i-execute-a-sequence-of-servlets – BalusC Dec 22 '11 at 14:14