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;
}
}
}