public class ServiceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=GB18030");
request.setCharacterEncoding("GB18030");
response.setCharacterEncoding("GB18030");
PrintWriter out = response.getWriter();
DofContext.setRequest(request);
DofContext.setResponse(response);
// 根据url得到actionName
String actionName = getActionName(request);
String methodname = request.getParameter("action");
// 执行action中的方法
DofInvocation invocation = new DofInvocation(actionName);
String resultString ="";
try {
resultString = invocation.execute(methodname);
} catch (Exception e) {
e.printStackTrace();
}
// 返回结果
out.print(resultString);
}
//分析uri得到action的name
private String getActionName(HttpServletRequest request){
String uri=request.getRequestURI();
String actionName=uri.substring(uri.lastIndexOf("/")+1 , uri.indexOf(".do"));
return actionName;
}
//把配置文件的map保存在Context里面
public void init(ServletConfig config) throws ServletException {
ServletContext context=config.getServletContext();
Map<String ,Configuration> configMap=new HashMap<String, Configuration>();
try {
configMap=ParseDofConfFile.convertPropertyFileToMap();
} catch (IOException e) {
e.printStackTrace();
}
DofContext.setServletContext(context);
DofContext.setConfigurationMap(configMap);
System.out.println("===DOF初始化完毕====");
}
}
解析配置文件
public class ParseDofConfFile {
public static String ACTION_CONF_FILE = "DofConf.xml";
private static final String S_Action = "action";
private static final String S_Mapping = "class-mapping";
private static final String S_ClassName = "class-name";
/**
* 解析DofConf.xml文件,将解析出来的数据组装成configuration对象,并放入map中
*/
@SuppressWarnings("unchecked")
public static Map<String, Configuration> convertPropertyFileToMap()
throws IOException {
Map<String, Configuration> actionResultMap = new HashMap<String, Configuration>();
InputStream fs = null;
try {
fs = ParseDofConfFile.class.getClassLoader().getResourceAsStream(
ACTION_CONF_FILE);
if (fs != null) {
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(fs);
if (doc != null) {
Element root = doc.getRootElement();
if (root != null) {
//取得action节点,并遍历它的只节点
List actionlist = root.elements(S_Action);
if (actionlist != null)
{
Iterator it = actionlist.iterator();
while (it.hasNext())
{
Element actionele = (Element) it.next();
if (actionele != null)
{
//得到class-mapping 和class-name 节点
Element mapele = actionele.element(S_Mapping);
Element nameele = actionele.element(S_ClassName);
//如果节点不为空,将其值组装成Configuration对象放入map中
if (mapele != null && nameele != null)
{
String actionName = mapele.getText();
String classname = nameele.getText();
Configuration conf = new Configuration();
conf.setActionName(actionName);
conf.setClassName(classname);
actionResultMap.put(actionName, conf);
}
}
}
}
}
}
}
}
catch (Exception e) {
throw new IOException("解析DofConfig.xml文件出错或文件不存在!");
} finally {
if (fs != null) {
fs.close();
fs = null;
}
}
return actionResultMap;
}
}
configuration对象
public class Configuration {
//请求的action名称
private String actionName;
//action对应的类名
private String className;
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String toString(){
return "actionName="+this.actionName+";classname="+this.className;
}
}
dofContext
public class DofContext {
//save actioncontext to a map
private static Map<String,Object> context=new HashMap<String, Object>();
private DofContext(){
}
public static Map<String, Object> getContext() {
return context;
}
public static HttpServletRequest getRequest(){
return (HttpServletRequest)getContext().get(ContextConstant.HTTP_REQUEST);
}
public static void setRequest(HttpServletRequest request){
getContext().put(ContextConstant.HTTP_REQUEST, request);
}
public static HttpServletResponse getResponse(){
return (HttpServletResponse)getContext().get(ContextConstant.HTTP_RESPONSE);
}
public static void setResponse(HttpServletResponse response){
getContext().put(ContextConstant.HTTP_RESPONSE, response);
}
public static void setServletContext(ServletContext servletContext){
getContext().put(ContextConstant.SERVLET_CONTEXT, servletContext);
}
public static ServletContext getServletContext(){
return (ServletContext)getContext().get(ContextConstant.SERVLET_CONTEXT);
}
public static void setConfigurationMap(Map<String,Configuration> configMap){
getContext().put(ContextConstant.ACTION_CONFIG_MAP, configMap);
}
@SuppressWarnings("unchecked")
public static Map<String,Configuration> getConfigurationMap(){
return (Map<String,Configuration>)getContext().get(ContextConstant.ACTION_CONFIG_MAP);
}
}
/**
* 通过反射机制调用action中的方法
* @author dengm
*
*/
public class DofInvocation {
private String actionName;
public DofInvocation(String actionName) {
this.actionName = actionName;
}
// 执行action中的方法
@SuppressWarnings("unchecked")
public String execute(String methodName) throws Exception {
Configuration config = getActionConfiguration();
if (null != config) {
String className = config.getClassName();
if (null == methodName || "".equals(methodName)) {
methodName = "execute";
}
try {
Class actionClass = Class.forName(className);
Object action = actionClass.newInstance();
Method method = action.getClass().getMethod(methodName, null);
String result = (String) method.invoke(action, null);
return result;
} catch (ClassNotFoundException e) {
throw new Exception("配置文件中没有找到类:" + className);
} catch (InstantiationException e) {
} catch (NoSuchMethodException e) {
throw new Exception("类 :" + className + "中没有相应的方法 >>>"
+ methodName);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
return ContextConstant.ACTION_ERROR;
}
/*
* 得到一个action的configuration配置对象
*/
@SuppressWarnings("unchecked")
public Configuration getActionConfiguration() {
Configuration config = new Configuration();
Map<String, Configuration> configMap = (Map<String, Configuration>) DofContext
.getConfigurationMap();
if (null != configMap) {
config = configMap.get(actionName);
}
return config;
}
}