【问题标题】:How to get servlet mapping by servlet name in tomcat 6?如何在tomcat 6中通过servlet名称获取servlet映射?
【发布时间】:2021-05-22 02:21:28
【问题描述】:

在 servlet 3.0 & tomcat 8 中,我们可以通过 servlet 名称得到 servlet 映射如下:

request.getServletContext().getServletRegistration(servletName).getMappings()

但是在tomcat6中,getServletRegistration是不存在的,那么如何通过servlet名称获取servlet的url映射呢?

【问题讨论】:

    标签: java tomcat servlets tomcat6


    【解决方案1】:

    由于没有人回复所以我回答我的问题。我找到了一种方法:

    由于tomcat6不支持动态添加servlet和端点注解,所以我找到了一种解决方法,在监听器中直接读取web.xml中的servlet信息:

    public class SomeListener implements ServletContextListener {
    
        public static ServletContext context;
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            context = sce.getServletContext();
            Map<String, String> valuesMap = new HashMap<String , String>();
            try {
                String fileName = context.getRealPath("/")+"/WEB-INF/web.xml";
                FileInputStream fileInputStream = new FileInputStream(fileName);
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document document = docBuilder.parse(fileInputStream);
                XPathFactory factory = XPathFactory.newInstance();
                XPath xpath = factory.newXPath();
                NodeList nodeList = (NodeList)xpath.compile("/web-app/servlet-mapping").evaluate(document, XPathConstants.NODESET);
                for (int index = 0; index < nodeList.getLength(); index++) {
                    Node node = nodeList.item(index);
                    if( node instanceof Element) {
                        Element docElement = (Element) node;
                        Node cell = docElement.getElementsByTagName("servlet-name").item(0);
                        Node cell2 = docElement.getElementsByTagName("url-pattern").item(0);
                        if ( cell != null && cell2 != null ){
                            valuesMap.put(cell.getTextContent() , cell2.getTextContent());
                        }
                    }
                }
                context.setAttribute("servletMapping" , valuesMap );
                System.out.println("init the servlet map." );
            }catch (Exception exception){
                exception.printStackTrace();
            }
        }
    
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            context = null;
        }
    
    }
    

    只需获取映射表单上下文:

        public static String getMapping( HttpServletRequest request , String servletName){
            return ((Map<String, String>)request.getSession().getServletContext().getAttribute("servletMapping")).get(servletName);
        }
    

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2010-11-08
      相关资源
      最近更新 更多