One of my recent tasks was making a new skin for one of our websites. the site had a common header, left side area and right side area that are to be shared among all pages. we used to include file for "header", "left side", "right side" in each of the pages.. it wasn't that pretty i know.
I wanted to think of something better where i can define the template in exactly one file and i do not have to include that file on every new page i add. something pretty much close the RubyOnRails "<% yield %>' directive to import the page content inside the template. i finally managed to do it.
I'll use two simple pages to illustrate the concept:
mahmoud.jsp
and index.jsp
what we want is to have each of the two pages displayed in a layout where is one header, banners and so. which will look like the following.
I was able to do this using a servlet that intercepts the request. and loads the template jsp file and pass the desired page to as a parameter to be included.
The template file is called template.jsp will look like:
The only line that matters in the template code is where we make the jsp:include page="<%request.getParameter("targetPage")%>"
The servlet intercepts http requests and loads this template using RequestDispatcher and passes the original desired page as a parameter:
The Servlet and servlet mapping in web.xml will look like
A note to be mentioned is that I had to configure my template to be initiated with requests to html pages (i can choose to work on any extension except jsp). When I configure my servlet mapping to be associated with jsp URLs, i get stuck in an infinite loop as since including the page inside the template results in a new jsp request.
The idea is simple. You are free to create your own template and to work on any file extensions in the url, but you have to specify a type other than jsp to avoid the infinite loop trap.
Bon appetite
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Tuesday, February 5, 2008
Wednesday, October 31, 2007
Effective Java Programming
Effective Java Programming, by Addison Wesley, is one of nice books i have read in software development material. and i recommend it to any developer developing in Java, starter or senior.
programming by nature is very flexible. you have many choices. it is like creating a statue using clay. You choose at every point; Class names, methods and variable names. public methods, internal implementation algorithms, structure of the package classes,.. all of those and others are left for the programmer to choose.
Effective Java programming comes to introduce a lot of the best practices for Java programmers; to enhance the stability, readability, clarity, reusability and maintainability of their code. it also has guides to the proper use of a lot of the java standard classes.
A note inside the book really draw my attention; in item 8, chapter 3, Wesley was talking about overriding the hashCode method, he stated an example of a phone number class and an implementation of a suggested hashCode, then he said "Writing such hash functions is a topic of active research and an activity best left to mathematicians and theoretical computer scientists.". Wesley encourages his readers to use the state-of-art code. that's the goal of the book.
Actually i was thinking about the classes of the open source libraries we use as i proceeded reading the book. the kind of code that shall be used by thousands of programmers all around the world.
programming by nature is very flexible. you have many choices. it is like creating a statue using clay. You choose at every point; Class names, methods and variable names. public methods, internal implementation algorithms, structure of the package classes,.. all of those and others are left for the programmer to choose.
Effective Java programming comes to introduce a lot of the best practices for Java programmers; to enhance the stability, readability, clarity, reusability and maintainability of their code. it also has guides to the proper use of a lot of the java standard classes.
A note inside the book really draw my attention; in item 8, chapter 3, Wesley was talking about overriding the hashCode method, he stated an example of a phone number class and an implementation of a suggested hashCode, then he said "Writing such hash functions is a topic of active research and an activity best left to mathematicians and theoretical computer scientists.". Wesley encourages his readers to use the state-of-art code. that's the goal of the book.
Actually i was thinking about the classes of the open source libraries we use as i proceeded reading the book. the kind of code that shall be used by thousands of programmers all around the world.
Wednesday, May 23, 2007
Bullet Proof code using RegExp.
Regular expressions is a great way to find those hard to find strings.
Suppose you have a bunch of old code and you want to bullet proof it or you might be interested in auditing it and for starters you want to find those uninitialized variables using eclipse.
the pattern of uninitialized variable could be in the following format
(Var)(space or more)(Alphanumeric word)(Possible space or more)(semi-colon)
to search for (space or more) we use expression
(\s+)
\s means space
+ means one or more
to search for(Alphanumeric word) we use expression
(\w+)
\w means Alphanumeric character
+ means one or more
to search for (Possible space or more) we use expression
(\s*)
\s means space
* means zero or more
so the regular expression would be int(\s+\w+\s*); it will return all uninitialized ints.
It would be great if we collect those regular expressions and keep them in a library to be our arsenal towards bad code.
do u have more regular expressions to share?
Suppose you have a bunch of old code and you want to bullet proof it or you might be interested in auditing it and for starters you want to find those uninitialized variables using eclipse.
the pattern of uninitialized variable could be in the following format
(Var)(space or more)(Alphanumeric word)(Possible space or more)(semi-colon)
to search for (space or more) we use expression
(\s+)
\s means space
+ means one or more
to search for(Alphanumeric word) we use expression
(\w+)
\w means Alphanumeric character
+ means one or more
to search for (Possible space or more) we use expression
(\s*)
\s means space
* means zero or more
so the regular expression would be int(\s+\w+\s*); it will return all uninitialized ints.
It would be great if we collect those regular expressions and keep them in a library to be our arsenal towards bad code.
do u have more regular expressions to share?
Thursday, May 17, 2007
Hi, I'm Ruby on Rails - Part 1
What do you get when you cross the Mac vs PC commercials and Rails Envy? Ruby on Rails ads to get everyone hyped for Railsconf, that's what!
Subscribe to:
Posts (Atom)