how can I get the servlet access url from servlet context

Discussion in 'Java' started by shaoanqing@gmail.com, Apr 18, 2007.

  1. Guest

    hi
    I am coding for a WebApp
    I wrote a servlet and defined the mapping in web.xml like:

    <servlet>
    <display-name>MySer</display-name>
    <servlet-name>MySer</servlet-name>
    <servlet-class>com.santa.servlet.MyServlet</servlet-class>
    <load-on-startup>30</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/service/MySer</url-pattern>
    </servlet-mapping>

    I am now can access the servlet using the URL:
    http://localhost:8080/myapp/service/MySer

    the question is how can I get the URL from the servlet Context
    or at least how can I get the url pattern("service/MySer") from
    servletContext?

    thanks.
    , Apr 18, 2007
    #1
    1. Advertising

  2. wrote:
    > I am now can access the servlet using the URL:
    > http://localhost:8080/myapp/service/MySer
    >
    > the question is how can I get the URL from the servlet Context
    > or at least how can I get the url pattern("service/MySer") from
    > servletContext?


    request.getRequestURL()

    Arne
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Apr 19, 2007
    #2
    1. Advertising

  3. santax Guest

    On Apr 19, 9:52 am, Arne Vajhøj <> wrote:
    > wrote:
    > > I am now can access the servlet using the URL:
    > >http://localhost:8080/myapp/service/MySer

    >
    > > the question is how can I get the URL from the servlet Context
    > > or at least how can I get the url pattern("service/MySer") from
    > > servletContext?

    >
    > request.getRequestURL()
    >
    > Arne

    Thanks.
    I am sorry , I was not present my problem clearly,
    I want get the URL before any user accessing it.
    I set the servle as load-on-startup servlet by definded below in
    web.xml

    <servlet>
    <display-name>MySer</display-name>
    <servlet-name>MySer</servlet-name>
    <servlet-class>com.santa.servlet.MyServlet</servlet-class>
    <load-on-startup>30</load-on-
    startup>
    <<---------------------------------------- init the servlet when
    server start
    </servlet>
    <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/service/MySer</url-pattern>
    </servlet-mapping>
    so ,I want get the url-pattern in the method

    @Override
    public void init(ServletConfig arg0) throws ServletException {
    System.out.println("*****************************");
    // I want get the url-pattern here
    System.out.println("*****************************");
    super.init(arg0);
    }

    Thank you very much for your reply.
    santax, Apr 20, 2007
    #3
  4. Guest

    On Apr 19, 6:06 pm, santax <> wrote:
    > On Apr 19, 9:52 am, Arne Vajhøj <> wrote:> wrote:
    > > > I am now can access the servlet using the URL:
    > > >http://localhost:8080/myapp/service/MySer

    >
    > > > the question is how can I get the URL from the servlet Context
    > > > or at least how can I get the url pattern("service/MySer") from
    > > > servletContext?

    >
    > > request.getRequestURL()

    >
    > > Arne

    >
    > Thanks.
    > I am sorry , I was not present my problem clearly,
    > I want get the URL before any user accessing it.
    > I set the servle as load-on-startup servlet by definded below in

    web.xml
    >
    > <servlet>
    > <display-name>MySer</display-name>
    > <servlet-name>MySer</servlet-name>
    > <servlet-class>com.santa.servlet.MyServlet</servlet-class>
    > <load-on-startup>30</load-on-
    > startup>
    > <<---------------------------------------- init the servlet when
    > server start
    > </servlet>
    > <servlet-mapping>
    > <servlet-name>MyServlet</servlet-name>
    > <url-pattern>/service/MySer</url-pattern>
    > </servlet-mapping>
    > so ,I want get the url-pattern in the method
    >
    > @Override
    > public void init(ServletConfig arg0) throws ServletException {
    > System.out.println("*****************************");
    > // I want get the url-pattern here
    > System.out.println("*****************************");
    > super.init(arg0);
    > }
    >
    > Thank you very much for your reply.


    Who says there is one? There could be none at all, if load-on-startup
    is turned on on the servlet entry. For that matter there could be
    several distinct mappings configured.

    The only time URL information is available to a servlet is when it's
    processing a request. During that time the URL used for that request
    is available to the servlet. Your container (eg., Tomcat or Weblogic)
    may provide extensions that allow you more detailed access to the
    application configuration, but the Java EE specification doesn't
    require it.
    , Apr 20, 2007
    #4
  5. santax Guest

    On Apr 20, 3:04 pm, wrote:
    > On Apr 19, 6:06 pm, santax <> wrote:
    >
    >
    >
    > > On Apr 19, 9:52 am, Arne Vajhøj <> wrote:> wrote:
    > > > > I am now can access the servlet using the URL:
    > > > >http://localhost:8080/myapp/service/MySer

    >
    > > > > the question is how can I get the URL from the servlet Context
    > > > > or at least how can I get the url pattern("service/MySer") from
    > > > > servletContext?

    >
    > > > request.getRequestURL()

    >
    > > > Arne

    >
    > > Thanks.
    > > I am sorry , I was not present my problem clearly,
    > > I want get the URL before any user accessing it.
    > > I set the servle as load-on-startup servlet by definded below in

    > web.xml
    >
    > > <servlet>
    > > <display-name>MySer</display-name>
    > > <servlet-name>MySer</servlet-name>
    > > <servlet-class>com.santa.servlet.MyServlet</servlet-class>
    > > <load-on-startup>30</load-on-
    > > startup>
    > > <<---------------------------------------- init the servlet when
    > > server start
    > > </servlet>
    > > <servlet-mapping>
    > > <servlet-name>MyServlet</servlet-name>
    > > <url-pattern>/service/MySer</url-pattern>
    > > </servlet-mapping>
    > > so ,I want get the url-pattern in the method

    >
    > > @Override
    > > public void init(ServletConfig arg0) throws ServletException {
    > > System.out.println("*****************************");
    > > // I want get the url-pattern here
    > > System.out.println("*****************************");
    > > super.init(arg0);
    > > }

    >
    > > Thank you very much for your reply.

    >
    > Who says there is one? There could be none at all, if load-on-startup
    > is turned on on the servlet entry. For that matter there could be
    > several distinct mappings configured.
    >
    > The only time URL information is available to a servlet is when it's
    > processing a request. During that time the URL used for that request
    > is available to the servlet. Your container (eg., Tomcat or Weblogic)
    > may provide extensions that allow you more detailed access to the
    > application configuration, but the Java EE specification doesn't
    > require it.


    So i can't get the url-pattern during init of servlet as your reply.
    Then is there any way reading the web.xml directly??
    santax, Apr 20, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    1,993
    Jack Jia
    Apr 4, 2004
  2. Andrew Purser
    Replies:
    3
    Views:
    5,075
    Alex Kay
    Nov 4, 2004
  3. Replies:
    3
    Views:
    29,091
    Stuart Wood
    Jun 6, 2006
  4. After
    Replies:
    9
    Views:
    915
  5. LB
    Replies:
    1
    Views:
    847
Loading...

Share This Page