Struts1 url intercept _ dynamic node Java school arrangement, struts1java
Url truncation of Struts1
First, let's analyze ActionServlet in depth. We can use the breakpoint debugging method to view the underlying source code. Because this instance is submitted in post mode, the breakpoint is set to the doPost method.
We run the program in debug and enter the method in doPost:
This method is very important as the core method for running ActionServlet.
Let's go to this method:
Continue to enter:
We suddenly found that this method is the processPath method, which is the string truncation method. The source code of this method is as follows:
/** * <p>Identify and return the path component(from the request URI) that * we will use to select an <code>ActionMapping</code> with which todispatch. * If no such path can be identified,create an error response and return * <code>null</code>.</p> * * @param request The servlet request weare processing * @param response The servlet response weare creating * * @exception IOException if an input/outputerror occurs */ protectedString processPath(HttpServletRequest request, HttpServletResponse response) throws IOException { String path = null; // For prefix matching, match on the path info (if any) path = (String) request.getAttribute(INCLUDE_PATH_INFO); if (path == null) { path = request.getPathInfo(); } if ((path != null) && (path.length() > 0)) { return (path); } // For extension matching, strip the module prefix and extension path = (String) request.getAttribute(INCLUDE_SERVLET_PATH); if (path == null) { path = request.getServletPath(); } String prefix = moduleConfig.getPrefix(); if (!path.startsWith(prefix)) { String msg =getInternal().getMessage("processPath"); log.error(msg + " " + request.getRequestURI()); response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); return null; } path = path.substring(prefix.length()); int slash = path.lastIndexOf("/"); int period = path.lastIndexOf("."); if ((period >= 0) && (period >slash)) { path = path.substring(0, period); } return (path); }
Analyze this Code:
path = (String)request.getAttribute(INCLUDE_PATH_INFO); if (path == null) { path = request.getPathInfo(); } if ((path != null) && (path.length() > 0)) { return (path); }
This code first checks whether the path information of javax. servlet. include. path_info exists. here we need to know that the attribute value exists only when a page is displayed as RequestDispatcher. include. If there is no value here, it will enter the path = request. getPathInfo () program. The value obtained by getPathInfo here is the path information relative to the servlet.
// For extension matching, stripthe module prefix and extension path = (String) request.getAttribute(INCLUDE_SERVLET_PATH); if (path == null) { path = request.getServletPath(); } String prefix = moduleConfig.getPrefix(); if (!path.startsWith(prefix)) { String msg =getInternal().getMessage("processPath"); log.error(msg + " " + request.getRequestURI()); response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); return null; }
This code is used to determine whether the javax. servlet. include. servlet_path value exists. This attribute value exists only when a page is displayed as equestDispatcher. include. Then enter path = request. getServletPath (); this method is used to obtain the substring after the returned URI context, therefore, the return value here is "/" and the access page name and suffix (here is the same as what is intercepted by my mvc instance ). Then enter the following code:
path = path.substring(prefix.length()); intslash = path.lastIndexOf("/"); intperiod = path.lastIndexOf("."); if((period >= 0) && (period > slash)) { path = path.substring(0, period); } return (path);
The methods here are basically the same as those above, mainly removing the suffix.