Struts2框架与servlet共存问题

Struts2框架与servlet共存问题


    今天在做一个项目的时候遇到了一个很奇怪的问题,这个项目的一部分是之前用servlet写的,但是后边打算用struts写,再加入struts框架配置后发现原先的那部分不能正常访问了,之前没这么做过所以很纳闷,struts2配置自认为写的很熟了,应该没什么问题啊,struts.xml文件改了又改还是找不到错误,后来在看到web.xml时突然眼前一亮。。
  
  
  1. <img src="https://img-blog.csdn.net/20140124152934046?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhb2hvbmd4dWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">  
  1.   
  1. <span style="font-size:24px;">原来是所有的请求都被struts2拦截了,<span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;">struts2把servlet当成action了,因为servlet和action都是没有后缀的。</span><span style="color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);"><span style="word-wrap: normal; word-break: normal; line-height: 28px; font-family: 宋体; color: red;"></span></span></span>  
  1. <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><span style="font-size:24px;"> 很是悲剧,要想使servlet和struts各司其职只能是限制struts的拦截范围了,</span></span>  
  1. <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;">  
  2. </span>  
  1. <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><img src="https://img-blog.csdn.net/20140124153004875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhb2hvbmd4dWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">  
  2. </span>  
  1. <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><span style="font-size:24px;">这里在struts.xml的配置里边加一个这个就可以限制struts只拦截action类型的请求了,当然在发起请求时也是要写清楚http://XXX/XX.action了这个action就不能向之前那样省掉了。</span></span>  
  1. <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><span style="font-size:24px;">下面给出网上的几种方法:</span></span>  
  1. <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"></span><p style="color: rgb(51, 51, 51); margin: 10px auto; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px;"><strong>方式一:修改servlet的相关配置,统一在servlet后面加上“.servlet”</strong>  
  2. 1.修改web.xml配置文件形如:</p><p style="color: rgb(51, 51, 51); margin: 10px auto; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px;"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_7_3832600" name="code" class="html"> <servlet>    
  3.        <servlet-name>jqueryAjaxServlet</servlet-name>    
  4.        <servlet-class>com.clzhang.sample.struts2.servlet.jQueryAjaxServlet</servlet-class>    
  5.     </servlet>    
  6.   
  7.     <servlet-mapping>    
  8.         <servlet-name>jqueryAjaxServlet</servlet-name>    
  9.         <url-pattern>/servlet/jqueryAjax.servlet</url-pattern>    
  10.     </servlet-mapping>  
  11. </pre><br><br><p></p><p></p><div class="cnblogs_code" style="color:rgb(51,51,51); line-height:26px; font-family:'Courier New'; border:1px solid rgb(204,204,204); padding:5px; overflow:auto; margin:5px 0px"><div style="background-color:rgb(245,245,245); font-size:14px"></div><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:24px">2.修改调用servlet的地方,形如:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_8_6168129" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;">    <%  
  12.     String path = request.getContextPath();  
  13.     %>  
  14.     ......  
  15.       
  16.     $.ajax({     
  17.     url:'<%=path%>/servlet/jqueryAjax.servlet',     
  18. </pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:18px">这样就可以正常处理servlet的请求了。此种情况适合于小范围使用servlet的情况。</span></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><strong><span style="font-size:18px">方式二:修改拦截页面配置,就是将struts的相关拦截配置一下</span></strong></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:18px">修改web.xml文件如下内容:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_9_1358580" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;">    <filter-mapping>  
  19.         <filter-name>struts2</filter-name>  
  20.         <url-pattern>*.action</url-pattern>  
  21.     </filter-mapping>  
  22.     <filter-mapping>  
  23.         <filter-name>struts2</filter-name>  
  24.         <url-pattern>*.do</url-pattern>  
  25.     </filter-mapping>  
  26.     <filter-mapping>  
  27.         <filter-name>struts2</filter-name>  
  28.         <url-pattern>*.jsp</url-pattern>  
  29.     </filter-mapping>  
  30.     <filter-mapping>  
  31.         <filter-name>struts2</filter-name>  
  32.         <url-pattern>/user/*</url-pattern>  
  33.     </filter-mapping></pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><br></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><strong><span style="font-size:24px">方式三:自定义Filter实现过滤</span></strong></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:24px">1.创建实现类,代码参考如下:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_10_9613692" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;">package com.clzhang.sample.struts2;  
  34.   
  35. import java.io.IOException;  
  36. import java.util.ArrayList;  
  37. import java.util.Arrays;  
  38. import javax.servlet.Filter;  
  39. import javax.servlet.FilterChain;  
  40. import javax.servlet.FilterConfig;  
  41. import javax.servlet.RequestDispatcher;  
  42. import javax.servlet.ServletException;  
  43. import javax.servlet.ServletRequest;  
  44. import javax.servlet.ServletResponse;  
  45. import javax.servlet.http.HttpServletRequest;  
  46.   
  47. /**  
  48.  * 这是一个过滤servlet的filter;就是拦截servlet的处理请求,自行转向处理,而不是由struts处理。  
  49.  * 有二种过滤方式,一种是判断uri中是否包含“/servlet/”;  
  50.  * 另一种是所有servlet在初始化参数中指定。  
  51.  * 需要在web.xml中配置:  
  52.     <filter>  
  53.         <filter-name>redisp</filter-name>  
  54.         <filter-class>com.clzhang.sample.struts2.FilterServlet</filter-class>  
  55.         <!--如果使用第二种方式过滤,则需要下面的代码  
  56.         <init-param>  
  57.             <param-name>includeServlets</param-name>  
  58.             <param-value>jqueryAjax,jsonView</param-value>  
  59.         </init-param>  
  60.         -->  
  61.     </filter>  
  62.       
  63.     <filter-mapping>  
  64.         <filter-name>redisp</filter-name>  
  65.         <url-pattern>/*</url-pattern>  
  66.     </filter-mapping>  
  67.     并且此过滤器必须在struts2的过滤器上面。  
  68.  * @author Administrator  
  69.  *  
  70.  */  
  71. public class FilterServlet implements Filter {  
  72.     public void destroy() {  
  73.     }  
  74.       
  75.     public void doFilter(ServletRequest req, ServletResponse resp,  
  76.             FilterChain chain) throws IOException, ServletException {  
  77.         HttpServletRequest request = (HttpServletRequest) req;  
  78.           
  79.         // 第一种方式(二选 一)  
  80.         String target = request.getRequestURI();  
  81.         target = target.lastIndexOf("?") > 0 ? target.substring(  
  82.                 target.lastIndexOf("/") + 1,  
  83.                 target.lastIndexOf("?") - target.lastIndexOf("/")) : target  
  84.                 .substring(target.lastIndexOf("/") + 1);  
  85.         if (request.getRequestURI().indexOf("/servlet/") > 0) {  
  86.             // request.getRequestURI()格式应该形如:/st/servlet/jqueryAjax,  
  87.             // 其中st是项目名,servlet是所有servlet都增加的前缀,用于能够判断出是servlet。  
  88.             // if只判断请求uri是否包含/servlet/,如果包含则处理;  
  89.             RequestDispatcher rdsp = request.getRequestDispatcher(target);  
  90.             rdsp.forward(req, resp);  
  91.         } else {  
  92.             chain.doFilter(req, resp);  
  93.         }  
  94.   
  95.         // 第二种方式 (二选 一)  
  96.         /**  
  97.         if (this.includes.contains(target)) {  
  98.             // target取出的值则直接是jqueryAjax,在web.xml中配置即可。  
  99.             // if判断请求uri最后的那部分是否包含在配置文件中,如果包含,则处理  
  100.             RequestDispatcher rdsp = request.getRequestDispatcher(target);  
  101.             rdsp.forward(req, resp);  
  102.         } else {  
  103.             chain.doFilter(req, resp);  
  104.         }  
  105.         */  
  106.     }  
  107.   
  108.     private ArrayList<String> includes = new ArrayList<String>();  
  109.   
  110.     public void init(FilterConfig config) throws ServletException {  
  111.         // 如果使用第二种方式过滤,则需要以下代码  
  112. //        this.includes.addAll(Arrays.asList(config.getInitParameter(  
  113. //                "includeServlets").split(",")));  
  114.     }  
  115. }</pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:24px">2.修改web.xml,类似如下格式:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_11_2852296" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;"><?xml version="1.0" encoding="UTF-8"?>  
  116. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  117.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  118.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  119.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  120.     <filter>  
  121.         <filter-name>redisp</filter-name>  
  122.         <filter-class>com.clzhang.sample.struts2.FilterServlet</filter-class>  
  123.         <!--如何使用第二种方式过滤,则需要下面的代码  
  124.         <init-param>  
  125.             <param-name>includeServlets</param-name>  
  126.             <param-value>jqueryAjax,jsonView</param-value>  
  127.         </init-param>  
  128.         -->  
  129.     </filter>  
  130.       
  131.     <filter-mapping>  
  132.         <filter-name>redisp</filter-name>  
  133.         <url-pattern>/*</url-pattern>  
  134.     </filter-mapping>  
  135.   
  136.     <filter>  
  137.         <filter-name>struts2</filter-name>  
  138.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  139.     </filter>  
  140.   
  141.     <filter-mapping>  
  142.         <filter-name>struts2</filter-name>  
  143.         <url-pattern>/*</url-pattern>  
  144.     </filter-mapping>  
  145.           
  146.     <servlet>    
  147.         <servlet-name>jqueryAjaxServlet</servlet-name>    
  148.         <servlet-class>com.clzhang.sample.struts2.servlet.jQueryAjaxServlet</servlet-class>    
  149.     </servlet>    
  150.   
  151.     <servlet-mapping>    
  152.         <servlet-name>jqueryAjaxServlet</servlet-name>    
  153.         <url-pattern>/servlet/jqueryAjax</url-pattern>    
  154.     </servlet-mapping>  
  155.   
  156.     <servlet>    
  157.         <servlet-name>jsonViewServlet</servlet-name>    
  158.         <servlet-class>com.clzhang.sample.struts2.servlet.JsonViewServlet</servlet-class>    
  159.     </servlet>    
  160.       
  161.     <servlet-mapping>    
  162.         <servlet-name>jsonViewServlet</servlet-name>    
  163.         <url-pattern>/servlet/jsonView</url-pattern>    
  164.     </servlet-mapping>     
  165.   
  166.     <welcome-file-list>  
  167.         <welcome-file>index.jsp</welcome-file>  
  168.         <welcome-file>index.html</welcome-file>  
  169.     </welcome-file-list>  
  170. </web-app></pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:18px; background-color:rgb(255,0,0)">注意<span style="color:rgb(0,0,255)"><filter-name>redisp</filter-name></span>必须放置于<span style="color:rgb(0,0,255)"><filter-name>struts2</filter-name></span>之前,这样才能够保证它们能够按照正常的顺序处理,否则会出错。</span></p><span style="background-color:rgb(245,245,245); font-size:14px"><br></span></div>  
  1. <span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(226, 226, 226);"><span style="word-wrap: normal; word-break: normal; line-height: 28px; font-size: 14pt; font-family: 宋体; color: red;">   </span></span>  
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值