1 3 package jodd.servlet; 4 5 import jodd.util.StringUtil; 6 7 import java.io.IOException ; 8 import java.lang.reflect.Method ; 9 10 import javax.servlet.ServletException ; 11 import javax.servlet.http.HttpServlet ; 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 15 45 public abstract class ServletAction extends HttpServlet { 46 47 49 public final static int HTTP_METHOD_UNKNOWN = 0; 50 public final static int HTTP_METHOD_GET = 1; 51 public final static int HTTP_METHOD_POST = 2; 52 53 private int method = HTTP_METHOD_UNKNOWN; 54 55 56 59 public int getHttpMethod() { 60 return method; 61 } 62 63 66 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 67 method = HTTP_METHOD_GET; 68 doRequest(request, response); 69 } 70 71 74 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 75 method = HTTP_METHOD_POST; 76 doRequest(request, response); 77 } 78 79 82 protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 83 } 84 85 87 90 public boolean forward(HttpServletRequest request, HttpServletResponse response, String url) throws IOException , ServletException { 91 return DispatcherUtil.forward(request, response, url); 92 } 93 94 100 public boolean forwardParam(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 101 String url = request.getParameter("forward"); 102 if (url != null) { 103 return forward(request, response, url); 104 } 105 return false; 106 } 107 108 115 public boolean forwardParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException , ServletException { 116 String url = request.getParameter("forward-" + s); 117 if (url != null) { 118 return forward(request, response, url); 119 } 120 return false; 121 122 } 123 124 125 127 130 protected static void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { 131 DispatcherUtil.redirect(request, response, url); 132 } 133 134 140 protected static boolean redirectParam(HttpServletRequest request, HttpServletResponse response) throws IOException { 141 String url = request.getParameter("redirect"); 142 if (url != null) { 143 redirect(request, response, url); 144 return true; 145 } 146 return false; 147 148 } 149 150 156 protected static boolean redirectParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException { 157 String url = request.getParameter("redirect-" + s); 158 if (url != null) { 159 redirect(request, response, url); 160 return true; 161 } 162 return false; 163 164 } 165 166 167 168 169 171 178 public String invokeActionParam(HttpServletRequest request, HttpServletResponse response) throws ServletException { 179 String actionName = request.getParameter("action"); 180 return invokeActionParam(this, request, response, actionName); 181 } 182 183 189 public String invokeActionParam(Object servlet, HttpServletRequest request, HttpServletResponse response, String actionName) throws ServletException { 190 if (actionName == null) { 191 return null; 192 } 193 try { 194 Method m = servlet.getClass().getMethod(actionName, new Class [] {HttpServletRequest .class, HttpServletResponse .class}); 195 Object result = m.invoke(servlet, new Object [] {request, response}); 196 return StringUtil.toString(result); 197 } catch (Exception ex) { 198 throw new ServletException ("Invocation error of action '" + actionName + "': " + ex.getMessage(), ex); 199 } 200 } 201 202 203 205 208 public static boolean include(HttpServletRequest request, HttpServletResponse response, String page) throws IOException , ServletException { 209 return DispatcherUtil.include(request, response, page); 210 } 211 212 public static boolean includeParam(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 213 String url = request.getParameter("include"); 214 if (url != null) { 215 return include(request, response, url); 216 } 217 return false; 218 } 219 220 public static boolean includeParam(HttpServletRequest request, HttpServletResponse response, String includeName) throws IOException , ServletException { 221 String url = request.getParameter("include-" + includeName); 222 if (url != null) { 223 return include(request, response, url); 224 } 225 return false; 226 } 227 228 229 } 230 | Popular Tags |