package com.iyunti.core;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class Dispatcher extends Controller {
private static final long serialVersionUID = 8396066198989032944L;
private ServletContext servletContext = null;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* ApplicationContext act = new ClassPathXmlApplicationContext(
* "applicationContext.xml"); DataSource ds = (DataSource)
* act.getBean("dataSource");
*
* JdbcTemplate template = new JdbcTemplate(ds); String sql =
* "select count(*) from T_ACCOUNT";
*
* int totalRecord = template.queryForInt(sql);
*
* new Product().doGet(request, response);
*/
// Map<String, Class<?>> classMap = (Map<String, Class<?>>)
// servletContext
// .getAttribute("servletClassMap");
String uri = getUriString(request);
// 获取要请求的servlet路径
String requestServletName = "com.iyunti.ec." + getActionString(uri);
// 创建类的实例
Object obj = null;
Class<?> clz = null;
Method targetMethod = null;
try {
// 获取要使用的Servlet类
clz = Class.forName(requestServletName);
if(clz.equals(null)){
logger.error("Servlet类不存在");
}
obj = clz.newInstance();
// 获取Servlet类的doGet方法
targetMethod = clz.getDeclaredMethod("doGet",
HttpServletRequest.class, HttpServletResponse.class);
// 调用对象的方法进行处理
targetMethod.invoke(obj, request, response);
} catch (Exception e) {
logger.error(e.getMessage());
}
loadJsp(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}