How to use a filter to change an incoming request url :
From : "http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123"
To : "http://nm-java.appspot.com/Check_License?Contact_Id=My_Obj_123" ?
Frank
Edit : According to BalusC's steps below, I came up with the following lines :
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
public class Url_Filter implements Filter
{
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig=filterConfig;
}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
{
HttpServletRequest httpRequest=(HttpServletRequest)request;
HttpServletResponse httpResponse=(HttpServletResponse)response;
String Incoming_Url=httpRequest.getRequestURI();
if (Incoming_Url.startsWith("/Check_License/Dir_My_App/"))
{
String Replace_Part=Incoming_Url.substring(Incoming_Url.indexOf("/Dir_My_App"),Incoming_Url.lastIndexOf("/")+1); // /Dir_My_App/Dir_ABC/
String New_Url=Incoming_Url.replace(Replace_Part,"?Contact_Id="); // http://nm-java.appspot.com/Check_License?Contact_Id=My_Obj_123
// Use either ServletRequest#getRequestDispatcher() and then RequestDispatcher#forward() to forward the request/response to the new URL (server-side redirect, not reflected in browser address bar)
RequestDispatcher request_Dispatcher=request.getRequestDispatcher(New_Url);
request_Dispatcher.forward(request,response);
/*
// Or cast the incoming ServletResponse to HttpServletResponse and then HttpServletResponse#sendRedirect() to redirect the response to the new URL (client side redirect, reflected in browser address bar).
httpResponse.sendRedirect(New_Url);
*/
}
chain.doFilter(request,response);
}
public void destroy()
{
}
}
My web.xml look like this :
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>Url_Filter</filter-name>
<filter-class>PayPal_Monitor.Url_Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Url_Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>PayPal_Monitor</servlet-name>
<servlet-class>PayPal_Monitor.PayPal_Monitor_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PayPal_Monitor</servlet-name>
<url-pattern>/PayPal_Monitor</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Check_License</servlet-name>
<servlet-class>PayPal_Monitor.Check_License_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Check_License</servlet-name>
<url-pattern>/Check_License</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>PayPal_Monitor.jsp</welcome-file>
</welcome-file-list>
</web-app>
I tried both server-side and client-side redirect with the expected results. It worked, thanks BalusC !