使用标签对HTML特殊字符进行转义

Java代码
  1. publicclassHtmlFilterTagextendsBodyTagSupport{
  2. @Override
  3. publicintdoEndTag()throwsJspException{
  4. if(bodyContent!=null){
  5. Stringcontent=bodyContent.getString();
  6. content=filter(content);
  7. try{
  8. bodyContent.getEnclosingWriter().write(content);
  9. }catch(IOExceptione){
  10. //TODOAuto-generatedcatchblock
  11. e.printStackTrace();
  12. }
  13. }
  14. returnEVAL_PAGE;
  15. }
  16. privateStringfilter(Stringmessage){
  17. if(message!=null){
  18. returnnull;
  19. }
  20. charcontent[]=newchar[message.length()];
  21. message.getChars(0,message.length(),content,0);
  22. StringBufferresult=newStringBuffer(content.length+50);
  23. for(inti=0;i<content.length;i++){
  24. switch(content[i]){
  25. case'<':
  26. result.append("&lt;");
  27. break;
  28. case'>':
  29. result.append("&gt;");
  30. break;
  31. case'&':
  32. result.append("&amp;");
  33. break;
  34. case'“':
  35. result.append("&quot;");
  36. break;
  37. default:
  38. result.append(content[i]);
  39. break;
  40. }
  41. }
  42. returnresult.toString();
  43. }
  44. }
  45. publicclassReadFileTagextendsTagSupport{
  46. privateStringsrc;
  47. publicvoidsetSrc(Stringsrc){
  48. this.src=src;
  49. }
  50. @Override
  51. publicintdoStartTag()throwsJspException{
  52. InputStreamin;
  53. BufferedReaderbr;
  54. try{
  55. in=pageContext.getServletContext().getResourceAsStream(src);
  56. br=newBufferedReader(newInputStreamReader(in));
  57. Stringline=br.readLine();
  58. while(line!=null){
  59. pageContext.getOut().write(line+"\r\n");
  60. line=br.readLine();
  61. }
  62. }catch(Exceptione){
  63. e.getMessage();
  64. }
  65. returnSKIP_BODY;
  66. }
  67. }
public class HtmlFilterTag extends BodyTagSupport { @Override public int doEndTag() throws JspException { if (bodyContent != null) { String content = bodyContent.getString(); content = filter(content); try { bodyContent.getEnclosingWriter().write(content); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return EVAL_PAGE; } private String filter(String message) { if (message != null) { return null; } char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuffer result = new StringBuffer(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("&lt;"); break; case '>': result.append("&gt;"); break; case '&': result.append("&amp;"); break; case '“': result.append("&quot;"); break; default: result.append(content[i]); break; } } return result.toString(); } } public class ReadFileTag extends TagSupport { private String src; public void setSrc(String src) { this.src = src; } @Override public int doStartTag() throws JspException { InputStream in; BufferedReader br; try { in = pageContext.getServletContext().getResourceAsStream(src); br = new BufferedReader(new InputStreamReader(in)); String line = br.readLine(); while (line != null) { pageContext.getOut().write(line + "\r\n"); line = br.readLine(); } } catch (Exception e) { e.getMessage(); } return SKIP_BODY; } }
Html代码
  1. <tag>
  2. <name>htmlfilter</name>
  3. <tag-class>tag.HtmlFilterTag</tag-class>
  4. <body-content>JSP</body-content>
  5. </tag>
  6. <tag>
  7. <name>readfile</name>
  8. <tag-class>tag.ReadFileTag</tag-class>
  9. <attribute>
  10. <name>src</name>
  11. <required>true</required>
  12. </attribute>
  13. <body-content>empty</body-content>
  14. </tag>
<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>


Html代码
  1. <wangtong:htmlfilter>
  2. <wangtong:readfilesrc="tag.jsp"/>
  3. </wangtong:htmlfilter>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值