KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > IncludeTag


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.utils;
14
15 import java.io.IOException JavaDoc;
16
17 import javax.servlet.RequestDispatcher JavaDoc;
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
22
23 import org.apache.log4j.Logger;
24
25 /**
26  * Includes a jsp if (and
27  * only if) there is a particular http parameter in the current request.
28  * <p>
29  * Example usage: httpParam = "query". If a request
30  * with parameter "?query=test" is received, this tag will
31  * include "/WEB-INF/test.jsp"
32  */

33
34 public class IncludeTag extends TagSupport JavaDoc {
35   String JavaDoc httpParam = "include";
36   String JavaDoc prefix = "/WEB-INF/";
37   String JavaDoc suffix = ".jsp";
38   private static Logger logger = Logger.getLogger(IncludeTag.class);
39
40   public String JavaDoc getHttpParam() {
41     return httpParam;
42   }
43
44   public void setHttpParam(String JavaDoc httpParam) {
45     this.httpParam = httpParam;
46   }
47
48   public int doStartTag() throws JspException JavaDoc {
49     try {
50       logger.info("enter");
51       HttpServletRequest JavaDoc hsr = (HttpServletRequest JavaDoc) pageContext.getRequest();
52
53       String JavaDoc name = hsr.getParameter(httpParam);
54       if (name != null) {
55         // the parameter was specified, now we include the jsp
56
String JavaDoc uri = prefix + name + suffix;
57         logger.info("including " + uri);
58         RequestDispatcher JavaDoc rd = pageContext.getRequest().getRequestDispatcher(uri);
59         rd.include(pageContext.getRequest(), pageContext.getResponse());
60       }
61       logger.info("leave");
62       return SKIP_BODY;
63     } catch (ServletException JavaDoc e) {
64       logger.error("exception caught", e);
65       throw new JspException JavaDoc(e);
66     } catch (IOException JavaDoc e) {
67       logger.error("exception caught", e);
68       throw new JspException JavaDoc(e);
69     }
70   }
71
72   public String JavaDoc getPrefix() {
73     return prefix;
74   }
75
76   public String JavaDoc getSuffix() {
77     return suffix;
78   }
79
80   public void setPrefix(String JavaDoc prefix) {
81     this.prefix = prefix;
82   }
83
84   public void setSuffix(String JavaDoc suffix) {
85     this.suffix = suffix;
86   }
87
88 }
89
Popular Tags