⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sevlet.java

📁 B2C的J2EE开发的 MVC +DAO 设计
💻 JAVA
字号:
package ldsj.gjm.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ldsj.gjm.form.*;
import ldsj.gjm.action.ActionFather;
import ldsj.gjm.tools.hashMapping;

public class sevlet extends HttpServlet 
{
    private HashMap map=new HashMap();
	
	public Method[] getMehodName(FormFather formfather)
	{
			Method[] aa=null;
			Class cs=formfather.getClass();
			aa=cs.getDeclaredMethods();
			return  aa;
			 
    }
	public sevlet() 
	{
		super();
	}
	public FormFather getFormInstance(String key)
	{
	   System.out.println("getFormInstance");	
		FormFather formfather=null;
		
		try {
			formfather=(FormFather)Class.forName(key).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return formfather;
	}
	public ActionFather getActionInstance(String key)
	{
		System.out.println("getActionInstance");
		System.out.println("actionkey"+key);
	  
		ActionFather actionfather=null;
		try {
			actionfather=(ActionFather)Class.forName(key).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return actionfather;
	}

	public void destroy() 
	{
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

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

		 doPost(request,response);
	}
	

	
	public  void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
  {
		FormFather formfather=null;
		//得到对应的form和action
	String path=request.getRequestURI().toString();
	path=path.substring(path.lastIndexOf("/")+1,path.length()-3);
	System.out.print(path);
	hashMapping mapping=(hashMapping)map.get(path);
	String form=mapping.getForm();
	System.out.println(form);//得到form 
	String action=mapping.getAction();
	System.out.println(action);//得到action
    if(form.equals("null"))
    {
    	System.out.println("form为空");
    	ActionFather actionfather=null;
    	actionfather=getActionInstance("ldsj.gjm.action."+action);//得到action实例
    	try
    	{
    		Method nn=actionfather.getClass().getDeclaredMethod("execute",FormFather.class,HttpServletRequest.class,HttpServletResponse.class);
    	    nn.invoke(actionfather, null,request,response);
    		
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	
    }
    else
    {
    	formfather=getFormInstance("ldsj.gjm.form."+form);//得到实例
//		设set值
		Method[] mm=getMehodName(formfather);
		Enumeration enu=request.getParameterNames();//参数名
		while(enu.hasMoreElements())
		{
			String paraName=(String)enu.nextElement();
			for(int i=0;i<mm.length;i++)
			{
				if((mm[i].getName().startsWith("set"))&&(mm[i].getName().substring(3).toLowerCase().equals(paraName)))
				{
					try 
					{
						mm[i].invoke(formfather, request.getParameterValues(paraName));
					} catch (IllegalArgumentException e) {
					
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						
						e.printStackTrace();
					}
				}
			}
	}
	
	
	
	//调用相应action的方法 
	ActionFather actionfather=null;
	actionfather=getActionInstance("ldsj.gjm.action."+action);//得到action实例
	try
	{
		Method nn=actionfather.getClass().getDeclaredMethod("execute",FormFather.class,HttpServletRequest.class,HttpServletResponse.class);
	    nn.invoke(actionfather, formfather,request,response);
		
	}
	catch(Exception e)
	{
	  e.printStackTrace();	
	}
    }
		
    
    
  }

	public void init() throws ServletException 
	{
		 //读取资源文件
		System.out.println("init......");
		ResourceBundle rbu=ResourceBundle.getBundle("fileconfig");
		Enumeration enu=rbu.getKeys();
		String value="";
		String key="";
		
		
		while(enu.hasMoreElements())
	    {
		    key=(String)enu.nextElement();
			value=rbu.getString(key);	
			String[] yy=value.split("=");
			hashMapping mapping=new hashMapping();
			mapping.setKey(key);
			mapping.setForm(yy[0]);
			mapping.setAction(yy[1]);
			map.put(key, mapping);
			
	    }
		
		
		
		
	}
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -