如何在tomcat 6 中通过servlet 名称获取servlet 映射?
How to get servlet mapping by servlet name in tomcat 6?
在servlet 3.0 & tomcat 8中,我们可以通过servlet名称获取servlet映射如下:
request.getServletContext().getServletRegistration(servletName).getMappings()
但在 tomcat6 中,getServletRegistration 不存在,那么如何通过 servlet 名称获取 servlet 的 url 映射?
既然没有人回复所以我回答我的问题。我找到了一种方法:
由于tomcat6不支持端点的动态添加servlet和注解,所以我找到了一种解决方法,直接读取监听器中的servlet信息表单web.xml:
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);
}
在servlet 3.0 & tomcat 8中,我们可以通过servlet名称获取servlet映射如下:
request.getServletContext().getServletRegistration(servletName).getMappings()
但在 tomcat6 中,getServletRegistration 不存在,那么如何通过 servlet 名称获取 servlet 的 url 映射?
既然没有人回复所以我回答我的问题。我找到了一种方法:
由于tomcat6不支持端点的动态添加servlet和注解,所以我找到了一种解决方法,直接读取监听器中的servlet信息表单web.xml:
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);
}