這篇文章主要介紹了Struts1之url截取_動力節點Java學院整理的相關資料,需要的朋友可以參考下
##Struts1之url截取
#先我們來對ActionServlet深層進行分析。我們用斷點的調試的方式來看底層源碼。因為這個實例是post方式提交,所以將斷點設定到doPost方法。
我們debug運作程序,進入doPost裡面的方法:
## 重要是ActionServlet運作的核心方法。
我們進入這個方法:
##卷
# 我們赫然發現了這樣一個方法就是processPath方法,而這個方法就是要截取字串的方法。這個方法的原始碼如下:
/** * <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); }
分析這段程式碼:
path = (String)request.getAttribute(INCLUDE_PATH_INFO); if (path == null) { path = request.getPathInfo(); } if ((path != null) && (path.length() > 0)) { return (path); }
這段程式碼首先判斷一下javax.servlet.include.path_info是否存在路徑訊息,這裡要知道當一個頁面是以RequestDispatcher.include方式顯示的話,這個屬性值才會存在。所以這裡沒有值,就會進入path=request.getPathInfo()程式中,這裡的getPathInfo所取得的值是相對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; }
path = path.substring(prefix.length()); intslash = path.lastIndexOf("/"); intperiod = path.lastIndexOf("."); if((period >= 0) && (period > slash)) { path = path.substring(0, period); } return (path);
以上是關於Struts1之url截取的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!