package action;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 这个类用来处理Dispatch(分发)的实现
* @author Administrator
* 所有的servlet继承此servlet
*/
public class DispatchAction extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
dispachMethod(request,response);
}
/**
* 用来执行具体的请求操作
*/
public void dispachMethod(HttpServletRequest request,
HttpServletResponse response)
{
String methodName = request.getParameter("method");
Class dispachAction = this.getClass();
try
{
Class[] parametersTypes = new Class[]{HttpServletRequest.class,HttpServletResponse.class};
Method method = dispachAction.getMethod(methodName, parametersTypes);
Object[] parameterVlaues = new Object[]{request,response};
method.invoke(this, parameterVlaues);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*request.setAttribute("errorMessage", e.getMessage());
String classname = e.getTargetException().getClass().getSimpleName();
System.out.println("aaaaaaaaaaaaaaaaaa"+classname);*/
e.printStackTrace();
}
}
}