servlet has been defined in structs2, how to use it when we create our own servlet

    Servlets have been defined in Structs2, and sometimes we don't need to write Servlets, but as we all know, Servlets extend the capabilities of servers by creating a framework to provide request and response services on the Web, and sometimes we still need to do it ourselves. To define our servlet, we need to use an Action to forward our own servlet. We have the following two methods for the use of servlet:
1. Add .servlet to the servlet name and
add .servlet to the end of the servlet (including web. xml configuration file and where servlet is used on the page), for example, the configuration in web.xml is as follows:    
<servlet-mapping>      
    <servlet-name>NewServlet</servlet-name>      
    <url-pattern>/New.servlet</url -pattern>    
</servlet-mapping>
2. Use Action to forward
When we use Structs2, we need to configure the web.xml file. When configuring the file, we will set <url-pattern> to /*, so set When all requests will go through struts2, as long as they pass through struts2, there must be an action corresponding to it, otherwise an error will be reported. Therefore, if you directly access /xxx/servlet/test in the jsp code of the view layer, an error will be reported and the action cannot be found. The content in the <url-pattern> here needs to be the same as the <result> configuration in structs.xml.
For this case, we just need to write an action and put it in struts.
Action requests will not be intercepted by others. In this case, the front desk can directly access /servlet/test. However, the url accessing struts2 must end with .action, so that it can be intercepted by struts2.














4、使用filter过滤servlet
①新建一个filter用于过滤servlet
public class ReDispatcherFilter implements Filter {       
    private ArrayList<String> includes = new ArrayList<String>();      
    @Override  
    public void destroy() {       }       
    @Override       
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain
        chain) throws IOException, ServletException {          
        HttpServletRequest request = (HttpServletRequest) req;         
        String target = request.getRequestURI();           
        target=target.lastIndexOf("?")>0?  
           target.substring(target.lastIndexOf("/")+1,target.lastIndexOf("?")-
           target.lastIndexOf("/")):target.substring(target.lastIndexOf("/") + 1);
        if (this.includes.contains(target)) {               
            RequestDispatcher rdsp = request.getRequestDispatcher(target);
            rdsp.forward(req,resp);          
         }else               
            chain.doFilter(req, resp);      
     }       
     @Override       
     public void init(FilterConfig config) throws ServletException {
this.includes.addAll(Arrays.asList(config.getInitParameter("includeServlets").spl       it(", ")));      
     } 
}
②在web.xml中做适当配置,在struts2的配置前加上下面的代码: 
<filter>     
    <filter-name>redisp</filter-name>     
    <filter-class>com.kcjxkj.filter.ReDispatcherFilter</filter-class>    
    <init-param>       
        <param-name>includeServlets</param-name>      
        <param- value>ValidateEmail</param-value>    
    </init-param>   
</filter>    
<filter-mapping>     
    <filter-name>redisp</filter-name>    
    <url-pattern>/*</url-pattern>   
< /filter-mapping>  
Among them, <param-value></param-value> is the name of the servlet to be filtered configured in web.xml, and multiples are separated by "," commas.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326587544&siteId=291194637
Recommended