Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting from a servlet without changing url?

I have a apache server, which applies mod_rewrite and mod_proxy based on a rewrite mapping file. The user just accesses the server, and does not know that he is redirected to another server supplying the source files. Everything is handled by apache in background.

As I'd now like to have more complexe redirecting, I thought about some solution within java/jsf/servlet.

Can I achieve the same robust remapping of an url from a servlet? If so, how could I start?

like image 653
membersound Avatar asked Nov 23 '25 10:11

membersound


1 Answers

  • A solution for redirecting from Java/servlet:

If it's on the same machine you can use forward:

request.getRequestDispatcher("pathToNewServletOrJsp").forward(request, response);


(with the relative path) and the URL will not change, otherwise you have to use sendRedirect:

response.sendRedirect("pathToNewServletOrJsp");

and in this case - the URL will change.

  • Another solution will be creating rules for 301 redirect in your .htaccess file.
like image 140
Nir Alfasi Avatar answered Nov 25 '25 22:11

Nir Alfasi