autocreatexml

package share.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class AutoCreateServletXmlTask extends Task{
 
 static  void  SetServletBody(String path ) {
   File file = new File(path);
   File[] files = file.listFiles();
   for (File fl : files) {
       if (fl.isDirectory())
        SetServletBody(fl.toString());
   else
    try {
     if(fl.getName().toLowerCase().endsWith(".class"))
     {
    
     
      String pathandname=fl.getCanonicalPath();
       String shortname=fl.getName();
       String fullname=pathandname.replace(allclasspath+"//","").replace("//",".");
       
      
       
     
      
       shortname=shortname.substring(0,shortname.length()-6);
       fullname=fullname.substring(0,fullname.length()-6);
       try {
        
         
       // System.out.println(AutoCreateServletXmlTask.class.getClassLoader());
        Class c=AutoCreateServletXmlTask.class.getClassLoader().loadClass(fullname);
           if(!(c.getSuperclass()!=null&&c.getSuperclass().getName().equals("javax.servlet.http.HttpServlet")))
           {
            continue;
           }
        
       } catch (Exception e) {
        // TODO Auto-generated catch block
       
        e.printStackTrace();
       }
       
      
         System.out.println(shortname);
         System.out.println(fullname);
         
         
         servletbody+="<servlet><servlet-name>"+shortname+"</servlet-name><servlet-class>"+fullname+"</servlet-class></servlet><servlet-mapping><servlet-name>"+shortname+"</servlet-name><url-pattern>/servlet/"+shortname+"</url-pattern></servlet-mapping>"+"/n";
     }
     
     
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
      }
 

  static String projectdir="C://workspace//jywj//";
  static String  allclasspath=projectdir+ "bin";
  static String servletbody="";
  public void execute() throws BuildException  {   
  
   SetServletBody(allclasspath);
   
   try{
   File file=new File("dd//WEB-INF//one.xml");
   file.createNewFile();
   BufferedWriter  writer = new java.io.BufferedWriter(new java.io.FileWriter(file));
         writer.write(servletbody);
 
   writer.close();
   System.out.println("create ok");
   }catch(Exception e)
   {
    
   }
    }   
 public static void main(String[] args) throws IOException {
  
   SetServletBody(allclasspath);
  
  
  File file=new File("dd//WEB-INF//one.xml");
  file.createNewFile();
  BufferedWriter  writer = new java.io.BufferedWriter(new java.io.FileWriter(file));
        writer.write(servletbody);
 
  writer.close();
  System.out.println("create ok");
 }
 
 
}
-----------------------------------------------------

package share.tools;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletServlet extends HttpServlet {
// private String[] locationArray;
 private List<ServletModel> servletModels;
  static String jbossdir=System.getProperty("jboss.home.dir");
  static String  allclasspath=jbossdir+ "//server//default//deploy//jywj.ear//jywj.war//WEB-INF//classes";
     void AddModels(String path ) {
    File file = new File(path);
    File[] files = file.listFiles();
    for (File fl : files) {
        if (fl.isDirectory())
         AddModels(fl.toString());
    else
     try {
      if(fl.getName().toLowerCase().endsWith(".class"))
      {
       String pathandname=fl.getCanonicalPath();
        String shortname=fl.getName();
        String fullname=pathandname.replace(allclasspath+"//","").replace("//",".");
       
          System.out.println(shortname);
          System.out.println(fullname);
          servletModels.add(new ServletModel("/servlet/" + shortname.substring(0,shortname.length()-6),
         fullname.substring(0,fullname.length()-6)));
      }
      
      
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
       }
 @Override
 public void init(ServletConfig arg0) throws ServletException {
 // String locations = arg0.getInitParameter("xmlLocations");
 // locationArray = locations.split(",");

  servletModels = new ArrayList<ServletModel>();
  // servletModels.add(new ServletModel("/servlet/BLoginServlet",
  // "jywj.bussiness.action.BloginServlet"));

  AddModels(allclasspath);
  

  
  
  System.out.println("servlet number:" + servletModels.size());

  super.init(arg0);
 }

 @Override
 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
   throws ServletException, IOException {
  this.service(arg0, arg1);
 }

 @Override
 protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
   throws ServletException, IOException {

  String requestURI = this.getRequestURI(arg0);

  ServletModel targetServeltModel = this
    .getRequestTargetServletModel(requestURI);

  try {
   HttpServlet targetServlet = this
     .getRequestTargetServlet(targetServeltModel);
   targetServlet.service(arg0, arg1);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private String getRequestURI(HttpServletRequest arg0) {
  return arg0.getRequestURI();
 }

 private ServletModel getRequestTargetServletModel(String requestURI) {
  ServletModel targetServeltModel = null;
  for (ServletModel servletModel : servletModels) {
   if (requestURI.endsWith(servletModel.urlParttern)) {
    targetServeltModel = servletModel;
    break;
   }
  }
  return targetServeltModel;
 }

 private HttpServlet getRequestTargetServlet(ServletModel targetServeltModel) {
  HttpServlet targetSevlet = null;
  try {
   targetSevlet = (HttpServlet) Class.forName(
     targetServeltModel.servletname).newInstance();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return targetSevlet;
 }

 class ServletModel {
  private String urlParttern;
  private String servletname;

  public ServletModel(String urlParttern, String servletname) {
   this.urlParttern = urlParttern;
   this.servletname = servletname;
  }

 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值