1 16 17 package org.apache.taglibs.response; 18 19 import java.io.*; 20 import java.lang.*; 21 import java.lang.reflect.*; 22 import java.util.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 import javax.servlet.jsp.*; 26 import javax.servlet.jsp.tagext.*; 27 28 60 61 public class SendErrorTag extends BodyTagSupport 62 { 63 private String error; 64 private boolean reset = false; 65 66 71 public final int doStartTag() throws JspException 72 { 73 return EVAL_BODY_TAG; 74 } 75 76 81 public final int doAfterBody() throws JspException 82 { 83 int error_code = 0; 85 BodyContent body = getBodyContent(); 86 String s = body.getString().trim(); 87 body.clearBody(); 90 91 if( reset ) 92 pageContext.getResponse().reset(); 93 94 try { 95 HttpServletResponse resp = null; 96 Field ec = null; 97 resp = (HttpServletResponse)pageContext.getResponse(); 98 Class rc = resp.getClass(); 99 ec = rc.getField(error); 100 error_code = ec.getInt(resp); 101 } catch(Exception e) { 102 throw new JspException( 103 "Response sendError tag could not find error code: " + error); 104 } 105 106 try { 107 if( s.length() > 0 ) 108 ((HttpServletResponse)pageContext.getResponse()).sendError(error_code,s); 109 else 110 ((HttpServletResponse)pageContext.getResponse()).sendError(error_code); 111 } catch(IOException e) { 112 throw new JspException( 113 "Response sendError tag failed: " + e.getMessage()); 114 } catch(IllegalStateException e) { 115 throw new JspException( 116 "Response sendError tag failed " + e.getMessage()); 117 } 118 119 return SKIP_BODY; 120 } 121 122 128 public final int doEndTag() throws JspException 129 { 130 return SKIP_PAGE; 131 } 132 133 142 public final void setError(String err) 143 { 144 error = err; 145 } 146 147 153 public final void setReset(boolean reset) 154 { 155 this.reset = reset; 156 } 157 } 158 | Popular Tags |