This is the 15th day of my participation in the August Text Challenge.More challenges in August

1. Web path analysis

https://juejin.cn/pins/recommended: / / address/port/routing address We focus on here is ok In front of the containerCopy the code

2. Route roadmap

2.1 Defining annotations 2.2 Scanning annotations during system initialization and caching parsed routes 2.3 Intercepting requests with custom filters 2.4 Parsing request paths and matching cached routes Handle business 2.5 Pay attention to singleton instantiation of action for business processing. 2.6 At present, the processing method here is only request of servlet, and response is just an example to understand the processCopy the code

3. Basic implementation

3.1 Annotations Simple definitions

@Retention(RetentionPolicy.RUNTIME) public @interface PathAnnotation { String path(); // here is the path String type(); // here is the type}Copy the code

3.2 Scanning annotations and caching paths

// Route the corresponding entity class. Public class Route {/** path */ private String path; /** private String method; /** method type */ private String type; /** private Object action; RouteList * @param Path * @return * @throws IllegalAccessException * @throws InstantiationException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void addAllRoutes(Set<Class> actionClass) throws InstantiationException, IllegalAccessException { if (null ! = actionClass) { for (Class cls : actionClass) { PathAnnotation pa = (PathAnnotation) cls.getAnnotation(PathAnnotation.class); String routPath = ""; if (null ! = pa) { routPath = pa.path(); } Method[] methods = cls.getMethods(); for (Method m : methods) { pa = m.getAnnotation(PathAnnotation.class); if (null == pa) { continue; } routPath += "/" + pa.path(); Route rt = new Route(routPath, m.getName(), pa.type(), BeanUtil.getBeanUtil().getObj(cls)); Logger.info (" Load route: "+ rt.toString()); routeList.add(rt); }}}}Copy the code

3.3 Path Matching

@param rs @param path @return */ public static Route findRoute(Routes rs,String path) { if(null==rs||rs.getRouteList().size()<1) { return null; }else { List<Route> allRoute=rs.getRouteList(); If (null==path) {return null; }else {/** * 1. Here we first remove the starting item address, then convert the character \ to /* 2. How to leave off * 3. How to leave off * 4 at the beginning of the project address. Item path matching returns normal route */ String currentPath=path.trim().replace('\\', '/'); if(currentPath.endsWith("/")) { currentPath=currentPath.substring(0,currentPath.length()-1); } if(currentPath.startsWith("/")) { currentPath=currentPath.substring(1,currentPath.length()); } for(Route rt:allRoute) { if(rt.getPath().equals(currentPath)) { return rt; } } } return null; }}Copy the code

4. Effect test

4.1 Specifying the Filter Configuration

<filter> <filter-name>pingpang</filter-name> <filter-class>com.pingpang.core.Core</filter-class> <init-param> <param-name>autoPackage</param-name> <! <param-value>com.pingpang. Test </param-value> </init-param> </filter> <filter-mapping> <filter-name>pingpang</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>Copy the code

4.2 test the action

@pathannotation (path = "hello", type = "") public class PingPangTest {@pathannotation (path = "test", type = "") public void helloWord(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { System.out.println("-----------"); request.getRequestDispatcher("/login.jsp").forward(request, response); }}Copy the code

4.3 rendering