The Kahimyang Project Logo
Primo's Code Blog
Howtos tips and tricks
The Kahimyang Project

Changing domain names, redirect each JSF page to corresponding page in new domain using a JSF Filter

When changing domain names, Google recommends that redirection is done page by page in order to preserve page search rank. Below is how to redirect pages of a Java Server Faces (JSF) application to corresponding pages of a new domain name using a JSF Filter.

In JSF applications, Filters are executed before any page is rendered making it the best place to do redirection.

In Netbeans you create a Filter through its add/new file facility, selecting filter as file new file type. A ready to use stub will then be created for you. All you need is edit the doFilter method. Your web.xml will also be updated with the new created filter. Below is how the entry of your filter in your web.xml looks like. Please note that "Redirect" is the name I use with my own filter. Use any desired name.


    <filter>
        <filter-name>Redirect</filter-name>
        <filter-class<package-name.Redirect</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Redirect</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

A similar feature should also be available in Eclipse and other IDE's.

Option 1. Passthrough without prompts.


    public void doFilter(ServletRequest request, 
            ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        String uri = req.getRequestURI();
        String params = req.getQueryString();        	

        // this is your new domain	
        String url = "http://your-new-domain-name.com"; 	   
        
        url += uri;
        
        if (params != null) {
            url += ("?"+params);
        }
       
        resp.setStatus(301);
        resp.sendRedirect(url);

    }

When you need to tell your user that a redirection is about to happen, sometimes you need to prompt the user or redirect with a message.

Option 2: Prompt user. Replace last two lines in Option 1 above with the code below.

 
        
        resp.setContentType("text/html");
        ServletOutputStream out = resp.getOutputStream();
         
        out.println("<html>");               
        out.println("<body>");
        out.println("<br/>");     
        out.println("Your message");                
        out.println("<a href='"+url+"'><h3>"+url+"</h3></a>");
        
        out.println("<br/>");                        
        out.println("</body>");
        out.println("</html>");
                           
        out.flush();
                
        out.close();
        

If you are using Netbeans press ctrl-alt-i to include required imports.

Good luck.

RSS Logo



Related articles

Comment icon   Comments (Newest first)

Recent posts in Java/JSF category
Blue dot icon Using Rhinoslider with image and youtube content in JSF pages
Blue dot icon A dynamic standard sitemap.xml with Google image extension implemented as a Java Servlet
Blue dot icon Creating a Facebook-like panel with slim scrollbars and infinite scrolling in PrimeFaces
Blue dot icon Generating XML RSS 2 feeds with JDOM 2 with a servlet
Blue dot icon Building a page with infinite scroll in PrimeFaces using Waypoints jQuery plugin
Blue dot icon Implementing a collapsible ui:repeat rows in JSF
Blue dot icon Validating an email address in PrimeFaces p:inputText field with p:ajax





Most popular articles
Blue dot icon Using Expect script to automate SSH logins and do routine tasks accross multiple hosts   (15374)
Blue dot icon How to setup Tomcat 7 as your primary webserver on Debian Squeeze    (14074)
Blue dot icon How to setup FLV streaming with crtmpserver C++ RTMP server   (9841)
Blue dot icon A Java class for sending multipart Email messages through your Gmail account    (6978)
Blue dot icon How to use Google Translate's Text to Speech (TTS) services in your web page using Servlet   (5673)
Blue dot icon Speed up Primefaces page load with p:remoteCommand partial update   (5492)
Blue dot icon Building a mobile website with JSF 2 core and JQuery Mobile 1.0   (5320)
Blue dot icon Google Map-Adding markers, info window, circle, small control, and events in Javascript    (5224)