Archive for September, 2005

Open a new window maximized

Written by coregps on Friday, September 30th, 2005 in Web Design.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Just a javascript function used to open a new window maximized:

function openwindow(url) {
    var options = ’scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no’;
    options += ‘,width=’ + screen.availWidth + ‘,height=’ + screen.availHeight;
    options += ‘,screenX=0,screenY=0,top=0,left=0′;
    var win = window.open(url, ‘’, options);
    win.focus();
    win.moveTo(0, 0);
    win.resizeTo(screen.availWidth, screen.availHeight);
}

Advanced syntax highlighting tool

Written by coregps on Friday, September 23rd, 2005 in General.

I often need to post code snippet to my weblog. Today I found the Advanced Syntax Highlighting tool provided by devshed.com. It can be used to generate HTML output of source code. Using it is very easy. Just copy and paste the code and click the Highlight button, and then copy the result HTML to weblog editor. This tool supports many languages, including PHP, HTML, Java, C, C++, ASP etc.

Devshed.com also provides other useful webmaster tools that can be found at:
http://tools.devshed.com/webmaster-tools/

Compressing web content with GZip and Filter

Written by coregps on Wednesday, September 7th, 2005 in Java.

I come across some performence problems in my recent j2ee web project. It always takes a long time for the visitors to wait to see the whole web page. Most of the web content is text-based. So I decide to compress the web content using gzip, which can dramatically reduce download times for text-based files. This can be done in a Filter.

First, check whether a browser supports gzip compression. Browsers that support this feature will set the Accept-Encoding request header.

/** Does the client support gzip? */
public boolean isGzipSupported(HttpServletRequest request) {
    String encodings = request.getHeader(”Accept-Encoding”);
 return ((encodings != null) && (encodings.indexOf(”gzip”) != -1));
}

If a browser supports gzip, we can then send gzip-compressed content to it. The following is the whole code:

package com.esurfer.filters;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GzipFilter implements Filter {
 private FilterConfig config;

 public GzipFilter() {
 }

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

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
        // If the browser supports gzip, send compressed content.
  // otherwise send the web content directly.
  PrintWriter out;
  if (isGzipSupported((HttpServletRequest) request)) {
   out = getGzipWriter((HttpServletResponse) response);
   ((HttpServletResponse) response).setHeader(”Content-Encoding”,
     ”gzip”);
   ResponseWrapper wrapper = new ResponseWrapper(
     (HttpServletResponse) response);
   chain.doFilter(request, wrapper);
   out.print(wrapper.toString());
   out.close();
  } else {
   chain.doFilter(request, response);
  }

 }

 public void destroy() {
  config = null;
 }

 /** Check the Accepting-Encoding header from the HTTP request. */
 public boolean isGzipSupported(HttpServletRequest request) {
  String encodings = request.getHeader(”Accept-Encoding”);
  return ((encodings != null) && (encodings.indexOf(”gzip”) != -1));
 }

 /** Return gzipping PrintWriter for response. */
 public PrintWriter getGzipWriter(HttpServletResponse response)
   throws IOException {
  return (new PrintWriter(
    new GZIPOutputStream(response.getOutputStream())));
 }

}

The java class used to wrap the response object:

package com.esurfer.filters;

import java.io.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class ResponseWrapper extends HttpServletResponseWrapper {
 StringWriter writer = new StringWriter();
 public ResponseWrapper(HttpServletResponse response) {
  super(response);
 }
 
 public PrintWriter getWriter() {
  return new PrintWriter(writer);
 }
 
 public String toString() {
  return writer.toString();
 }
}

To make it work, we need register the gzip filter in the deployment descriptor, and then map it to URL patterns.

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

<!DOCTYPE web-app
PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
http://java.sun.com/dtd/web-app_2_3.dtd“>

<web-app>
<display-name>GzipFilter</display-name>
<description>
GzipFilter Test
</description>
<filter>       
  <filter-name>Compress</filter-name>       
  <filter-class>com.esurfer.filters.GzipFilter</filter-class>       
</filter> 
<filter-mapping>       
  <filter-name>Compress</filter-name>
  <url-pattern>*.html</url-pattern>   
</filter-mapping>
<filter-mapping>       
  <filter-name>Compress</filter-name>
  <url-pattern>*.htm</url-pattern>   
</filter-mapping>
<filter-mapping>       
  <filter-name>Compress</filter-name>
  <url-pattern>*.jsp</url-pattern>   
</filter-mapping> 
<filter-mapping>       
  <filter-name>Compress</filter-name>
  <url-pattern>*.css</url-pattern>   
</filter-mapping>
<filter-mapping>       
  <filter-name>Compress</filter-name>
  <url-pattern>*.js</url-pattern>   
</filter-mapping>
</web-app>

After finishing the steps above, we can examinate it using Web Page Analyzer provided by WebSiteOptimization.com. Which is a free website performance tool, and can be used to calculate page size, composition, and download time.

References

HTTP Compression http://www.port80software.com/products/httpzip/httpcompression

Compressing Web Content with mod_gzip and mod_deflate http://www.linuxjournal.com/article/6802

Servlet and JSP performance tuning  http://www.javaworld.com/javaworld/jw-06-2004/jw-0628-performance.html

Java Platform Performance: Strategies and Tactics http://java.sun.com/docs/books/performance/

How can you speed up webpage loading using Java Servlets? — From HttpRevealer.com  http://java.ittoolbox.com/pub/SC071902/httprevealer_servlets_itx.htm

Two Servlet Filters Every Web Application Should Have http://www.onjava.com/pub/a/onjava/2003/11/19/filters.html?page=1

Select all the checkboxes in a form

Written by coregps on Monday, September 5th, 2005 in Web Design.

The following two JavaScript functions can be used to quickly select or unselect all the checkboxes in a form.


The first one applies to a group of checkboxes with the same name:



function setCheckboxesByName(the_form, elts_name, do_check) {
    var elts = document.forms[the_form].elements[elts_name];
    var elts_cnt  = (typeof(elts.length) != ‘undefined’)
                  ? elts.length
                  : 0;
 if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        }
    } else {
        elts.checked = do_check;
    }
}


The second one applies to checkboxes with different names:



function setCheckboxesByType(do_check) {
    var elts = document.getElementsByTagName(”input”);
    var elts_cnt  = (typeof(elts.length) != ‘undefined’)
                  ? elts.length
                  : 0;
 if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            if (elts[i].type == “checkbox”) {
                elts[i].checked = do_check;
            }
        }
    } else {
        if (elts.type == “checkbox”) {
            elts.checked = do_check;
        }
    }
}


Example code below:


// Select or unselect checkboxes with the same name



<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>
<title>Select all checkboxes in a form</title>
<script type=”text/javascript”>
function setCheckboxesByName(the_form, elts_name, do_check) {
    var elts = document.forms[the_form].elements[elts_name];
    var elts_cnt  = (typeof(elts.length) != ‘undefined’)
                  ? elts.length
                  : 0;
 if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        }
    } else {
        elts.checked = do_check;
    }
}
</script>
</head>
<body>
<fieldset style=”width: 200″>
  <legend><input type=”checkbox” name=”sel_by_name” onclick=”setCheckboxesByName(’form1′, ‘tip’, this.checked)” />Select(with the same name)</legend>
  <form name=”form1″>
  <input type=”checkbox” name=”tip” />JSF<br />
  <input type=”checkbox” name=”tip” />JSTL<br />
  <input type=”checkbox” name=”tip” />Strtus<br />
  <input type=”checkbox” name=”tip” />Hibernate<br />
  </form>
</fieldset>
</body>
</html>


// Select or unselect checkboxes with different names



<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>
<title>Select all checkboxes in a form</title>
<script type=”text/javascript”>
function setCheckboxesByType(do_check) {
    var elts = document.getElementsByTagName(”input”);
    var elts_cnt  = (typeof(elts.length) != ‘undefined’)
                  ? elts.length
                  : 0;
 if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            if (elts[i].type == “checkbox”) {
                elts[i].checked = do_check;
            }
        }
    } else {
        if (elts.type == “checkbox”) {
            elts.checked = do_check;
        }
    }
}
</script>
</head>
<body>
<fieldset style=”width: 200″>
  <legend><input type=”checkbox” name=”sel_by_type” onclick=”setCheckboxesByType(this.checked)” />Select(with different names)</legend>
  <form name=”form2″>
  <input type=”checkbox” name=”tip1″ />JSF<br />
  <input type=”checkbox” name=”tip2″ />JSTL<br />
  <input type=”checkbox” name=”tip3″ />Strtus<br />
  <input type=”checkbox” name=”tip4″ />Hibernate<br />
  </form>
</fieldset>
</body>
</html>

Free online J2EE Programming course

Written by coregps on Friday, September 2nd, 2005 in Java.

A free online “J2EE Programming” course is currently being offered for anyone who wants to learn J2EE or increase their knowledge on J2EE.  In this 12-week course, students learn basic J2EE technologies such as Servlet, JSP, Ant, JDBC, Web application Security, Struts, and JavaServer Faces. They also learn how to use NetBeans IDE 4.1 effectively for building and deploying various types of J2EE applications.


This course runs very much like a regular college course in which the students are expected to do weekly homework and final project after studying the presentation material but it is free and can be taken online.  There is also class group alias where students can ask/answer questions.  The complete set of course contents (StarOffice slides with detailed speaker notes and some audio files, homework assignments, reading materials, code samples, FAQ etc.) are available on the website of the course.  This course has been taken by more than 12,000 people all over the world during the past 3 years and many of them found the course quite useful.


The current and the 8th session  of this class is done as of last week and the next and 9th session is going to start from Sep. 26th, 2005. The only thing you have to do in order to join the class is sending an email to


j2eerocks-subscribe@yahoogroups.com.


For detailed information about this class, please go to the following class website.


  Course website: http://www.javapassion.com/j2ee
  Course schedule: http://www.javapassion.com/j2ee/#ClassSchedule1
  Course group alias: http://groups.yahoo.com/group/j2eerocks/
  Course logistics: http://www.javapassion.com/j2ee/#About_This_Class



Site Navigation