Java package-based request routing implementation instance sharing _java

Source: Internet
Author: User

Webfilter.java

Copy Code code as follows:

Package Com.hongyuan.route;

Import Java.io.File;
Import java.io.IOException;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;

Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;

public class Webfilter implements Filter {

public static final String base_package= "Com.hongyuan";
public static final String class_flag= "Action";
@Override
public void Destroy () {}

@Override
public void Dofilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {

if (!hasresouce (request)) {

String requestpath= ((httpservletrequest) request). Getservletpath ();
if (Requestpath.indexof (".") >=0) {
Requestpath=requestpath.substring (0, Requestpath.indexof ("."));
}
if (Requestpath.endswith ("/")) {
Requestpath=requestpath.substring (0, Requestpath.length ()-1);
}
Get the requested class fully qualified name
String Classname=base_package+requestpath.replaceall ("/", ".") +class_flag;
Get Request Method Name
String Methodname=request.getparameter ("method");
if (methodname==null| | "". Equals (Methodname.trim ())) {
Methodname= "Index";
}
try {
Gets the processing class and responds to the request
Class Clazz=class.forname (className);
Object Instance=this.initcontext (Clazz,request,response);
Method Method=clazz.getmethod (MethodName, New class[]{});
Object Result=method.invoke (instance, new object[]{});
Response.getwriter (). Print (result);
catch (Exception e) {
E.printstacktrace (Response.getwriter ());
}
}else{
Chain.dofilter (request, response);
}

 }
 //to determine if there is a requested resource
 private Boolean hasresouce (ServletRequest request) {
  string Realpath=request.getservletcontext (). Getrealpath ((httpservletrequest) request). Getservletpath ());
  file resouce=new File (Realpath);
  if (resouce.exists ()) {
   return true;
  }else{
   return false;
 &NBSP}
 }
 //Initialization context
 private Object initcontext (Class clazz, servletrequest request,
   servletresponse response) throws exception{
  object instance=clazz.newinstance ();
  clazz.getmethod ("Setrequest", New Class[]{servletrequest.class}). Invoke (instance, new object[]{ Request});
  clazz.getmethod ("Setresponse", New Class[]{servletresponse.class}). Invoke (instance, new object[]{ Response});
  return instance;
 }

@Override
public void init (Filterconfig arg0) throws Servletexception {}

}


Baseaction.java

Copy Code code as follows:

Package Com.hongyuan.route;

Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;

Public abstract class Baseaction {
protected ServletRequest request=null;
protected Servletresponse response=null;

Public ServletRequest getrequest () {
return request;
}
public void Setrequest (ServletRequest request) {
This.request = Request;
}
Public Servletresponse GetResponse () {
return response;
}
public void Setresponse (Servletresponse response) {
This.response = response;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.