Archive

Posts Tagged ‘JEE’

How to improve performance of Seam based RichFaces JSF application

December 2nd, 2009 Karthikeyan C 3 comments

I was trying to improve the performance (decrease the time taken to render the pages)   of a Seam based JSF application (Infact this post is applicable to any web application using RichFaces).

Initially the response time for JSF pages was around 12 to 20 seconds (The internet connection speed was roughly 512kbps). I tried the optimisation techniques mentioned in the links in this post http://www.karthikeyanc.com/blog/index.php/2009/11/jsf-and-richfaces-optimization-links/

Even then the page rendering time did not decrease substantially. So started using Firefox with YSlow and HttpFox add-ons.

Found that the js, css and image files related to RichFaces were expiring immediately and hence each time a JSF page was rendered, the resources (js,css and image files) were not fetched from local cache and the request was sent to the server again (It does not matter even if it is 304 Unmodified as already the damage is done and increases the response time).

Hence wrote a Servlet Filter which will override the default time to expire (which is immediate) and add a configurable duration as the time to expire. Part of the Filter code is provided below. The Filter name is ResponseHeaderFilter.java


public void init(FilterConfig filterConfig) {
 this.filterConfig = filterConfig;
 }

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

 HttpServletResponse httpResp = (HttpServletResponse) response;
 Enumeration e = filterConfig.getInitParameterNames();
 while (e.hasMoreElements()) {
 String headerName = (String) e.nextElement();
 httpResp.addHeader(headerName, filterConfig.getInitParameter(headerName));
 }
 chain.doFilter(request, response);
 }

In web.xml I added the following configuration for the filter.


<filter>
 <filter-name>CacheForDuration</filter-name>
 <filter-class>com.packagename.ResponseHeaderFilter</filter-class>
 <init-param>
 <param-name>Cache-Control</param-name>
 <param-value>max-age=900000, public</param-value>
 </init-param>
 </filter>

<filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/img/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/js/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/css/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/scripts/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/a4j/*</url-pattern>
 </filter-mapping>

Once this configuration was in place the resources (js, css and scripts) were fetched from local cache of the browser and hence the response time was reduced to 2 to 3 seconds. Hope this is useful.

  • Share/Bookmark
Categories: Development Tags: ,

Redirecting permanently in HttpServlet from your old domain (website) to new domain

November 29th, 2009 Karthikeyan C No comments

Let us assume we face a scenario where we move from a old domain (website) to a new domain (website). You may do this when you expand your business offering and the old name may not be appropriate.

But your old domain has gained reputation (like a good page rank and a number of incoming links) which you wish to carry forward to your new domain. One way of doing this is respond with HTTP code 301 which is permanently moved.

You may also make use of use Google web master tools to inform the change in domain address (as Google is currently the #1 search engine ). This will prevent the decrease in hits to your website from search engines.

The below code shows how to implement it if you host a Java web application for your old domain.

In web.xml make the below entry.


<?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">
 <servlet>
 <servlet-name>RedirectionServlet</servlet-name>
 <servlet-class>com.olddomain.web.servlet.RedirectionServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>RedirectionServlet</servlet-name>
 <url-pattern>/*</url-pattern>
 </servlet-mapping>
 </web-app>

Next the code for RedirectionServlet.java


package com.olddomain.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RedirectionServlet extends HttpServlet {

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 //Get the incoming URL request.
 response.setHeader("Location", "http://www.yournewdomain.com"+request.getRequestURI());
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
 }

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 processRequest(request, response);
 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 processRequest(request, response);
 }
}

Note: Using response.sendRedirect will produce only HTTP status code 302 which is temporarily moved and not 301 which indicates it is permanently moved.

  • Share/Bookmark

How to configure Log4j for a web application in Tomcat

November 18th, 2009 Karthikeyan C No comments

By default Tomcat uses JDK logging. I used the following  log4j.xml [which should go under WEB-INF\classes folder] to enable log4j for a SEAM based web application [infact  for any  web application] in Tomcat. In Maven please place it under, src\main\resources.


<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

 <appender name="CONSOLE">

 <param name="Target" value="System.out"/>

 <layout>
 <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n"/>
 </layout>
 </appender>
 <appender name="FILE">

 <param name="File" value="${catalina.base}/logs/yourwebappname.log"/>

 <param name="DatePattern" value="'.'yyyy-MM-dd"/>
 <layout>
 <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n"/>
 </layout>
 </appender>

 <root>
 <priority value="INFO"/>

 <appender-ref ref="FILE"/>
 <appender-ref ref="CONSOLE"/>
 </root>

 </log4j:configuration>

In a Maven based application add the below dependency to include log4j jar.

<dependency>
<groupId>apache-log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
  • Share/Bookmark