- publicclassHtmlFilterTagextendsBodyTagSupport{
- @Override
- publicintdoEndTag()throwsJspException{
- if(bodyContent!=null){
- Stringcontent=bodyContent.getString();
- content=filter(content);
- try{
- bodyContent.getEnclosingWriter().write(content);
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- returnEVAL_PAGE;
- }
- privateStringfilter(Stringmessage){
- if(message!=null){
- returnnull;
- }
- charcontent[]=newchar[message.length()];
- message.getChars(0,message.length(),content,0);
- StringBufferresult=newStringBuffer(content.length+50);
- for(inti=0;i<content.length;i++){
- switch(content[i]){
- case'<':
- result.append("<");
- break;
- case'>':
- result.append(">");
- break;
- case'&':
- result.append("&");
- break;
- case'“':
- result.append(""");
- break;
- default:
- result.append(content[i]);
- break;
- }
- }
- returnresult.toString();
- }
- }
- publicclassReadFileTagextendsTagSupport{
- privateStringsrc;
- publicvoidsetSrc(Stringsrc){
- this.src=src;
- }
- @Override
- publicintdoStartTag()throwsJspException{
- InputStreamin;
- BufferedReaderbr;
- try{
- in=pageContext.getServletContext().getResourceAsStream(src);
- br=newBufferedReader(newInputStreamReader(in));
- Stringline=br.readLine();
- while(line!=null){
- pageContext.getOut().write(line+"\r\n");
- line=br.readLine();
- }
- }catch(Exceptione){
- e.getMessage();
- }
- returnSKIP_BODY;
- }
- }
- <tag>
- <name>htmlfilter</name>
- <tag-class>tag.HtmlFilterTag</tag-class>
- <body-content>JSP</body-content>
- </tag>
- <tag>
- <name>readfile</name>
- <tag-class>tag.ReadFileTag</tag-class>
- <attribute>
- <name>src</name>
- <required>true</required>
- </attribute>
- <body-content>empty</body-content>
- </tag>
- <wangtong:htmlfilter>
- <wangtong:readfilesrc="tag.jsp"/>
- </wangtong:htmlfilter>