posts - 51, comments - 17, trackbacks - 0, articles - 9
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          第一個自定義標簽的實現

          Posted on 2007-06-26 20:04 chenweicai 閱讀(295) 評論(0)  編輯  收藏

          步一:定義標簽

          步二:創建標簽庫描述器TLD diagnostics.tld, 將它放在WEB-INF目錄下的tlds文件夾下,diagnostics.tld如下:

          <?xml version="1.0"?>
          <taglib>
           <tlibversion>1.0</tlibversion>
           <jspversion>1.1</jspversion>
           <shortname>diag</shortname>
           <tag>
             <name>getWebServer</name>
             <tagclass>servlet.GetWebServerTag</tagclass>
             <bodycontent>empty</bodycontent>
           </tag>
          </taglib>

          步三:編寫標簽處理器 GetServerTag.java

          package servlet;

          import java.io.IOException;
          import java.net.HttpURLConnection;
          import java.net.URL;
          import java.net.URLConnection;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspWriter;
          import javax.servlet.jsp.tagext.TagSupport;

          public class GetWebServerTag extends TagSupport {

           @Override
           public int doStartTag() throws JspException {
            try{
             // get the request object from the page context
             HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
             
             // Request information form the web server
             URL url = new URL("http", request.getServerName(), request.getServerPort(), "/");
             URLConnection con = url.openConnection();
             ((HttpURLConnection)con).setRequestMethod("OPTIONS");
             String webserver = con.getHeaderField("server");
             
             // write it to the output stream
             JspWriter out = pageContext.getOut();
             out.print(webserver);
            }catch (IOException e)
            {
             throw new JspException(e.getMessage());
            }
            
            return SKIP_BODY;
           }
           
          }


          步四: 編寫WEB.xml文件

          加入:
          <jsp-config>
          <taglib>
          <taglib-uri>diagnostics</taglib-uri>
          <taglib-location>/WEB-INF/tlds/diagnostics.tld</taglib-location>
          </taglib>
          </jsp-config>

          步五:編寫jsp頁面,將標簽并入該JSP頁面

          <%@ taglib prefix="diag" uri="diagnostics"%>
             
          <html>
          <head>
          <title>Basci Example of a Custom Tag</title>
          </head>
          <body>
          <H3>Basci Example of a Custom Tag</H3>
           The web server is <diag:getWebServer/>
          </body>
          </html>


          OK  , so much so this ,


          wish you success

          <注: 注意紅色的地方要一致哦>


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 江华| 万安县| 昌都县| 安康市| 慈利县| 克拉玛依市| 万荣县| 彩票| 唐山市| 林周县| 汝阳县| 呼和浩特市| 象州县| 全州县| 启东市| 宝鸡市| 昌邑市| 靖边县| 应城市| 读书| 湘潭县| 定边县| 绍兴县| 双流县| 建湖县| 屏南县| 西畴县| 长岭县| 什邡市| 蓝田县| 额敏县| 读书| 邢台市| 普兰店市| 菏泽市| 邳州市| 鄂尔多斯市| 浦县| 赣州市| 霍州市| 安宁市|