Tuesday, February 5, 2008

One template file for all pages using Servlets and JSP

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

44 comments:

david santos said...

Congratulations Egypt!!!!!!!!!!

Jason Lee said...

Hi,

Anyone knows how i could contact Hassan Wassel? I try to email him but didnt get any response?

Could anyone get back to me at
huajie.lee@gmail.com?


Thanks alot.

محبة الرحمن said...

شكرا جزيلا

SorayaDulaney said...

成人圖片msn成人祼體圖片成人的做愛圖情慾圖庫情愛圖片情趣圖片情色漫畫圖情色日本帖圖情色帖圖情色圖庫情色合成圖片情色合成圖情色全裸圖情色試看圖情色鉆圖免費無碼寫真免費無碼a圖免費無碼a片線上觀看免費美女影片免費美女寫真影片免費美女寫真圖片免費美女寫真短片免費美女寫真桌布免費美女裸體寫真免費美女圖片下載免費美女短片免費看鋼管秀影片免費看線上成人影片免費看線上成人片免費看線上卡通動漫限制級影片介紹哈拉色情遊戲網哈啦免費遊戲網麗的情色線上小遊戲麗的性愛小遊戲麗的圾遊戲麗的免費情色遊戲麗的色情小遊戲的美少女少女麗的色情小遊戲麗的色情遊戲區麗的色情遊戲麗的色遊戲網麗的美女遊戲麗的線上小遊戲麗的線上色情遊戲

傷口很殺 said...

如果你不思考未來,你便不會有未來..................................................

香蕉哥哥 said...

I love readding, and thanks for your artical. ........................................

馨裕 said...

dvd 日本色情片新做愛色情片做愛色情影片免費色情論壇色情網小說尋夢中部聊天尋夢中部聊天室尋夢元聊天是尋夢園大學生尋夢園聊天館尋夢園遊戲尋夢聯盟廁所盜攝免費看援交中壢援交平台援高雄晴色小說無碼日本女優甜心寶貝貼影片區情色大全情色a影片情色人成情色小文學情色小說上看情色小說成人學情色小說圖貼情色小舖情色文張情色文章sex情色文學頁gogogirl視訊美女 網路小說 情色論壇聊天室ut

葉雅慧 said...

認識自己,是發現妳的真性格、掌握妳的命運、創照你前程的根源。 ..................................................

彭昱宏 said...

凡是遇到困擾的問題,不要把它當作可怕的,討厭的,無奈的遭遇,而要把它當作歷練、訓練和幫助。......................................................

RexTemples20144 said...

adult成人情色激情成人聊天網情色影片色情俱樂部性愛影片情趣淫水影音情色限制級照片live show女生自慰影片超屌成人情色留言板淫亂女自慰免費視訊辣妹巨乳鹹濕色情影音聊天自拍走光照片免費情色av圖片亞洲情色論壇淫慾免費裸女圖片裙底風光台灣色情論壇一夜正妹淫娃網情色下載a圖片全裸淫蕩女人情色影音聊天高潮自慰色情圖片成人自拍女生自慰亞洲成人色情dvd舔乳頭女生自慰影片台灣色情網站台灣性樂園sex story情色影音激情聊天室嘿咻情色自拍

PorshaCoghlan梁子珠 said...

thank u........................................

Stephany9991 said...

A good medicine tastes bitter. ....................................................

俊翔俊翔 said...

文章很棒~感謝!!..................................................

Glennie9654 said...

ut聊天77p2p85cc85st85街視訊視訊聊天ava片a片下載成人情色色情影音視訊聊天洪爺影城洪爺免費視訊免費a片免費一對多utsogo論壇ut聊天室成人片免費看................

韋于倫成 said...

凡走過必留下痕跡!不留言對不起你!........................................

韋于倫成 said...

來拜訪你囉~期待你的下次文章~加油^^.........................

呈均 said...

Constant dripping wears away the stone. 滴水穿石!加油!..................................................

東芳 said...

台灣夫妻自拍做愛自拍照片打炮自拍貼圖夫妻自拍片免費a動畫交友內容同志聊天室ut同志線上看吊帶襪美女圖后工后宮無碼圭賢七年間的愛歌詞圭賢圖片好色貼片區如何延遲射精如何做愛持久如何做愛最爽如何讓女生舒服宅女的幸福生活同志性愛片同志色網站同志色文學交援妹伊莉討論區小說伊莉貼圖區伊莉影片網伊莉論台伊葉情貼片區伊葉情貼圖視訊聊天室 免費視訊 pc交友4u成人

于呈均名 said...

your english is incredible............................................................

佩GailBohanan1蓉 said...

Learning makes a good man better and ill man worse.............................................................

陳卓人 said...

成熟,就是有能力適應生活中的模糊。..................................................

曉薇 said...

Many a little makes a mickle...................................................................

德倫 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。......................................................................

江婷 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。......................................................................

萱祥 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

吳婷婷 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

宥妃宥妃 said...

睇完之後覺得有d頓悟..感謝分享...................................................................

峻帆峻帆峻帆 said...

這麼用心的經營你的文章, 當然值得我們留連拜訪的!............................................................

tuckerdarrell翁tuckerdarrell怡婷 said...

晚上好,很喜歡你的blog哦...............................................................

姿柯瑩柯dgdd憶曾g智曾 said...

我在戀愛著?--------是的,因為我在等待著.................................................................

祐音祐音 said...

你的努力我們都看見了--支持你............................................................

王美妹 said...

一時的錯誤不算什麼,錯而不改才是一生中永遠且最大的錯誤............................................................

ju吳phe宇te佳ns said...

文章是心情的反應~~祝妳天天寫的都是讓人開心的好文章哦!!...............................................................

夏瓊陳詩蓁富 said...

這個時代,不缺乏感傷,但缺乏反思~~希望能多看到值得思考的文章!............................................................

蔡苡玄 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

謝翁穎翰毓珍 said...

我從來不認為不同意我的看法就是冒犯................................................

家唐銘 said...

人生像一杯茶,若一飲而盡,會提早見到杯底..................................................

敬周喜 said...

成熟,就是有能力適應生活中的模糊。............................................................

蕙陳春宗陳穎 said...

人生是故事的創造與遺忘。............................................................

法榮陳法榮陳法榮陳 said...

讓好心情回味發酵;壞心情留在文字裡隨時間消逝吧!............................................................

國林林林林維 said...

善言能贏得聽眾,善聽才能贏得朋友。......................................................................

翊翊翊翊張瑜翊翊翊 said...

若有人問你成功時會不會記得他 試問若你失敗時他會不會記得你......................................................................

瑰潼 said...

辛苦了!祝你愈來愈好!............................................................

王怡迪 said...

良言一句三冬暖,惡語傷人六月寒。............................................................