例如http://localhost:8080/test/index.jsp
这里的test是项目名称,在server.xml中定义的,当它启动的时候如何获得test名称呢?
目前我是当用户请求过来的时候分析URL获得的,有没有办法在初始化的时候获得?谢谢

解决方案 »

  1.   

    写一个监听器。。在web.xml配置一下监听器里面就可以获得了
      

  2.   

    过滤器中public void init(FilterConfig fConfig) throws ServletException {
    appName = fConfig.getServletContext().getContextPath();
    //...}Servlet中request.getContextPath();
      

  3.   

    servletcontextlistener
    http://www.cnblogs.com/kentyshang/archive/2007/06/26/795878.html
    相关用法
      

  4.   


    package com.ex ;import java.io.PrintWriter ;
    import java.io.FileOutputStream ;
    import java.text.SimpleDateFormat ;
    import javax.servlet.* ;public class MyServletContextListener implements ServletContextListener, ServletContextAttributeListener{ public void contextInitialized(ServletContextEvent event){

    ServletContext sc = event.getServletContext() ;

    print("ServletContext初始化..." +  sc.getContextPath();
    }

    public void contextDestroyed(ServletContextEvent event){
    print("ServletContext被释放...") ;
    }

    public void attributeAdded(ServletContextAttributeEvent event){

    print("增加ServletContext对象的一个属性:attributeAdded('" + event.getName() + "','" + event.getValue()+"')") ;

    }

    public void attributeRemoved(ServletContextAttributeEvent event){
    print("删除ServletContext对象的一个属性:attributeRemoved('" + event.getName() + "','" + event.getValue()+"')") ;
    }

    public void attributeReplaced(ServletContextAttributeEvent event){
    print("更新ServletContext对象的一个属性:attributeReplaced('" + event.getName() + "','" + event.getValue()+"')") ;
    }

    private void print(String msg){

    PrintWriter out = null ;
    try{
    out = new PrintWriter(new FileOutputStream("D:\\output.txt", true) );
    out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss").format(new java.util.Date()) + "ContextListener: " + msg ) ;
    out.close() ;
    } catch(Exception e){
    e.printStackTrace() ;
    }
    }
    }<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"
      metadata-complete="true">      <description>
          Servlet and JSP Examples.
        </description>
        <display-name>Servlet and JSP Examples</display-name>
    <!--
    <welcome-list-file>
    <welcome-file>index.jsp</welcome-file>
    </welcome-list-file>
    -->
    <context-param>
    <param-name>breed</param-name>
    <param-value>Great Dane AA</param-value>
    </context-param> <servlet>
    <servlet-name>beer Advisor</servlet-name>
    <servlet-class>com.ex.web.BeerSelect</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>beer Advisor</servlet-name>
    <url-pattern>/view/SelectBeer.do</url-pattern>
    </servlet-mapping>

    <servlet>
    <servlet-name>ListenerTester</servlet-name>
    <servlet-class>com.ex.ListenerTester</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>ListenerTester</servlet-name>
    <url-pattern>/Tester.do</url-pattern>
    </servlet-mapping>

    <listener>
    <listener-class>com.ex.MyServletContextListener</listener-class>
    </listener>

    </web-app>