Home >Java >javaTutorial >Detailed introduction to Struts1 url interception

Detailed introduction to Struts1 url interception

黄舟
黄舟Original
2018-05-24 15:19:261714browse

This article mainly introduces the url interception of Struts1_related information compiled by the Power Node Java Academy. Friends in need can refer to

Struts1 url interception

First let's analyze ActionServlet in depth. We use breakpoint debugging to look at the underlying source code. Because this instance is submitted in post mode, set the breakpoint to the doPost method.


We debug the program and enter the method inside doPost:

This method is very Importantly, it is the core method of ActionServlet running.

We enter this method:


Then continue to enter:



We suddenly discovered that such a method is the processPath method, which is a method of intercepting strings. 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 determines whether the path information exists in javax.servlet.include.path_info. Here you need to know the Dangdang one This attribute value only exists if the page is displayed in RequestDispatcher.include mode. So 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 to determine whether javax.servlet.include.servlet_path has a value. This attribute value only exists when a page is displayed in requestDispatcher.include mode, so the value here does not exist. Then enter path = request.getServletPath(); This method is to get the substring after returning the request URI context, so the return value here is "/" and the access page name and suffix (is this the same as what my mvc instance intercepts? principle). 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 method here is mainly the same as mine above, the main thing is to remove the suffix.

The above is the detailed content of Detailed introduction to Struts1 url interception. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn