使用CoreServlet减少代码编写

使用servlet时可以利用CoreServlet减少代码的编写量

CoreServlet.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.imageio.stream.FileImageInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CoreServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 411450637985944586L;
	Properties po = new Properties();
	
	public void init() throws ServletException {
		
		System.out.println("启动主控制器");
		String path = this.getServletContext().getRealPath("//WEB-INF//classes//config.properties");
		try {
			FileInputStream fis = new FileInputStream(path);
			po.load(fis);
		} catch (Exception e) {
			System.out.println("核心Servlet中属性文件加载异常");
		}
		
		
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		//从request中获取uri,从uri中提取key,再通过key和config.properties解析请求
		String uri = request.getRequestURI();
		String  key = uri.substring(1).substring(uri.substring(1).indexOf("/")+1,uri.substring(1).lastIndexOf(".action"));
		String[] values = null;
		String classpath = "";
		String methods = "";
		String response_type = "";
	    String responsepath = "";
	    
		Map<String,Object> context = new HashMap<String, Object>();
		context.put("javax.servlet.http.HttpServletRequest", request);
		context.put("javax.servlet.http.HttpServletResponse", response);
		//创建ActionContext对像
		ActionContext actionContext = new ActionContext(context);
		//将ActionContext对象设置到本地线程变量 ThreadLocal 中
		ActionContext.setActionContext(actionContext);

	    try{
	    	String value =	po.getProperty(key);
		    values = value.split(",");
		    classpath =values[0];//类路径
		    methods = values[1];
		    response_type = values[2];//响应类型
	    	String method_name = methods.substring(0,methods.indexOf("("));  //方法名字	
		    String bean_path = methods.substring(methods.indexOf("(")+1,methods.lastIndexOf(")"));//参数类路径
		    //通过类路径,获取类对象,创建类的对象
		    Class cl= Class.forName(classpath);  
		    Object obj =cl.newInstance();
		    
		    if(bean_path.equalsIgnoreCase("")||bean_path==null){
		    	//通过类对象和方法名,获取方法
		    	Method meth = cl.getMethod(method_name);
		    	//调用方法,返回参数
		   		responsepath =(String)meth.invoke(obj);
		    	
		    }else{
		    	
		    	Class cls = Class.forName(bean_path);
		    	Object beans = cls.newInstance();
		    	
		    	//获取所有提交的参数
		    	Map<String,String[]>  map  =request.getParameterMap();
		    	//获取参数bean的所有属性
		    	Field[] fie =cls.getDeclaredFields();
		    	
		    	
		    	//将获取的参数绑定到beans属性上
		    	for(int i=0;i<fie.length;i++){
		    		fie[i].setAccessible(true);
		    		//通过属性名,获取页面提交的属性值
		    		String[] kt =map.get(fie[i].getName());
		    		if(kt!=null){
		    			String type =fie[i].getType().getName();
		    			//System.out.println(type);
		    			if(type.equalsIgnoreCase("int")){
		    				fie[i].set(beans, Integer.parseInt(kt[0]));
		    				
		    			}else if(type.equals("java.lang.String")){
		    				
		    				fie[i].set(beans, kt[0]);				
		    			}    	
		    		}
		    	}
		    	
		    	Method meth = cl.getMethod(method_name,cls);
		    	responsepath = (String)meth.invoke(obj, beans);
		    	
		    }
		    
		    if(response_type.equals("forword")){
		    	
		    	request.getRequestDispatcher(responsepath).forward(request, response);
		    	
		    }else if("redirect".equals(response_type)){
		    	
		    	response.sendRedirect(responsepath);
		    }
		
	    }catch(Exception e){
	    	e.printStackTrace();
	    	request.getRequestDispatcher("error.html").forward(request, response);
	    }

	}

}

跳转配置文件config.properties

##CoreServlet Configuration
## RequestPath=ClassPath,MethodName
customer_info=com.group.busicontrol.CustomerAction,selectCustomer(),forword
customer_dele=com.group.busicontrol.CustomerAction,deleCustomer(com.group3.bean.CustomerBean),redirect
customer_into=com.group.busicontrol.CustomerAction,insertCustomer(com.group3.bean.CustomerBean),redirect

web.xml配置:

 <servlet>
    <servlet-name>CoreServlet</servlet-name>
    <servlet-class>com.group.controller.CoreServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>CoreServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值